View Javadoc

1   /*
2    StatSVN - SVN Subversion statistics generation 
3    Copyright (C) 2006 Benoit Xhenseval
4    http://www.statsvn.org
5    
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10  
11   This library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Lesser General Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with this library; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  		// collection.setDomainIsPointsInTime(false);
76  		final TimeSeriesCollection churnCollection = new TimeSeriesCollection(churnSeries);
77  		// churnCollection.setDomainIsPointsInTime(false);
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;// (locCollection.getSeriesCount() > 1);
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 		// new...
101 		final NumberAxis rangeAxis1 = (NumberAxis) plot.getRangeAxis();
102 		rangeAxis1.setLowerMargin(LOWER_MARGIN); // to leave room for volume bars
103 
104 		final DateAxis domainAxis = (DateAxis) plot.getDomainAxis();
105 		domainAxis.setVerticalTickLabels(true);
106 
107 		// now add the churnSet
108 		final NumberAxis rangeAxis2 = new NumberAxis(Messages.getString("CHURN_RANGE"));
109 		rangeAxis2.setUpperMargin(1.00); // to leave room for price line
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 }