AltosSiteMap: add targeting circles around landing site
[fw/altos] / ao-tools / 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 import altosui.AltosDataPoint;
28 import altosui.AltosGraph;
29
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);
35     }
36
37     static class TimeAxis implements Element {
38         private int axis;
39         private Color color;
40         private String label;
41         private AxisLocation locn;
42         private double min_y = Double.NaN;
43
44         public TimeAxis(int axis, String label, Color color, AxisLocation locn)
45         {
46             this.axis = axis;
47             this.color = color;
48             this.label = label;
49             this.locn = locn;
50         }
51
52         public void setLowerBound(double min_y) {
53             this.min_y = min_y;
54         }
55
56         public void attachGraph(AltosGraphTime g) { return; }
57         public void gotTimeData(double time, AltosDataPoint d) { return; }
58
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);
68         }
69     }
70
71     abstract static class TimeSeries implements Element {
72         protected XYSeries series;
73         private String axisName;
74         private Color color;
75
76         public TimeSeries(String axisName, String label, Color color) {
77             this.series = new XYSeries(label);
78             this.axisName = axisName;
79             this.color = color;
80         }
81
82         public void attachGraph(AltosGraphTime g) {
83             g.setAxis(this, axisName, color);
84         }
85         abstract public void gotTimeData(double time, AltosDataPoint d);
86
87         public void addToPlot(AltosGraphTime g, XYPlot plot) {
88             XYSeriesCollection dataset = new XYSeriesCollection();
89             dataset.addSeries(this.series);
90
91             XYItemRenderer renderer = new StandardXYItemRenderer();
92             renderer.setSeriesPaint(0, color);
93
94             int dataNum = g.getDataNum(this);
95             int axisNum = g.getAxisNum(this);
96
97             plot.setDataset(dataNum, dataset);
98             plot.mapDatasetToRangeAxis(dataNum, axisNum);
99             plot.setRenderer(dataNum, renderer);
100         }
101     }
102
103     static class StateMarker implements Element {
104         private double val = Double.NaN;
105         private String name;
106         private int state;
107
108         StateMarker(int state, String name) {
109             this.state = state;
110             this.name = name;
111         }
112
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) {
117                     val = time;
118                 }
119             }
120         }
121
122         public void addToPlot(AltosGraphTime g, XYPlot plot) {
123             if (Double.isNaN(val))
124                 return;
125
126             ValueMarker m = new ValueMarker(val);
127             m.setLabel(name);
128             m.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
129             m.setLabelTextAnchor(TextAnchor.TOP_LEFT);
130             plot.addDomainMarker(m);
131         }
132     }
133
134     private String callsign = null;
135     private Integer serial = null;
136     private Integer flight = null; 
137
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;
143
144     public AltosGraphTime(String title) {
145         this.filename = title.toLowerCase().replaceAll("[^a-z0-9]","_")+".png";
146         this.title = title;
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>();
151     }
152
153     public AltosGraphTime addElement(Element e) {
154         e.attachGraph(this);
155         elements.add(e);
156         return this;
157     }
158
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);
164         }
165         datasets.put(ds, dsNum);
166         datasetAxis.add(axisNum);
167     }
168
169     public int getAxisNum(Element ds) {
170         return datasetAxis.get( datasets.get(ds) ).intValue();
171     }
172     public int getDataNum(Element ds) {
173         return datasets.get(ds).intValue();
174     }
175
176     private Integer newAxis(String name, Color color) {
177         int cnt = axes.size();
178         AxisLocation locn = AxisLocation.BOTTOM_OR_LEFT;
179         if (cnt > 0) {
180             locn = AxisLocation.TOP_OR_RIGHT;
181         }
182         Integer res = new Integer(cnt);
183         axes.put(name, res);
184         this.addElement(new TimeAxis(cnt, name, color, locn));
185         return res;
186     }
187
188     public void addData(AltosDataPoint d) {
189         double time = d.time();
190         for (Element e : elements) {
191             e.gotTimeData(time, d);
192         }
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());
196     }
197
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);
206
207         if (serial != null && flight != null) {
208             title = serial + "/" + flight + ": " + title;
209         }
210         if (callsign != null) {
211             title = callsign + " - " + title;
212         }
213
214         renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
215         JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
216                                 plot, true);
217         ChartUtilities.applyCurrentTheme(chart);
218
219         plot.setDomainPannable(true);
220         plot.setRangePannable(true);
221    
222         for (Element e : elements) {
223             e.addToPlot(this, plot);
224         }
225
226         return chart;
227     }
228
229     public void toPNG() throws java.io.IOException {
230         if (axes.size() > 1) {
231             toPNG(800, 500);
232         } else {
233             toPNG(300, 500);
234         }
235     }
236 }