Coverage Report - net.sf.statcvs.charts.TimeBarChartMaker
 
Classes in this File Line Coverage Branch Coverage Complexity
TimeBarChartMaker
0%
0/42
0%
0/10
1.429
TimeBarChartMaker$HourBarChartMaker
0%
0/11
N/A
1.429
TimeBarChartMaker$WeekdayBarChartMaker
0%
0/12
N/A
1.429
 
 1  
 package net.sf.statcvs.charts;
 2  
 
 3  
 import java.awt.Color;
 4  
 import java.awt.Dimension;
 5  
 import java.util.Calendar;
 6  
 import java.util.Date;
 7  
 import java.util.GregorianCalendar;
 8  
 import java.util.Iterator;
 9  
 import java.util.SortedSet;
 10  
 
 11  
 import net.sf.statcvs.model.Revision;
 12  
 import net.sf.statcvs.output.ReportConfig;
 13  
 
 14  
 import org.jfree.chart.ChartFactory;
 15  
 import org.jfree.chart.JFreeChart;
 16  
 import org.jfree.chart.axis.CategoryLabelPositions;
 17  
 import org.jfree.chart.plot.CategoryPlot;
 18  
 import org.jfree.chart.plot.PlotOrientation;
 19  
 import org.jfree.data.category.DefaultCategoryDataset;
 20  
 
 21  
 /**
 22  
  * Produces bar charts where each bar represents a time slot, e.g. a weekday.,
 23  
  * and each revision from a given collection is sorted into the appropriate
 24  
  * slot.
 25  
  * 
 26  
  * @author jentzsch
 27  
  * @author Richard Cyganiak (richard@cyganiak.de)
 28  
  * @version $Id: TimeBarChartMaker.java,v 1.7 2009/04/25 16:36:20 benoitx Exp $
 29  
  */
 30  
 public abstract class TimeBarChartMaker {
 31  
     private final ReportConfig config;
 32  
     private final SortedSet revisions;
 33  
     private final String title;
 34  
     private final String fileName;
 35  
     private final String[] barLabels;
 36  
     private final String chartName;
 37  
 
 38  
     /**
 39  
      * Creates a new BarChartMaker.
 40  
      * @param config The configuration to use
 41  
      * @param revisions The revisions to analyze
 42  
      * @param title The chart's title
 43  
      * @param fileName The file name for the image file, including <tt>.png</tt> extension
 44  0
      * @param barLabels The labels for each bar
 45  0
      */
 46  0
     public TimeBarChartMaker(final String chartName, final ReportConfig config, final SortedSet revisions, final String title, final String fileName,
 47  0
             final String[] barLabels) {
 48  0
         this.config = config;
 49  0
         this.chartName = chartName;
 50  0
         this.revisions = revisions;
 51  0
         this.title = title;
 52  0
         this.fileName = fileName;
 53  0
         this.barLabels = barLabels;
 54  0
     }
 55  
 
 56  
     /**
 57  0
      * Creates a bar chart image file.
 58  0
      * @return An image file containing the chart
 59  0
      */
 60  
     public ChartImage toFile() {
 61  0
         final int[] barValues = new int[this.barLabels.length];
 62  0
         for (int i = 0; i < barValues.length; i++) {
 63  0
             barValues[i] = 0;
 64  0
         }
 65  0
         final Iterator it = this.revisions.iterator();
 66  0
         while (it.hasNext()) {
 67  0
             final Revision rev = (Revision) it.next();
 68  0
             final Date date = rev.getDate();
 69  0
             final Calendar cal = new GregorianCalendar();
 70  0
             cal.setTime(date);
 71  0
             barValues[barNumberForTime(cal)]++;
 72  0
         }
 73  0
         final DefaultCategoryDataset data = new DefaultCategoryDataset();
 74  0
         for (int i = 0; i < barValues.length; i++) {
 75  0
             data.addValue(barValues[i], "Commits", this.barLabels[i]);
 76  0
         }
 77  0
         final JFreeChart chart = ChartFactory.createBarChart(this.config.getProjectName() + ": " + this.title, "", "Commits", data, PlotOrientation.VERTICAL,
 78  0
                 false, false, false);
 79  0
         final CategoryPlot plot = chart.getCategoryPlot();
 80  0
         plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);
 81  0
         plot.getRenderer().setSeriesPaint(0, Color.blue);
 82  
 
 83  0
         plot.setBackgroundPaint(ChartConfigUtil.getPlotColor(chartName));
 84  0
         chart.setBackgroundPaint(ChartConfigUtil.getBackgroundColor(chartName));
 85  
         //        final CategoryItemRenderer renderer = plot.getRenderer();
 86  
         //        ChartConfigUtil.configureStroke(chartName, renderer, data);
 87  
         //        ChartConfigUtil.configureShapes(chartName, renderer);
 88  0
         ChartConfigUtil.configureCopyrightNotice(chartName, chart);
 89  0
         ChartConfigUtil.configureChartBackgroungImage(chartName, chart);
 90  0
         ChartConfigUtil.configurePlotImage(chartName, chart);
 91  
 
 92  0
         final Dimension dim = ChartConfigUtil.getDimension(chartName, config.getLargeChartSize());
 93  
 
 94  0
         return this.config.createChartImage(this.fileName, this.title, chart, dim);
 95  
     }
 96  0
 
 97  0
     protected abstract int barNumberForTime(Calendar time);
 98  0
 
 99  
     public static class HourBarChartMaker extends TimeBarChartMaker {
 100  0
         private final static String[] HOURS = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
 101  
                 "18", "19", "20", "21", "22", "23" };
 102  
 
 103  
         /**
 104  
          * Creates a bar chart showing a distribution of revisions
 105  0
          * over the hours of the day.
 106  
          * @param config The configuration to use
 107  
          * @param revisions The set of revisions to analyze
 108  
          * @param title The title of the chart
 109  
          * @param fileName The file for saving the chart
 110  0
          */
 111  0
         public HourBarChartMaker(final String chartName, final ReportConfig config, final SortedSet revisions, final String title, final String fileName) {
 112  0
             super(chartName, config, revisions, title, fileName, HOURS);
 113  0
         }
 114  0
 
 115  
         protected int barNumberForTime(final Calendar time) {
 116  0
             return time.get(Calendar.HOUR_OF_DAY);
 117  0
         }
 118  
     }
 119  0
 
 120  0
     public static class WeekdayBarChartMaker extends TimeBarChartMaker {
 121  0
         private final static String[] WEEKDAYS = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
 122  
 
 123  
         /**
 124  
          * Creates a bar chart showing a distribution of revisions
 125  
          * over the days of the week.
 126  
          * @param config The configuration to use
 127  
          * @param revisions The set of revisions to analyze
 128  
          * @param title The title of the chart
 129  
          * @param fileName The file for saving the chart
 130  0
          */
 131  0
         public WeekdayBarChartMaker(final String chartName, final ReportConfig config, final SortedSet revisions, final String title, final String fileName) {
 132  0
             super(chartName, config, revisions, title, fileName, WEEKDAYS);
 133  0
         }
 134  0
 
 135  
         protected int barNumberForTime(final Calendar time) {
 136  0
             return time.get(Calendar.DAY_OF_WEEK) - 1;
 137  
         }
 138  
     }
 139  
 }