Merge remote-tracking branch 'origin/master' into micropeak-logging
[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 public class MicroGraph {
38
39         XYPlot          plot;
40         JFreeChart      chart;
41         ChartPanel      panel;
42         NumberAxis      xAxis;
43         XYSeries        heightSeries;
44         XYSeries        speedSeries;
45         XYSeries        accelSeries;
46
47         MicroData       data;
48
49         public JPanel panel() {
50                 return panel;
51         }
52
53         private void addSeries(XYSeries series, int index, String label, String units) {
54                 XYSeriesCollection      dataset = new XYSeriesCollection(series);
55                 NumberAxis              axis = new NumberAxis(String.format("%s (%s)", label, units));
56                 XYItemRenderer          renderer = new XYLineAndShapeRenderer(true, false);
57
58                 renderer.setPlot(plot);
59                 renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator(String.format("{1}s: {2}%s ({0})", units),
60                                                                                 new java.text.DecimalFormat("0.00"),
61                                                                                 new java.text.DecimalFormat("0.00")));
62                 plot.setRangeAxis(index, axis);
63                 plot.setDataset(index, dataset);
64                 plot.setRenderer(index, renderer);
65                 plot.mapDatasetToRangeAxis(index, index);
66         }
67         
68         public void setData (MicroData data) {
69                 heightSeries.clear();
70                 speedSeries.clear();
71                 accelSeries.clear();
72                 for (int i = 0; i < data.pressures.length; i++) {
73                         double x = data.time(i);
74                         heightSeries.add(x, data.height(i));
75                         speedSeries.add(x, data.speed(i));
76                         accelSeries.add(x, data.acceleration(i));
77                 }
78         }
79
80         public MicroGraph(MicroData data) {
81
82                 this.data = data;
83
84                 heightSeries = new XYSeries("Height");
85                 speedSeries = new XYSeries("Speed");
86                 accelSeries = new XYSeries("Acceleration");
87
88                 xAxis = new NumberAxis("Time (s)");
89                 
90                 xAxis.setAutoRangeIncludesZero(true);
91
92                 plot = new XYPlot();
93                 plot.setDomainAxis(xAxis);
94                 plot.setOrientation(PlotOrientation.VERTICAL);
95                 plot.setDomainPannable(true);
96                 plot.setRangePannable(true);
97
98                 addSeries(heightSeries, 0, "Height", "m");
99                 addSeries(speedSeries, 1, "Speed", "m/s");
100                 addSeries(accelSeries, 2, "Acceleration", "m/s²");
101
102                 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
103                                        plot, true);
104
105                 ChartUtilities.applyCurrentTheme(chart);
106                 panel = new ChartPanel(chart);
107                 panel.setMouseWheelEnabled(true);
108                 panel.setPreferredSize(new java.awt.Dimension(800, 500));
109         }
110 }