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.ChartConfigUtil;
28  import net.sf.statcvs.charts.ChartImage;
29  
30  import org.jfree.chart.ChartFactory;
31  import org.jfree.chart.JFreeChart;
32  import org.jfree.chart.annotations.XYAnnotation;
33  import org.jfree.chart.axis.DateAxis;
34  import org.jfree.chart.axis.NumberAxis;
35  import org.jfree.chart.plot.PlotOrientation;
36  import org.jfree.chart.plot.XYPlot;
37  import org.jfree.chart.renderer.xy.XYBarRenderer;
38  import org.jfree.chart.renderer.xy.XYItemRenderer;
39  import org.jfree.chart.renderer.xy.XYStepRenderer;
40  import org.jfree.data.time.TimeSeries;
41  import org.jfree.data.time.TimeSeriesCollection;
42  import org.jfree.data.xy.IntervalXYDataset;
43  
44  /**
45   * Class for producing Lines Of Code with Churn charts
46   * 
47   * @author Benoit Xhenseval (www.ObjectLab.co.uk)
48   */
49  public class LOCChurnChartMaker {
50      private static final double LOWER_MARGIN = 0.40;
51      private ChartImage chartFile = null;
52      private final String chartName;
53  
54      /**
55       * Creates a Lines Of Code chart from a <tt>BasicTimeSeries</tt> and saves
56       * it as PNG
57       * @param chartName 
58       * 
59       * @param churnSeries
60       *            the Churn history
61       * @param locSeries
62       *            the LOC history
63       * @param title
64       *            the chart title
65       * @param fileName
66       *            the filename where the chart will be saved
67       * @param width
68       *            width of PNG in pixels
69       * @param height
70       *            height of PNG in pixels
71       * @param annotations
72       */
73      public LOCChurnChartMaker(final String chartName, final ReportConfig config, final TimeSeries churnSeries, final TimeSeries locSeries, final String title,
74              final String fileName, final List annotations) {
75          this.chartName = chartName;
76          final TimeSeriesCollection collection = new TimeSeriesCollection(locSeries);
77          // collection.setDomainIsPointsInTime(false);
78          final TimeSeriesCollection churnCollection = new TimeSeriesCollection(churnSeries);
79          // churnCollection.setDomainIsPointsInTime(false);
80          final JFreeChart chart = createChart(collection, churnCollection, title, annotations);
81  
82          final Dimension dim = ChartConfigUtil.getDimension(chartName, config.getLargeChartSize());
83  
84          chartFile = config.createChartImage(fileName, title, chart, dim);
85      }
86  
87      public ChartImage toFile() {
88          return this.chartFile;
89      }
90  
91      private JFreeChart createChart(final TimeSeriesCollection locCollection, final TimeSeriesCollection churnSet, final String title, final List annotations) {
92          final String domain = Messages.getString("TIME_LOC_DOMAIN");
93          final String range = Messages.getString("TIME_LOC_RANGE");
94  
95          final IntervalXYDataset data = locCollection;
96          final boolean legend = true;// (locCollection.getSeriesCount() > 1);
97  
98          final JFreeChart chart = ChartFactory.createXYBarChart(ConfigurationOptions.getProjectName() + ":" + title, domain, true, range, data,
99                  PlotOrientation.VERTICAL, legend, false, false);
100 
101         final XYPlot plot = chart.getXYPlot();
102         plot.setRenderer(new XYStepRenderer());
103 
104         // new...
105         final NumberAxis rangeAxis1 = (NumberAxis) plot.getRangeAxis();
106         rangeAxis1.setLowerMargin(LOWER_MARGIN); // to leave room for volume bars
107 
108         final DateAxis domainAxis = (DateAxis) plot.getDomainAxis();
109         domainAxis.setVerticalTickLabels(true);
110 
111         // now add the churnSet
112         final NumberAxis rangeAxis2 = new NumberAxis(Messages.getString("CHURN_RANGE"));
113         rangeAxis2.setUpperMargin(1.00); // to leave room for price line
114         plot.setRangeAxis(1, rangeAxis2);
115         plot.setDataset(1, churnSet);
116         plot.setRangeAxis(1, rangeAxis2);
117         plot.mapDatasetToRangeAxis(1, 1);
118         final XYBarRenderer renderer2 = new XYBarRenderer(0.20);
119         plot.setRenderer(1, renderer2);
120 
121         if (annotations != null) {
122             for (final Iterator it = annotations.iterator(); it.hasNext();) {
123                 plot.addAnnotation((XYAnnotation) it.next());
124             }
125         }
126 
127         plot.setBackgroundPaint(ChartConfigUtil.getPlotColor(chartName));
128         chart.setBackgroundPaint(ChartConfigUtil.getBackgroundColor(chartName));
129         final XYItemRenderer renderer = plot.getRenderer();
130         ChartConfigUtil.configureStroke(chartName, renderer, data);
131         ChartConfigUtil.configureShapes(chartName, renderer);
132 
133         return chart;
134     }
135 }