View Javadoc

1   /*
2    StatCVS - CVS statistics generation 
3    Copyright (C) 2006 Benoit Xhenseval
4    
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14  
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   
19   */
20  package net.sf.statcvs.output;
21  
22  import java.awt.Dimension;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import net.sf.statcvs.Messages;
27  import net.sf.statcvs.charts.ChartImage;
28  
29  import org.jfree.chart.ChartFactory;
30  import org.jfree.chart.JFreeChart;
31  import org.jfree.chart.annotations.XYAnnotation;
32  import org.jfree.chart.axis.DateAxis;
33  import org.jfree.chart.axis.NumberAxis;
34  import org.jfree.chart.plot.PlotOrientation;
35  import org.jfree.chart.plot.XYPlot;
36  import org.jfree.chart.renderer.xy.XYBarRenderer;
37  import org.jfree.chart.renderer.xy.XYStepRenderer;
38  import org.jfree.data.time.TimeSeries;
39  import org.jfree.data.time.TimeSeriesCollection;
40  import org.jfree.data.xy.IntervalXYDataset;
41  
42  /**
43   * Class for producing Lines Of Code with Churn charts
44   * 
45   * @author Benoit Xhenseval (www.ObjectLab.co.uk)
46   */
47  public class LOCChurnChartMaker {
48      private static final double LOWER_MARGIN = 0.40;
49      private ChartImage chartFile = null;
50  
51      /**
52       * Creates a Lines Of Code chart from a <tt>BasicTimeSeries</tt> and saves
53       * it as PNG
54       * 
55       * @param churnSeries
56       *            the Churn history
57       * @param locSeries
58       *            the LOC history
59       * @param title
60       *            the chart title
61       * @param fileName
62       *            the filename where the chart will be saved
63       * @param width
64       *            width of PNG in pixels
65       * @param height
66       *            height of PNG in pixels
67       * @param annotations
68       */
69      public LOCChurnChartMaker(final ReportConfig config, final TimeSeries churnSeries, final TimeSeries locSeries, final String title, final String fileName,
70              final Dimension size, final List annotations) {
71          final TimeSeriesCollection collection = new TimeSeriesCollection(locSeries);
72          // collection.setDomainIsPointsInTime(false);
73          final TimeSeriesCollection churnCollection = new TimeSeriesCollection(churnSeries);
74          // churnCollection.setDomainIsPointsInTime(false);
75          final JFreeChart chart = createChart(collection, churnCollection, title, annotations);
76  
77          chartFile = config.createChartImage(fileName, title, chart, size);
78      }
79  
80      public ChartImage toFile() {
81          return this.chartFile;
82      }
83  
84      private JFreeChart createChart(final TimeSeriesCollection locCollection, final TimeSeriesCollection churnSet, final String title, final List annotations) {
85          final String domain = Messages.getString("TIME_LOC_DOMAIN");
86          final String range = Messages.getString("TIME_LOC_RANGE");
87  
88          final IntervalXYDataset data = locCollection;
89          final boolean legend = true;// (locCollection.getSeriesCount() > 1);
90  
91          final JFreeChart chart = ChartFactory.createXYBarChart(ConfigurationOptions.getProjectName() + ":" + title, domain, true, range, data,
92                  PlotOrientation.VERTICAL, legend, false, false);
93  
94          final XYPlot plot = chart.getXYPlot();
95          plot.setRenderer(new XYStepRenderer());
96  
97          // new...
98          final NumberAxis rangeAxis1 = (NumberAxis) plot.getRangeAxis();
99          rangeAxis1.setLowerMargin(LOWER_MARGIN); // to leave room for volume bars
100 
101         final DateAxis domainAxis = (DateAxis) plot.getDomainAxis();
102         domainAxis.setVerticalTickLabels(true);
103 
104         // now add the churnSet
105         final NumberAxis rangeAxis2 = new NumberAxis(Messages.getString("CHURN_RANGE"));
106         rangeAxis2.setUpperMargin(1.00); // to leave room for price line
107         plot.setRangeAxis(1, rangeAxis2);
108         plot.setDataset(1, churnSet);
109         plot.setRangeAxis(1, rangeAxis2);
110         plot.mapDatasetToRangeAxis(1, 1);
111         final XYBarRenderer renderer2 = new XYBarRenderer(0.20);
112         plot.setRenderer(1, renderer2);
113 
114         if (annotations != null) {
115             for (final Iterator it = annotations.iterator(); it.hasNext();) {
116                 plot.addAnnotation((XYAnnotation) it.next());
117             }
118         }
119 
120         return chart;
121     }
122 }