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