2 // Copyright (c) 2010 Anthony Towns
9 import java.util.concurrent.*;
12 import java.awt.Color;
13 import java.util.ArrayList;
14 import java.util.HashMap;
16 import org.jfree.chart.ChartUtilities;
17 import org.jfree.chart.JFreeChart;
18 import org.jfree.chart.axis.AxisLocation;
19 import org.jfree.chart.axis.NumberAxis;
20 import org.jfree.chart.labels.StandardXYToolTipGenerator;
21 import org.jfree.chart.plot.PlotOrientation;
22 import org.jfree.chart.plot.XYPlot;
23 import org.jfree.chart.plot.ValueMarker;
24 import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
25 import org.jfree.chart.renderer.xy.XYItemRenderer;
26 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
27 import org.jfree.data.xy.XYSeries;
28 import org.jfree.data.xy.XYSeriesCollection;
29 import org.jfree.ui.RectangleAnchor;
30 import org.jfree.ui.TextAnchor;
32 class AltosGraphTime extends AltosGraph {
33 static interface Element {
34 void attachGraph(AltosGraphTime g);
35 void gotTimeData(double time, AltosDataPoint d);
36 void addToPlot(AltosGraphTime g, XYPlot plot);
39 static class TimeAxis implements Element {
43 private AxisLocation locn;
44 private double min_y = Double.NaN;
46 public TimeAxis(int axis, String label, Color color, AxisLocation locn)
54 public void setLowerBound(double min_y) {
58 public void attachGraph(AltosGraphTime g) { return; }
59 public void gotTimeData(double time, AltosDataPoint d) { return; }
61 public void addToPlot(AltosGraphTime g, XYPlot plot) {
62 NumberAxis numAxis = new NumberAxis(label);
63 if (!Double.isNaN(min_y))
64 numAxis.setLowerBound(min_y);
65 plot.setRangeAxis(axis, numAxis);
66 plot.setRangeAxisLocation(axis, locn);
67 numAxis.setLabelPaint(color);
68 numAxis.setTickLabelPaint(color);
69 numAxis.setAutoRangeIncludesZero(false);
73 abstract static class TimeSeries implements Element {
74 protected XYSeries series;
75 private String axisName;
78 public TimeSeries(String axisName, String label, Color color) {
79 this.series = new XYSeries(label);
80 this.axisName = axisName;
84 public void attachGraph(AltosGraphTime g) {
85 g.setAxis(this, axisName, color);
87 abstract public void gotTimeData(double time, AltosDataPoint d);
89 public void addToPlot(AltosGraphTime g, XYPlot plot) {
90 XYSeriesCollection dataset = new XYSeriesCollection();
91 dataset.addSeries(this.series);
93 XYItemRenderer renderer = new StandardXYItemRenderer();
94 renderer.setSeriesPaint(0, color);
96 int dataNum = g.getDataNum(this);
97 int axisNum = g.getAxisNum(this);
99 plot.setDataset(dataNum, dataset);
100 plot.mapDatasetToRangeAxis(dataNum, axisNum);
101 plot.setRenderer(dataNum, renderer);
105 static class StateMarker implements Element {
106 private LinkedList<Double> times = new LinkedList<Double>();
109 private int prev_state = Altos.ao_flight_startup;
111 StateMarker(int state, String name) {
116 public void attachGraph(AltosGraphTime g) { return; }
117 public void gotTimeData(double time, AltosDataPoint d) {
118 if (prev_state != state && d.state() == state)
120 prev_state = d.state();
123 public void addToPlot(AltosGraphTime g, XYPlot plot) {
124 for (double time : times) {
125 ValueMarker m = new ValueMarker(time);
127 m.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
128 m.setLabelTextAnchor(TextAnchor.TOP_LEFT);
129 plot.addDomainMarker(m);
134 private String callsign = null;
135 private Integer serial = null;
136 private Integer flight = null;
138 private ArrayList<Element> elements;
139 private HashMap<String,Integer> axes;
140 private HashMap<Element,Integer> datasets;
141 private ArrayList<Integer> datasetAxis;
143 public AltosGraphTime(String title) {
144 this.filename = title.toLowerCase().replaceAll("[^a-z0-9]","_")+".png";
146 this.elements = new ArrayList<Element>();
147 this.axes = new HashMap<String,Integer>();
148 this.datasets = new HashMap<Element,Integer>();
149 this.datasetAxis = new ArrayList<Integer>();
152 public AltosGraphTime addElement(Element e) {
158 public void setAxis(Element ds, String axisName, Color color) {
159 Integer axisNum = axes.get(axisName);
160 int dsNum = datasetAxis.size();
161 if (axisNum == null) {
162 axisNum = newAxis(axisName, color);
164 datasets.put(ds, dsNum);
165 datasetAxis.add(axisNum);
168 public int getAxisNum(Element ds) {
169 return datasetAxis.get( datasets.get(ds) ).intValue();
171 public int getDataNum(Element ds) {
172 return datasets.get(ds).intValue();
175 private Integer newAxis(String name, Color color) {
176 int cnt = axes.size();
177 AxisLocation locn = AxisLocation.BOTTOM_OR_LEFT;
179 locn = AxisLocation.TOP_OR_RIGHT;
181 Integer res = new Integer(cnt);
183 this.addElement(new TimeAxis(cnt, name, color, locn));
187 public void addData(AltosDataPoint d) {
188 double time = d.time();
189 for (Element e : elements) {
190 e.gotTimeData(time, d);
192 if (callsign == null) callsign = d.callsign();
193 if (serial == null) serial = new Integer(d.serial());
194 if (flight == null) flight = new Integer(d.flight());
197 public JFreeChart createChart() {
198 NumberAxis xAxis = new NumberAxis("Time (s)");
199 xAxis.setAutoRangeIncludesZero(false);
200 XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
201 XYPlot plot = new XYPlot();
202 plot.setDomainAxis(xAxis);
203 plot.setRenderer(renderer);
204 plot.setOrientation(PlotOrientation.VERTICAL);
206 if (serial != null && flight != null) {
207 title = serial + "/" + flight + ": " + title;
209 if (callsign != null) {
210 title = callsign + " - " + title;
213 renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
214 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
216 ChartUtilities.applyCurrentTheme(chart);
218 plot.setDomainPannable(true);
219 plot.setRangePannable(true);
221 for (Element e : elements) {
222 e.addToPlot(this, plot);
228 public void toPNG() throws java.io.IOException {
229 if (axes.size() > 1) {