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 class MicroSeries extends XYSeries {
44 return String.format("%s (%s)", label, units);
47 void set_units(String units) {
50 axis.setLabel(label());
53 public MicroSeries (String label, String units, Color color) {
59 axis = new NumberAxis(label());
60 axis.setLabelPaint(color);
61 axis.setTickLabelPaint(color);
65 public class MicroGraph implements AltosUnitsListener {
71 MicroSeries heightSeries;
72 MicroSeries speedSeries;
73 MicroSeries accelSeries;
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);
81 public JPanel panel() {
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);
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);
102 public void resetData() {
103 heightSeries.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)));
114 public void setName (String name) {
115 chart.setTitle(name);
118 public void setData (MicroData data) {
120 chart.setTitle(data.name);
124 public void units_changed(boolean imperial_units) {
126 heightSeries.set_units(AltosConvert.height.show_units());
127 speedSeries.set_units(AltosConvert.speed.show_units());
128 accelSeries.set_units(AltosConvert.accel.show_units());
133 public MicroGraph() {
135 xAxis = new NumberAxis("Time (s)");
137 xAxis.setAutoRangeIncludesZero(true);
140 plot.setDomainAxis(xAxis);
141 plot.setOrientation(PlotOrientation.VERTICAL);
142 plot.setDomainPannable(true);
143 plot.setRangePannable(true);
145 heightSeries = addSeries(0, "Height", AltosConvert.height.show_units(), red);
146 speedSeries = addSeries(1, "Speed", AltosConvert.speed.show_units(), green);
147 accelSeries = addSeries(2, "Acceleration", AltosConvert.accel.show_units(), blue);
149 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
152 ChartUtilities.applyCurrentTheme(chart);
153 panel = new ChartPanel(chart);
154 panel.setMouseWheelEnabled(true);
155 panel.setPreferredSize(new java.awt.Dimension(800, 500));
157 AltosPreferences.register_units_listener(this);