altoslib: Check for valid pad alt before computing GPS height series
[fw/altos] / altosuilib / AltosUIGraph.java
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_11;
20
21 import java.io.*;
22 import java.util.ArrayList;
23
24 import java.awt.*;
25 import javax.swing.*;
26 import org.altusmetrum.altoslib_11.*;
27
28 import org.jfree.ui.*;
29 import org.jfree.chart.*;
30 import org.jfree.chart.plot.*;
31 import org.jfree.chart.axis.*;
32 import org.jfree.chart.renderer.*;
33 import org.jfree.chart.renderer.xy.*;
34 import org.jfree.chart.labels.*;
35 import org.jfree.data.xy.*;
36 import org.jfree.data.*;
37
38 public class AltosUIGraph implements AltosUnitsListener {
39
40         XYPlot                          plot;
41         JFreeChart                      chart;
42         public ChartPanel               panel;
43         NumberAxis                      xAxis;
44         AltosUIEnable                   enable;
45         ArrayList<AltosUIGrapher>       graphers;
46         AltosUIDataSet                  dataSet;
47         int                             axis_index;
48         int                             series_index;
49
50         static final private Color gridline_color = new Color(0, 0, 0);
51         static final private Color border_color = new Color(255, 255, 255);
52         static final private Color background_color = new Color(255, 255, 255);
53
54         public JPanel panel() {
55                 return panel;
56         }
57
58         public AltosUIAxis newAxis(String label, AltosUnits units, Color color, int flags) {
59                 AltosUIAxis axis = new AltosUIAxis(label, units, color, axis_index++, flags);
60                 plot.setRangeAxis(axis.index, axis);
61                 return axis;
62         }
63
64         public AltosUIAxis newAxis(String label, AltosUnits units, Color color) {
65                 return newAxis(label, units, color, AltosUIAxis.axis_default);
66         }
67
68         public void addSeries(String label, int fetch, AltosUnits units, Color color,
69                               boolean enabled, AltosUIAxis axis) {
70                 AltosUISeries           series = new AltosUISeries(label, fetch, units, color, enabled, axis);
71                 XYSeriesCollection      dataset = new XYSeriesCollection(series);
72
73                 series.renderer.setPlot(plot);
74                 plot.setDataset(series_index, dataset);
75                 plot.setRenderer(series_index, series.renderer);
76                 plot.mapDatasetToRangeAxis(series_index, axis.index);
77                 if (enable != null)
78                         enable.add(label, series, enabled);
79                 this.graphers.add(series);
80                 series_index++;
81         }
82
83         public void addSeries(String label, int fetch, AltosUnits units, Color color) {
84                 addSeries(label, fetch, units, color, true, newAxis(label, units, color));
85         }
86
87         public void addMarker(String label, int fetch, Color color) {
88                 AltosUIMarker           marker = new AltosUIMarker(fetch, color, plot);
89                 this.graphers.add(marker);
90         }
91
92         public void resetData() {
93                 for (AltosUIGrapher g : graphers) {
94                         g.clear();
95                         g.setNotify(false);
96                 }
97                 if (dataSet != null) {
98                         for (AltosUIDataPoint dataPoint : dataSet.dataPoints())
99                                 for (AltosUIGrapher g : graphers)
100                                         g.add(dataPoint);
101                 }
102                 for (AltosUIGrapher g : graphers) {
103                         g.setNotify(true);
104                         g.fireSeriesChanged();
105                 }
106         }
107
108         public void units_changed(boolean imperial_units) {
109                 for (AltosUIGrapher g : graphers)
110                         g.set_units();
111                 resetData();
112         }
113
114         public void setName (String name) {
115                 chart.setTitle(name);
116         }
117
118         public void setDataSet (AltosUIDataSet dataSet) {
119                 this.dataSet = dataSet;
120                 resetData();
121                 if (dataSet != null)
122                         setName(dataSet.name());
123         }
124
125         public AltosUIGraph(AltosUIEnable enable) {
126
127                 this.enable = enable;
128                 this.graphers = new ArrayList<AltosUIGrapher>();
129                 this.series_index = 0;
130                 this.axis_index = 0;
131
132                 xAxis = new NumberAxis("Time (s)");
133
134                 xAxis.setAutoRangeIncludesZero(true);
135
136                 plot = new XYPlot();
137                 plot.setDomainAxis(xAxis);
138                 plot.setOrientation(PlotOrientation.VERTICAL);
139                 plot.setDomainPannable(true);
140                 plot.setRangePannable(true);
141
142                 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
143                                        plot, true);
144
145                 ChartUtilities.applyCurrentTheme(chart);
146
147                 plot.setDomainGridlinePaint(gridline_color);
148                 plot.setRangeGridlinePaint(gridline_color);
149                 plot.setBackgroundPaint(background_color);
150                 plot.setBackgroundAlpha((float) 1);
151
152                 chart.setBackgroundPaint(background_color);
153                 chart.setBorderPaint(border_color);
154                 panel = new ChartPanel(chart);
155                 panel.setMouseWheelEnabled(true);
156                 panel.setPreferredSize(new java.awt.Dimension(800, 500));
157
158                 AltosPreferences.register_units_listener(this);
159         }
160 }