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