| 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 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 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 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 0 | |
| 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 | |
| 58 | 0 | |
| 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 | |
|
| 86 | |
|
| 87 | |
|
| 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 | |
|
| 105 | 0 | |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 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 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 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 | |
} |