View Javadoc

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       * @param barLabels The labels for each bar
45       */
46      public TimeBarChartMaker(final String chartName, final ReportConfig config, final SortedSet revisions, final String title, final String fileName,
47              final String[] barLabels) {
48          this.config = config;
49          this.chartName = chartName;
50          this.revisions = revisions;
51          this.title = title;
52          this.fileName = fileName;
53          this.barLabels = barLabels;
54      }
55  
56      /**
57       * Creates a bar chart image file.
58       * @return An image file containing the chart
59       */
60      public ChartImage toFile() {
61          final int[] barValues = new int[this.barLabels.length];
62          for (int i = 0; i < barValues.length; i++) {
63              barValues[i] = 0;
64          }
65          final Iterator it = this.revisions.iterator();
66          while (it.hasNext()) {
67              final Revision rev = (Revision) it.next();
68              final Date date = rev.getDate();
69              final Calendar cal = new GregorianCalendar();
70              cal.setTime(date);
71              barValues[barNumberForTime(cal)]++;
72          }
73          final DefaultCategoryDataset data = new DefaultCategoryDataset();
74          for (int i = 0; i < barValues.length; i++) {
75              data.addValue(barValues[i], "Commits", this.barLabels[i]);
76          }
77          final JFreeChart chart = ChartFactory.createBarChart(this.config.getProjectName() + ": " + this.title, "", "Commits", data, PlotOrientation.VERTICAL,
78                  false, false, false);
79          final CategoryPlot plot = chart.getCategoryPlot();
80          plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);
81          plot.getRenderer().setSeriesPaint(0, Color.blue);
82  
83          plot.setBackgroundPaint(ChartConfigUtil.getPlotColor(chartName));
84          chart.setBackgroundPaint(ChartConfigUtil.getBackgroundColor(chartName));
85          //        final CategoryItemRenderer renderer = plot.getRenderer();
86          //        ChartConfigUtil.configureStroke(chartName, renderer, data);
87          //        ChartConfigUtil.configureShapes(chartName, renderer);
88          ChartConfigUtil.configureCopyrightNotice(chartName, chart);
89          ChartConfigUtil.configureChartBackgroungImage(chartName, chart);
90          ChartConfigUtil.configurePlotImage(chartName, chart);
91  
92          final Dimension dim = ChartConfigUtil.getDimension(chartName, config.getLargeChartSize());
93  
94          return this.config.createChartImage(this.fileName, this.title, chart, dim);
95      }
96  
97      protected abstract int barNumberForTime(Calendar time);
98  
99      public static class HourBarChartMaker extends TimeBarChartMaker {
100         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          * 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          */
111         public HourBarChartMaker(final String chartName, final ReportConfig config, final SortedSet revisions, final String title, final String fileName) {
112             super(chartName, config, revisions, title, fileName, HOURS);
113         }
114 
115         protected int barNumberForTime(final Calendar time) {
116             return time.get(Calendar.HOUR_OF_DAY);
117         }
118     }
119 
120     public static class WeekdayBarChartMaker extends TimeBarChartMaker {
121         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          */
131         public WeekdayBarChartMaker(final String chartName, final ReportConfig config, final SortedSet revisions, final String title, final String fileName) {
132             super(chartName, config, revisions, title, fileName, WEEKDAYS);
133         }
134 
135         protected int barNumberForTime(final Calendar time) {
136             return time.get(Calendar.DAY_OF_WEEK) - 1;
137         }
138     }
139 }