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 MicroGraph(MicroData data) {
72 heightSeries = new XYSeries("Height");
73 speedSeries = new XYSeries("Speed");
74 accelSeries = new XYSeries("Acceleration");
76 for (int i = 0; i < data.pressures.length; i++) {
77 double x = data.time(i);
78 heightSeries.add(x, data.height(i));
79 speedSeries.add(x, data.speed(i));
80 accelSeries.add(x, data.acceleration(i));
83 xAxis = new NumberAxis("Time (s)");
85 xAxis.setAutoRangeIncludesZero(true);
88 plot.setDomainAxis(xAxis);
89 plot.setOrientation(PlotOrientation.VERTICAL);
90 plot.setDomainPannable(true);
91 plot.setRangePannable(true);
93 addSeries(heightSeries, 0, "Height", "m");
94 addSeries(speedSeries, 1, "Speed", "m/s");
95 addSeries(accelSeries, 2, "Acceleration", "m/s²");
97 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
100 ChartUtilities.applyCurrentTheme(chart);
101 panel = new ChartPanel(chart);
102 panel.setMouseWheelEnabled(true);
103 panel.setPreferredSize(new java.awt.Dimension(800, 500));