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