2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 package org.altusmetrum.micropeak;
21 import java.util.ArrayList;
25 import org.altusmetrum.AltosLib.*;
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.*;
37 public class MicroGraph {
43 XYSeries heightSeries;
49 public JPanel panel() {
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);
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);
68 public void setData (MicroData data) {
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));
80 public MicroGraph(MicroData data) {
84 heightSeries = new XYSeries("Height");
85 speedSeries = new XYSeries("Speed");
86 accelSeries = new XYSeries("Acceleration");
88 xAxis = new NumberAxis("Time (s)");
90 xAxis.setAutoRangeIncludesZero(true);
93 plot.setDomainAxis(xAxis);
94 plot.setOrientation(PlotOrientation.VERTICAL);
95 plot.setDomainPannable(true);
96 plot.setRangePannable(true);
98 addSeries(heightSeries, 0, "Height", "m");
99 addSeries(speedSeries, 1, "Speed", "m/s");
100 addSeries(accelSeries, 2, "Acceleration", "m/s²");
102 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
105 ChartUtilities.applyCurrentTheme(chart);
106 panel = new ChartPanel(chart);
107 panel.setMouseWheelEnabled(true);
108 panel.setPreferredSize(new java.awt.Dimension(800, 500));