View Javadoc

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