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