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
22
23
24
25
26
27
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
38
39
40
41
42
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
54
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
89
90
91
92
93
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
109
110
111
112
113
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 }