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 {
42 XYItemRenderer renderer;
44 void set_units(String units) {
47 axis.setLabel(String.format("%s (%s)", label, units));
49 StandardXYToolTipGenerator ttg;
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);
57 public MicroSeries (String label, String units, Color color) {
63 axis = new NumberAxis();
64 axis.setLabelPaint(color);
65 axis.setTickLabelPaint(color);
67 renderer = new XYLineAndShapeRenderer(true, false);
68 renderer.setSeriesPaint(0, color);
73 public class MicroGraph implements AltosUnitsListener {
79 MicroSeries heightSeries;
80 MicroSeries speedSeries;
81 MicroSeries accelSeries;
83 static final private Color height_color = new Color(194,31,31);
84 static final private Color speed_color = new Color(31,194,31);
85 static final private Color accel_color = new Color(31,31,194);
86 static final private Color gridline_color = new Color(0, 0, 0);
87 static final private Color border_color = new Color(255, 255, 255);
88 static final private Color background_color = new Color(255, 255, 255);
92 public JPanel panel() {
96 private MicroSeries addSeries(int index, String label, String units, Color color) {
97 MicroSeries series = new MicroSeries(label, units, color);
98 XYSeriesCollection dataset = new XYSeriesCollection(series);
100 series.renderer.setPlot(plot);
101 plot.setRangeAxis(index, series.axis);
102 plot.setDataset(index, dataset);
103 plot.setRenderer(index, series.renderer);
104 plot.mapDatasetToRangeAxis(index, index);
108 public void resetData() {
109 heightSeries.clear();
113 for (MicroDataPoint point : data.points()) {
114 heightSeries.add(point.time, AltosConvert.height.value(point.height));
115 speedSeries.add(point.time, AltosConvert.speed.value(point.speed));
116 accelSeries.add(point.time, AltosConvert.accel.value(point.accel));
121 public void setName (String name) {
122 chart.setTitle(name);
125 public void setData (MicroData data) {
132 public void units_changed(boolean imperial_units) {
133 heightSeries.set_units(AltosConvert.height.show_units());
134 speedSeries.set_units(AltosConvert.speed.show_units());
135 accelSeries.set_units(AltosConvert.accel.show_units());
139 public MicroGraph() {
141 xAxis = new NumberAxis("Time (s)");
143 xAxis.setAutoRangeIncludesZero(true);
146 plot.setDomainAxis(xAxis);
147 plot.setOrientation(PlotOrientation.VERTICAL);
148 plot.setDomainPannable(true);
149 plot.setRangePannable(true);
151 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
154 ChartUtilities.applyCurrentTheme(chart);
156 heightSeries = addSeries(0, "Height", AltosConvert.height.show_units(), height_color);
157 speedSeries = addSeries(1, "Speed", AltosConvert.speed.show_units(), speed_color);
158 accelSeries = addSeries(2, "Acceleration", AltosConvert.accel.show_units(), accel_color);
160 plot.setDomainGridlinePaint(gridline_color);
161 plot.setRangeGridlinePaint(gridline_color);
162 plot.setBackgroundPaint(background_color);
163 plot.setBackgroundAlpha((float) 1);
165 chart.setBackgroundPaint(background_color);
166 chart.setBorderPaint(border_color);
167 panel = new ChartPanel(chart);
168 panel.setMouseWheelEnabled(true);
169 panel.setPreferredSize(new java.awt.Dimension(800, 500));
171 AltosPreferences.register_units_listener(this);