View Javadoc

1   package net.sf.statcvs.charts;
2   
3   import java.awt.Color;
4   import java.util.Collection;
5   import java.util.Iterator;
6   import java.util.List;
7   
8   import net.sf.statcvs.Messages;
9   import net.sf.statcvs.output.ReportConfig;
10  import net.sf.statcvs.reportmodel.TimeLine;
11  import net.sf.statcvs.reportmodel.TimePoint;
12  
13  import org.jfree.chart.ChartFactory;
14  import org.jfree.chart.JFreeChart;
15  import org.jfree.chart.annotations.XYAnnotation;
16  import org.jfree.chart.axis.DateAxis;
17  import org.jfree.chart.plot.XYPlot;
18  import org.jfree.chart.renderer.xy.XYStepRenderer;
19  import org.jfree.data.time.Millisecond;
20  import org.jfree.data.time.TimeSeries;
21  import org.jfree.data.time.TimeSeriesCollection;
22  import org.jfree.data.xy.XYDataset;
23  
24  /**
25   * Creates charts from {@link net.sf.statcvs.reportmodel.TimeLine}s.
26   *
27   * @author Richard Cyganiak
28   * @version $Id: TimeLineChartMaker.java,v 1.8 2008/04/02 11:22:15 benoitx Exp $
29   */
30  public class TimeLineChartMaker {
31      private final ReportConfig config;
32      private final TimeLine timeLine;
33      private final String fileName;
34      private final List annotations;
35  
36      /**
37       * Creates a chart from a time line.
38       * @param config The configuration to use
39       * @param timeLine the time line data for the chart 
40       * @param fileName the file name for the PNG image
41       */
42      public TimeLineChartMaker(final ReportConfig config, final TimeLine timeLine, final String fileName, final Collection symbolicNames) {
43          this.config = config;
44          this.timeLine = timeLine;
45          this.fileName = fileName;
46          this.annotations = SymbolicNameAnnotation.createAnnotations(symbolicNames);
47      }
48  
49      public ChartImage toFile() {
50          if (this.timeLine.isEmpty()) {
51              return null;
52          }
53          final TimeSeriesCollection collection = new TimeSeriesCollection();
54          collection.addSeries(createTimeSeries(timeLine));
55  
56          final String range = timeLine.getRangeLabel();
57          final String domain = Messages.getString("DOMAIN_TIME");
58  
59          final XYDataset data = collection;
60          final JFreeChart chart = ChartFactory.createTimeSeriesChart(this.config.getProjectName() + ": " + timeLine.getTitle(), domain, range, data, false,
61                  false, false);
62  
63          final XYPlot plot = (XYPlot) chart.getPlot();
64          plot.getRenderer().setSeriesPaint(0, Color.blue);
65          final DateAxis axis = (DateAxis) plot.getDomainAxis();
66          axis.setVerticalTickLabels(true);
67          plot.getRangeAxis().setLowerBound(0);
68          plot.setRenderer(new XYStepRenderer());
69          for (final Iterator it = annotations.iterator(); it.hasNext();) {
70              plot.addAnnotation((XYAnnotation) it.next());
71          }
72          return this.config.createChartImage(this.fileName, this.timeLine.getTitle(), chart, this.config.getLargeChartSize());
73      }
74  
75      private TimeSeries createTimeSeries(final TimeLine timeLine) {
76          final TimeSeries result = new TimeSeries("!??!SERIES_LABEL!??!", Millisecond.class);
77          final Iterator it = timeLine.getDataPoints().iterator();
78          while (it.hasNext()) {
79              final TimePoint timePoint = (TimePoint) it.next();
80              result.add(new Millisecond(timePoint.getDate()), timePoint.getValue());
81          }
82          return result;
83      }
84  }