Altosui: Add flight statistics tab to graph window
[fw/altos] / altosui / AltosGraphTime.java
1
2 // Copyright (c) 2010 Anthony Towns
3 // GPL v2 or later
4
5 package altosui;
6
7 import java.awt.Color;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10
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;
26
27 class AltosGraphTime extends AltosGraph {
28     static interface Element {
29         void attachGraph(AltosGraphTime g);
30         void gotTimeData(double time, AltosDataPoint d);
31         void addToPlot(AltosGraphTime g, XYPlot plot);
32     }
33
34     static class TimeAxis implements Element {
35         private int axis;
36         private Color color;
37         private String label;
38         private AxisLocation locn;
39         private double min_y = Double.NaN;
40
41         public TimeAxis(int axis, String label, Color color, AxisLocation locn)
42         {
43             this.axis = axis;
44             this.color = color;
45             this.label = label;
46             this.locn = locn;
47         }
48
49         public void setLowerBound(double min_y) {
50             this.min_y = min_y;
51         }
52
53         public void attachGraph(AltosGraphTime g) { return; }
54         public void gotTimeData(double time, AltosDataPoint d) { return; }
55
56         public void addToPlot(AltosGraphTime g, XYPlot plot) {
57             NumberAxis numAxis = new NumberAxis(label);
58             if (!Double.isNaN(min_y))
59                 numAxis.setLowerBound(min_y);
60             plot.setRangeAxis(axis, numAxis);
61             plot.setRangeAxisLocation(axis, locn);
62             numAxis.setLabelPaint(color);
63             numAxis.setTickLabelPaint(color);
64             numAxis.setAutoRangeIncludesZero(false);
65         }
66     }
67
68     abstract static class TimeSeries implements Element {
69         protected XYSeries series;
70         private String axisName;
71         private Color color;
72
73         public TimeSeries(String axisName, String label, Color color) {
74             this.series = new XYSeries(label);
75             this.axisName = axisName;
76             this.color = color;
77         }
78
79         public void attachGraph(AltosGraphTime g) {
80             g.setAxis(this, axisName, color);
81         }
82         abstract public void gotTimeData(double time, AltosDataPoint d);
83
84         public void addToPlot(AltosGraphTime g, XYPlot plot) {
85             XYSeriesCollection dataset = new XYSeriesCollection();
86             dataset.addSeries(this.series);
87
88             XYItemRenderer renderer = new StandardXYItemRenderer();
89             renderer.setSeriesPaint(0, color);
90
91             int dataNum = g.getDataNum(this);
92             int axisNum = g.getAxisNum(this);
93
94             plot.setDataset(dataNum, dataset);
95             plot.mapDatasetToRangeAxis(dataNum, axisNum);
96             plot.setRenderer(dataNum, renderer);
97         }
98     }
99
100     static class StateMarker implements Element {
101         private double val = Double.NaN;
102         private String name;
103         private int state;
104
105         StateMarker(int state, String name) {
106             this.state = state;
107             this.name = name;
108         }
109
110         public void attachGraph(AltosGraphTime g) { return; }
111         public void gotTimeData(double time, AltosDataPoint d) {
112             if (Double.isNaN(val) || time < val) {
113                 if (d.state() == state) {
114                     val = time;
115                 }
116             }
117         }
118
119         public void addToPlot(AltosGraphTime g, XYPlot plot) {
120             if (Double.isNaN(val))
121                 return;
122
123             ValueMarker m = new ValueMarker(val);
124             m.setLabel(name);
125             m.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
126             m.setLabelTextAnchor(TextAnchor.TOP_LEFT);
127             plot.addDomainMarker(m);
128         }
129     }
130
131     private String callsign = null;
132     private Integer serial = null;
133     private Integer flight = null; 
134
135     private ArrayList<Element> elements;
136     private HashMap<String,Integer> axes;
137     private HashMap<Element,Integer> datasets;
138     private ArrayList<Integer> datasetAxis;
139
140     public AltosGraphTime(String title) {
141         this.filename = title.toLowerCase().replaceAll("[^a-z0-9]","_")+".png";
142         this.title = title;
143         this.elements = new ArrayList<Element>();
144         this.axes = new HashMap<String,Integer>();
145         this.datasets = new HashMap<Element,Integer>();
146         this.datasetAxis = new ArrayList<Integer>();
147     }
148
149     public AltosGraphTime addElement(Element e) {
150         e.attachGraph(this);
151         elements.add(e);
152         return this;
153     }
154
155     public void setAxis(Element ds, String axisName, Color color) {
156         Integer axisNum = axes.get(axisName);
157         int dsNum = datasetAxis.size();
158         if (axisNum == null) {
159             axisNum = newAxis(axisName, color);
160         }
161         datasets.put(ds, dsNum);
162         datasetAxis.add(axisNum);
163     }
164
165     public int getAxisNum(Element ds) {
166         return datasetAxis.get( datasets.get(ds) ).intValue();
167     }
168     public int getDataNum(Element ds) {
169         return datasets.get(ds).intValue();
170     }
171
172     private Integer newAxis(String name, Color color) {
173         int cnt = axes.size();
174         AxisLocation locn = AxisLocation.BOTTOM_OR_LEFT;
175         if (cnt > 0) {
176             locn = AxisLocation.TOP_OR_RIGHT;
177         }
178         Integer res = new Integer(cnt);
179         axes.put(name, res);
180         this.addElement(new TimeAxis(cnt, name, color, locn));
181         return res;
182     }
183
184     public void addData(AltosDataPoint d) {
185         double time = d.time();
186         for (Element e : elements) {
187             e.gotTimeData(time, d);
188         }
189         if (callsign == null) callsign = d.callsign();
190         if (serial == null) serial = new Integer(d.serial());
191         if (flight == null) flight = new Integer(d.flight());
192     }
193
194     public JFreeChart createChart() {
195         NumberAxis xAxis = new NumberAxis("Time (s)");
196         xAxis.setAutoRangeIncludesZero(false);
197         XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
198         XYPlot plot = new XYPlot();
199         plot.setDomainAxis(xAxis);
200         plot.setRenderer(renderer);
201         plot.setOrientation(PlotOrientation.VERTICAL);
202
203         if (serial != null && flight != null) {
204             title = serial + "/" + flight + ": " + title;
205         }
206         if (callsign != null) {
207             title = callsign + " - " + title;
208         }
209
210         renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
211         JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
212                                 plot, true);
213         ChartUtilities.applyCurrentTheme(chart);
214
215         plot.setDomainPannable(true);
216         plot.setRangePannable(true);
217    
218         for (Element e : elements) {
219             e.addToPlot(this, plot);
220         }
221
222         return chart;
223     }
224
225     public void toPNG() throws java.io.IOException {
226         if (axes.size() > 1) {
227             toPNG(800, 500);
228         } else {
229             toPNG(300, 500);
230         }
231     }
232 }