2 // Copyright (c) 2010 Anthony Towns
8 import java.util.ArrayList;
9 import java.util.HashMap;
11 import org.jfree.chart.ChartUtilities;
12 import org.jfree.chart.JFreeChart;
13 import org.jfree.chart.axis.AxisLocation;
14 import org.jfree.chart.axis.NumberAxis;
15 import org.jfree.chart.labels.StandardXYToolTipGenerator;
16 import org.jfree.chart.plot.PlotOrientation;
17 import org.jfree.chart.plot.XYPlot;
18 import org.jfree.chart.plot.ValueMarker;
19 import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
20 import org.jfree.chart.renderer.xy.XYItemRenderer;
21 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
22 import org.jfree.data.xy.XYSeries;
23 import org.jfree.data.xy.XYSeriesCollection;
24 import org.jfree.ui.RectangleAnchor;
25 import org.jfree.ui.TextAnchor;
27 import altosui.AltosDataPoint;
28 import altosui.AltosGraph;
30 class AltosGraphTime extends AltosGraph {
31 static interface Element {
32 void attachGraph(AltosGraphTime g);
33 void gotTimeData(double time, AltosDataPoint d);
34 void addToPlot(AltosGraphTime g, XYPlot plot);
37 static class TimeAxis implements Element {
41 private AxisLocation locn;
42 private double min_y = Double.NaN;
44 public TimeAxis(int axis, String label, Color color, AxisLocation locn)
52 public void setLowerBound(double min_y) {
56 public void attachGraph(AltosGraphTime g) { return; }
57 public void gotTimeData(double time, AltosDataPoint d) { return; }
59 public void addToPlot(AltosGraphTime g, XYPlot plot) {
60 NumberAxis numAxis = new NumberAxis(label);
61 if (!Double.isNaN(min_y))
62 numAxis.setLowerBound(min_y);
63 plot.setRangeAxis(axis, numAxis);
64 plot.setRangeAxisLocation(axis, locn);
65 numAxis.setLabelPaint(color);
66 numAxis.setTickLabelPaint(color);
67 numAxis.setAutoRangeIncludesZero(false);
71 abstract static class TimeSeries implements Element {
72 protected XYSeries series;
73 private String axisName;
76 public TimeSeries(String axisName, String label, Color color) {
77 this.series = new XYSeries(label);
78 this.axisName = axisName;
82 public void attachGraph(AltosGraphTime g) {
83 g.setAxis(this, axisName, color);
85 abstract public void gotTimeData(double time, AltosDataPoint d);
87 public void addToPlot(AltosGraphTime g, XYPlot plot) {
88 XYSeriesCollection dataset = new XYSeriesCollection();
89 dataset.addSeries(this.series);
91 XYItemRenderer renderer = new StandardXYItemRenderer();
92 renderer.setSeriesPaint(0, color);
94 int dataNum = g.getDataNum(this);
95 int axisNum = g.getAxisNum(this);
97 plot.setDataset(dataNum, dataset);
98 plot.mapDatasetToRangeAxis(dataNum, axisNum);
99 plot.setRenderer(dataNum, renderer);
103 static class StateMarker implements Element {
104 private double val = Double.NaN;
108 StateMarker(int state, String name) {
113 public void attachGraph(AltosGraphTime g) { return; }
114 public void gotTimeData(double time, AltosDataPoint d) {
115 if (Double.isNaN(val) || time < val) {
116 if (d.state() == state) {
122 public void addToPlot(AltosGraphTime g, XYPlot plot) {
123 if (Double.isNaN(val))
126 ValueMarker m = new ValueMarker(val);
128 m.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
129 m.setLabelTextAnchor(TextAnchor.TOP_LEFT);
130 plot.addDomainMarker(m);
134 private String callsign = null;
135 private Integer serial = null;
136 private Integer flight = null;
138 private String title;
139 private ArrayList<Element> elements;
140 private HashMap<String,Integer> axes;
141 private HashMap<Element,Integer> datasets;
142 private ArrayList<Integer> datasetAxis;
144 public AltosGraphTime(String title) {
145 this.filename = title.toLowerCase().replaceAll("[^a-z0-9]","_")+".png";
147 this.elements = new ArrayList<Element>();
148 this.axes = new HashMap<String,Integer>();
149 this.datasets = new HashMap<Element,Integer>();
150 this.datasetAxis = new ArrayList<Integer>();
153 public AltosGraphTime addElement(Element e) {
159 public void setAxis(Element ds, String axisName, Color color) {
160 Integer axisNum = axes.get(axisName);
161 int dsNum = datasetAxis.size();
162 if (axisNum == null) {
163 axisNum = newAxis(axisName, color);
165 datasets.put(ds, dsNum);
166 datasetAxis.add(axisNum);
169 public int getAxisNum(Element ds) {
170 return datasetAxis.get( datasets.get(ds) ).intValue();
172 public int getDataNum(Element ds) {
173 return datasets.get(ds).intValue();
176 private Integer newAxis(String name, Color color) {
177 int cnt = axes.size();
178 AxisLocation locn = AxisLocation.BOTTOM_OR_LEFT;
180 locn = AxisLocation.TOP_OR_RIGHT;
182 Integer res = new Integer(cnt);
184 this.addElement(new TimeAxis(cnt, name, color, locn));
188 public void addData(AltosDataPoint d) {
189 double time = d.time();
190 for (Element e : elements) {
191 e.gotTimeData(time, d);
193 if (callsign == null) callsign = d.callsign();
194 if (serial == null) serial = new Integer(d.serial());
195 if (flight == null) flight = new Integer(d.flight());
198 public JFreeChart createChart() {
199 NumberAxis xAxis = new NumberAxis("Time (s)");
200 xAxis.setAutoRangeIncludesZero(false);
201 XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
202 XYPlot plot = new XYPlot();
203 plot.setDomainAxis(xAxis);
204 plot.setRenderer(renderer);
205 plot.setOrientation(PlotOrientation.VERTICAL);
207 if (serial != null && flight != null) {
208 title = serial + "/" + flight + ": " + title;
210 if (callsign != null) {
211 title = callsign + " - " + title;
214 renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
215 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
217 ChartUtilities.applyCurrentTheme(chart);
219 plot.setDomainPannable(true);
220 plot.setRangePannable(true);
222 for (Element e : elements) {
223 e.addToPlot(this, plot);
229 public void toPNG() throws java.io.IOException {
230 if (axes.size() > 1) {