38f54fe04fa7f9905327000de0f2a47721669ba0
[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 red = new Color(194,31,31);
76         static final private Color green = new Color(31,194,31);
77         static final private Color blue = new Color(31,31,194);
78
79         MicroData       data;
80
81         public JPanel panel() {
82                 return panel;
83         }
84
85         private MicroSeries addSeries(int index, String label, String units, Color color) {
86                 MicroSeries             series = new MicroSeries(label, units, color);
87                 XYSeriesCollection      dataset = new XYSeriesCollection(series);
88                 XYItemRenderer          renderer = new XYLineAndShapeRenderer(true, false);
89
90                 renderer.setSeriesPaint(0, color);
91                 renderer.setPlot(plot);
92                 renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator(String.format("{1}s: {2}%s ({0})", units),
93                                                                                 new java.text.DecimalFormat("0.00"),
94                                                                                 new java.text.DecimalFormat("0.00")));
95                 plot.setRangeAxis(index, series.axis);
96                 plot.setDataset(index, dataset);
97                 plot.setRenderer(index, renderer);
98                 plot.mapDatasetToRangeAxis(index, index);
99                 return series;
100         }
101         
102         public void resetData() {
103                 heightSeries.clear();
104                 speedSeries.clear();
105                 accelSeries.clear();
106                 for (int i = 0; i < data.pressures.length; i++) {
107                         double x = data.time(i);
108                         heightSeries.add(x, AltosConvert.height.value(data.height(i)));
109                         speedSeries.add(x, AltosConvert.speed.value(data.speed(i)));
110                         accelSeries.add(x, AltosConvert.accel.value(data.acceleration(i)));
111                 }
112         }
113
114         public void setData (MicroData data) {
115                 this.data = data;
116                 resetData();
117         }
118
119         public void units_changed(boolean imperial_units) {
120                 if (data != null) {
121                         heightSeries.set_units(AltosConvert.height.show_units());
122                         speedSeries.set_units(AltosConvert.speed.show_units());
123                         accelSeries.set_units(AltosConvert.accel.show_units());
124                         resetData();
125                 }
126         }
127
128         public MicroGraph() {
129
130                 xAxis = new NumberAxis("Time (s)");
131                 
132                 xAxis.setAutoRangeIncludesZero(true);
133
134                 plot = new XYPlot();
135                 plot.setDomainAxis(xAxis);
136                 plot.setOrientation(PlotOrientation.VERTICAL);
137                 plot.setDomainPannable(true);
138                 plot.setRangePannable(true);
139
140                 heightSeries = addSeries(0, "Height", AltosConvert.height.show_units(), red);
141                 speedSeries = addSeries(1, "Speed", AltosConvert.speed.show_units(), green);
142                 accelSeries = addSeries(2, "Acceleration", AltosConvert.accel.show_units(), blue);
143
144                 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
145                                        plot, true);
146
147                 ChartUtilities.applyCurrentTheme(chart);
148                 panel = new ChartPanel(chart);
149                 panel.setMouseWheelEnabled(true);
150                 panel.setPreferredSize(new java.awt.Dimension(800, 500));
151
152                 AltosPreferences.register_units_listener(this);
153         }
154 }