1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.statsvn.output;
22
23 import java.awt.Dimension;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import net.sf.statcvs.Messages;
28 import net.sf.statcvs.charts.ChartImage;
29 import net.sf.statcvs.output.ConfigurationOptions;
30 import net.sf.statcvs.output.ReportConfig;
31
32 import org.jfree.chart.ChartFactory;
33 import org.jfree.chart.JFreeChart;
34 import org.jfree.chart.annotations.XYAnnotation;
35 import org.jfree.chart.axis.DateAxis;
36 import org.jfree.chart.axis.NumberAxis;
37 import org.jfree.chart.plot.PlotOrientation;
38 import org.jfree.chart.plot.XYPlot;
39 import org.jfree.chart.renderer.xy.XYBarRenderer;
40 import org.jfree.chart.renderer.xy.XYStepRenderer;
41 import org.jfree.data.time.TimeSeries;
42 import org.jfree.data.time.TimeSeriesCollection;
43 import org.jfree.data.xy.IntervalXYDataset;
44
45 /**
46 * Class for producing Lines Of Code with Churn charts
47 *
48 * @author Benoit Xhenseval (www.ObjectLab.co.uk)
49 */
50 public class LOCChurnChartMaker {
51 private static final double LOWER_MARGIN = 0.40;
52 private ChartImage chartFile = null;
53
54 /**
55 * Creates a Lines Of Code chart from a <tt>BasicTimeSeries</tt> and saves
56 * it as PNG
57 *
58 * @param churnSeries
59 * the Churn history
60 * @param locSeries
61 * the LOC history
62 * @param title
63 * the chart title
64 * @param fileName
65 * the filename where the chart will be saved
66 * @param width
67 * width of PNG in pixels
68 * @param height
69 * height of PNG in pixels
70 * @param annotations
71 */
72 public LOCChurnChartMaker(final ReportConfig config, final TimeSeries churnSeries, final TimeSeries locSeries, final String title, final String fileName,
73 final Dimension size, final List annotations) {
74 final TimeSeriesCollection collection = new TimeSeriesCollection(locSeries);
75
76 final TimeSeriesCollection churnCollection = new TimeSeriesCollection(churnSeries);
77
78 final JFreeChart chart = createChart(collection, churnCollection, title, annotations);
79
80 chartFile = config.createChartImage(fileName, title, chart, size);
81 }
82
83 public ChartImage toFile() {
84 return this.chartFile;
85 }
86
87 private JFreeChart createChart(final TimeSeriesCollection locCollection, final TimeSeriesCollection churnSet, final String title, final List annotations) {
88 final String domain = Messages.getString("TIME_LOC_DOMAIN");
89 final String range = Messages.getString("TIME_LOC_RANGE");
90
91 final IntervalXYDataset data = locCollection;
92 final boolean legend = true;
93
94 final JFreeChart chart = ChartFactory.createXYBarChart(ConfigurationOptions.getProjectName() + ":" + title, domain, true, range, data,
95 PlotOrientation.VERTICAL, legend, false, false);
96
97 final XYPlot plot = chart.getXYPlot();
98 plot.setRenderer(new XYStepRenderer());
99
100
101 final NumberAxis rangeAxis1 = (NumberAxis) plot.getRangeAxis();
102 rangeAxis1.setLowerMargin(LOWER_MARGIN);
103
104 final DateAxis domainAxis = (DateAxis) plot.getDomainAxis();
105 domainAxis.setVerticalTickLabels(true);
106
107
108 final NumberAxis rangeAxis2 = new NumberAxis(Messages.getString("CHURN_RANGE"));
109 rangeAxis2.setUpperMargin(1.00);
110 plot.setRangeAxis(1, rangeAxis2);
111 plot.setDataset(1, churnSet);
112 plot.setRangeAxis(1, rangeAxis2);
113 plot.mapDatasetToRangeAxis(1, 1);
114 final XYBarRenderer renderer2 = new XYBarRenderer(0.20);
115 plot.setRenderer(1, renderer2);
116
117 if (annotations != null) {
118 for (final Iterator it = annotations.iterator(); it.hasNext();) {
119 plot.addAnnotation((XYAnnotation) it.next());
120 }
121 }
122
123 return chart;
124 }
125 }