Merge branch 'master' into micropeak-logging
[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.util.*;
8 import java.awt.Color;
9 import java.util.ArrayList;
10 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;
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 String axisUnits;
72         private Color color;
73
74         public TimeSeries(String axisName, String axisUnits, String label, Color color) {
75             this.series = new XYSeries(label);
76             this.axisName = String.format("%s (%s)", axisName, axisUnits);
77             this.axisUnits = axisUnits;
78             this.color = color;
79         }
80
81         public void attachGraph(AltosGraphTime g) {
82             g.setAxis(this, axisName, color);
83         }
84         abstract public void gotTimeData(double time, AltosDataPoint d);
85
86         public void addToPlot(AltosGraphTime g, XYPlot plot) {
87             XYSeriesCollection dataset = new XYSeriesCollection();
88             dataset.addSeries(this.series);
89
90             XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
91             renderer.setSeriesPaint(0, color);
92             StandardXYToolTipGenerator  tool_tip;
93
94             tool_tip = new StandardXYToolTipGenerator(String.format("{1}s: {2}%s ({0})", axisUnits),
95                                                       new java.text.DecimalFormat("0.00"),
96                                                       new java.text.DecimalFormat("0.00"));
97             renderer.setBaseToolTipGenerator(tool_tip);
98
99             int dataNum = g.getDataNum(this);
100             int axisNum = g.getAxisNum(this);
101
102             plot.setDataset(dataNum, dataset);
103             plot.mapDatasetToRangeAxis(dataNum, axisNum);
104             plot.setRenderer(dataNum, renderer);
105         }
106     }
107
108     static class StateMarker implements Element {
109         private LinkedList<Double> times = new LinkedList<Double>();
110         private String name;
111         private int state;
112         private int prev_state = Altos.ao_flight_startup;
113
114         StateMarker(int state, String name) {
115             this.state = state;
116             this.name = name;
117         }
118
119         public void attachGraph(AltosGraphTime g) { return; }
120         public void gotTimeData(double time, AltosDataPoint d) {
121             if (prev_state != state && d.state() == state)
122                 times.add(time);
123             prev_state = d.state();
124         }
125
126         public void addToPlot(AltosGraphTime g, XYPlot plot) {
127             for (double time : times) {
128                 ValueMarker m = new ValueMarker(time);
129                 m.setLabel(name);
130                 m.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
131                 m.setLabelTextAnchor(TextAnchor.TOP_LEFT);
132                 plot.addDomainMarker(m);
133             }
134         }
135     }
136
137     private String callsign = null;
138     private Integer serial = null;
139     private Integer flight = null; 
140
141     private ArrayList<Element> elements;
142     private HashMap<String,Integer> axes;
143     private HashMap<Element,Integer> datasets;
144     private ArrayList<Integer> datasetAxis;
145
146     public AltosGraphTime(String title) {
147         this.filename = title.toLowerCase().replaceAll("[^a-z0-9]","_")+".png";
148         this.title = title;
149         this.elements = new ArrayList<Element>();
150         this.axes = new HashMap<String,Integer>();
151         this.datasets = new HashMap<Element,Integer>();
152         this.datasetAxis = new ArrayList<Integer>();
153     }
154
155     public AltosGraphTime addElement(Element e) {
156         e.attachGraph(this);
157         elements.add(e);
158         return this;
159     }
160
161     public void setAxis(Element ds, String axisName, Color color) {
162         Integer axisNum = axes.get(axisName);
163         int dsNum = datasetAxis.size();
164         if (axisNum == null) {
165             axisNum = newAxis(axisName, color);
166         }
167         datasets.put(ds, dsNum);
168         datasetAxis.add(axisNum);
169     }
170
171     public int getAxisNum(Element ds) {
172         return datasetAxis.get( datasets.get(ds) ).intValue();
173     }
174     public int getDataNum(Element ds) {
175         return datasets.get(ds).intValue();
176     }
177
178     private Integer newAxis(String name, Color color) {
179         int cnt = axes.size();
180         AxisLocation locn = AxisLocation.BOTTOM_OR_LEFT;
181         if (cnt > 0) {
182             locn = AxisLocation.TOP_OR_RIGHT;
183         }
184         Integer res = new Integer(cnt);
185         axes.put(name, res);
186         this.addElement(new TimeAxis(cnt, name, color, locn));
187         return res;
188     }
189
190     public void addData(AltosDataPoint d) {
191         double time = d.time();
192         for (Element e : elements) {
193             e.gotTimeData(time, d);
194         }
195         if (callsign == null) callsign = d.callsign();
196         if (serial == null) serial = new Integer(d.serial());
197         if (flight == null) flight = new Integer(d.flight());
198     }
199
200     public JFreeChart createChart() {
201         NumberAxis xAxis = new NumberAxis("Time (s)");
202         xAxis.setAutoRangeIncludesZero(false);
203         XYPlot plot = new XYPlot();
204         plot.setDomainAxis(xAxis);
205         plot.setOrientation(PlotOrientation.VERTICAL);
206
207         if (serial != null && flight != null) {
208             title = serial + "/" + flight + ": " + title;
209         }
210         if (callsign != null) {
211             title = callsign + " - " + title;
212         }
213
214         JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
215                                 plot, true);
216         ChartUtilities.applyCurrentTheme(chart);
217
218         plot.setDomainPannable(true);
219         plot.setRangePannable(true);
220    
221         for (Element e : elements) {
222             e.addToPlot(this, plot);
223         }
224
225         return chart;
226     }
227
228     public void toPNG() throws java.io.IOException {
229         if (axes.size() > 1) {
230             toPNG(800, 500);
231         } else {
232             toPNG(300, 500);
233         }
234     }
235 }