| 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 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 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 | |
|
| 41 | |
|
| 42 | 0 | |
| 43 | 0 | |
| 44 | 0 | |
| 45 | 0 | public TimeLineChartMaker(final String chartName, final ReportConfig config, final TimeLine timeLine, final String fileName, final Collection symbolicNames) { |
| 46 | 0 | this.chartName = chartName; |
| 47 | 0 | this.config = config; |
| 48 | 0 | this.timeLine = timeLine; |
| 49 | 0 | this.fileName = fileName; |
| 50 | 0 | this.annotations = SymbolicNameAnnotation.createAnnotations(symbolicNames); |
| 51 | 0 | } |
| 52 | |
|
| 53 | 0 | public ChartImage toFile() { |
| 54 | 0 | if (this.timeLine.isEmpty()) { |
| 55 | 0 | return null; |
| 56 | 0 | } |
| 57 | 0 | final TimeSeriesCollection collection = new TimeSeriesCollection(); |
| 58 | 0 | collection.addSeries(createTimeSeries(timeLine)); |
| 59 | 0 | |
| 60 | 0 | final String range = timeLine.getRangeLabel(); |
| 61 | 0 | final String domain = Messages.getString("DOMAIN_TIME"); |
| 62 | |
|
| 63 | 0 | final XYDataset data = collection; |
| 64 | 0 | final JFreeChart chart = ChartFactory.createTimeSeriesChart(this.config.getProjectName() + ": " + timeLine.getTitle(), domain, range, data, false, |
| 65 | 0 | false, false); |
| 66 | 0 | |
| 67 | 0 | final XYPlot plot = (XYPlot) chart.getPlot(); |
| 68 | 0 | plot.getRenderer().setSeriesPaint(0, Color.blue); |
| 69 | 0 | final DateAxis axis = (DateAxis) plot.getDomainAxis(); |
| 70 | 0 | axis.setVerticalTickLabels(true); |
| 71 | 0 | plot.getRangeAxis().setLowerBound(0); |
| 72 | 0 | plot.setRenderer(new XYStepRenderer()); |
| 73 | 0 | for (final Iterator it = annotations.iterator(); it.hasNext();) { |
| 74 | 0 | plot.addAnnotation((XYAnnotation) it.next()); |
| 75 | |
} |
| 76 | 0 | |
| 77 | 0 | plot.setBackgroundPaint(ChartConfigUtil.getPlotColor(chartName)); |
| 78 | 0 | chart.setBackgroundPaint(ChartConfigUtil.getBackgroundColor(chartName)); |
| 79 | 0 | final XYItemRenderer renderer = plot.getRenderer(); |
| 80 | 0 | ChartConfigUtil.configureStroke(chartName, renderer, data); |
| 81 | 0 | ChartConfigUtil.configureShapes(chartName, renderer); |
| 82 | 0 | ChartConfigUtil.configureCopyrightNotice(chartName, chart); |
| 83 | 0 | ChartConfigUtil.configureChartBackgroungImage(chartName, chart); |
| 84 | 0 | ChartConfigUtil.configurePlotImage(chartName, chart); |
| 85 | |
|
| 86 | 0 | final Dimension dim = ChartConfigUtil.getDimension(chartName, config.getLargeChartSize()); |
| 87 | |
|
| 88 | 0 | return this.config.createChartImage(this.fileName, this.timeLine.getTitle(), chart, dim); |
| 89 | |
} |
| 90 | 0 | |
| 91 | 0 | private TimeSeries createTimeSeries(final TimeLine timeLine) { |
| 92 | 0 | final TimeSeries result = new TimeSeries("!??!SERIES_LABEL!??!", Millisecond.class); |
| 93 | 0 | final Iterator it = timeLine.getDataPoints().iterator(); |
| 94 | 0 | while (it.hasNext()) { |
| 95 | 0 | final TimePoint timePoint = (TimePoint) it.next(); |
| 96 | 0 | result.add(new Millisecond(timePoint.getDate()), timePoint.getValue()); |
| 97 | 0 | } |
| 98 | 0 | return result; |
| 99 | |
} |
| 100 | |
} |