Build installable versions of MicroPeak GUI
[fw/altos] / micropeak / MicroGraph.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.micropeak;
19
20 import java.io.*;
21 import java.util.ArrayList;
22
23 import java.awt.*;
24 import javax.swing.*;
25 import org.altusmetrum.AltosLib.*;
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 class MicroSeries extends XYSeries {
38         NumberAxis      axis;
39         String          label;
40         String          units;
41         Color           color;
42         
43         String label() {
44                 return String.format("%s (%s)", label, units);
45         }
46
47         void set_units(String units) {
48                 this.units = units;
49
50                 axis.setLabel(label());
51         }
52
53         public MicroSeries (String label, String units, Color color) {
54                 super(label);
55                 this.label = label;
56                 this.units = units;
57                 this.color = color;
58
59                 axis = new NumberAxis(label());
60                 axis.setLabelPaint(color);
61                 axis.setTickLabelPaint(color);
62         }
63 }
64
65 public class MicroGraph implements AltosUnitsListener {
66
67         XYPlot          plot;
68         JFreeChart      chart;
69         ChartPanel      panel;
70         NumberAxis      xAxis;
71         MicroSeries     heightSeries;
72         MicroSeries     speedSeries;
73         MicroSeries     accelSeries;
74
75         static final private Color height_color = new Color(194,31,31);
76         static final private Color speed_color = new Color(31,194,31);
77         static final private Color accel_color = new Color(31,31,194);
78         static final private Color gridline_color = new Color(0, 0, 0);
79         static final private Color border_color = new Color(255, 255, 255);
80         static final private Color background_color = new Color(255, 255, 255);
81
82         MicroData       data;
83
84         public JPanel panel() {
85                 return panel;
86         }
87
88         private MicroSeries addSeries(int index, String label, String units, Color color) {
89                 MicroSeries             series = new MicroSeries(label, units, color);
90                 XYSeriesCollection      dataset = new XYSeriesCollection(series);
91                 XYItemRenderer          renderer = new XYLineAndShapeRenderer(true, false);
92
93                 renderer.setSeriesPaint(0, color);
94                 renderer.setPlot(plot);
95                 renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator(String.format("{1}s: {2}%s ({0})", units),
96                                                                                 new java.text.DecimalFormat("0.00"),
97                                                                                 new java.text.DecimalFormat("0.00")));
98                 plot.setRangeAxis(index, series.axis);
99                 plot.setDataset(index, dataset);
100                 plot.setRenderer(index, renderer);
101                 plot.mapDatasetToRangeAxis(index, index);
102                 return series;
103         }
104         
105         public void resetData() {
106                 heightSeries.clear();
107                 speedSeries.clear();
108                 accelSeries.clear();
109                 for (int i = 0; i < data.pressures.length; i++) {
110                         double x = data.time(i);
111                         heightSeries.add(x, AltosConvert.height.value(data.height(i)));
112                         speedSeries.add(x, AltosConvert.speed.value(data.speed(i)));
113                         accelSeries.add(x, AltosConvert.accel.value(data.acceleration(i)));
114                 }
115         }
116
117         public void setName (String name) {
118                 chart.setTitle(name);
119         }
120
121         public void setData (MicroData data) {
122                 this.data = data;
123                 chart.setTitle(data.name);
124                 resetData();
125         }
126
127         public void units_changed(boolean imperial_units) {
128                 if (data != null) {
129                         heightSeries.set_units(AltosConvert.height.show_units());
130                         speedSeries.set_units(AltosConvert.speed.show_units());
131                         accelSeries.set_units(AltosConvert.accel.show_units());
132                         resetData();
133                 }
134         }
135
136         public MicroGraph() {
137
138                 xAxis = new NumberAxis("Time (s)");
139                 
140                 xAxis.setAutoRangeIncludesZero(true);
141
142                 plot = new XYPlot();
143                 plot.setDomainAxis(xAxis);
144                 plot.setOrientation(PlotOrientation.VERTICAL);
145                 plot.setDomainPannable(true);
146                 plot.setRangePannable(true);
147
148                 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
149                                        plot, true);
150
151                 ChartUtilities.applyCurrentTheme(chart);
152
153                 heightSeries = addSeries(0, "Height", AltosConvert.height.show_units(), height_color);
154                 speedSeries = addSeries(1, "Speed", AltosConvert.speed.show_units(), speed_color);
155                 accelSeries = addSeries(2, "Acceleration", AltosConvert.accel.show_units(), accel_color);
156
157                 plot.setDomainGridlinePaint(gridline_color);
158                 plot.setRangeGridlinePaint(gridline_color);
159                 plot.setBackgroundPaint(background_color);
160                 plot.setBackgroundAlpha((float) 1);
161
162                 chart.setBackgroundPaint(background_color);
163                 chart.setBorderPaint(border_color);
164                 panel = new ChartPanel(chart);
165                 panel.setMouseWheelEnabled(true);
166                 panel.setPreferredSize(new java.awt.Dimension(800, 500));
167
168                 AltosPreferences.register_units_listener(this);
169         }
170 }