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.altosuilib_7;
21 import java.util.ArrayList;
25 import org.altusmetrum.altoslib_7.*;
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 AltosUIGraph implements AltosUnitsListener {
41 public ChartPanel panel;
44 ArrayList<AltosUIGrapher> graphers;
45 AltosUIDataSet dataSet;
49 static final private Color gridline_color = new Color(0, 0, 0);
50 static final private Color border_color = new Color(255, 255, 255);
51 static final private Color background_color = new Color(255, 255, 255);
53 public JPanel panel() {
57 public AltosUIAxis newAxis(String label, AltosUnits units, Color color, int flags) {
58 AltosUIAxis axis = new AltosUIAxis(label, units, color, axis_index++, flags);
59 plot.setRangeAxis(axis.index, axis);
63 public AltosUIAxis newAxis(String label, AltosUnits units, Color color) {
64 return newAxis(label, units, color, AltosUIAxis.axis_default);
67 public void addSeries(String label, int fetch, AltosUnits units, Color color,
68 boolean enabled, AltosUIAxis axis) {
69 AltosUISeries series = new AltosUISeries(label, fetch, units, color, enabled, axis);
70 XYSeriesCollection dataset = new XYSeriesCollection(series);
72 series.renderer.setPlot(plot);
73 plot.setDataset(series_index, dataset);
74 plot.setRenderer(series_index, series.renderer);
75 plot.mapDatasetToRangeAxis(series_index, axis.index);
77 enable.add(label, series, enabled);
78 this.graphers.add(series);
82 public void addSeries(String label, int fetch, AltosUnits units, Color color) {
83 addSeries(label, fetch, units, color, true, newAxis(label, units, color));
86 public void addMarker(String label, int fetch, Color color) {
87 AltosUIMarker marker = new AltosUIMarker(fetch, color, plot);
88 this.graphers.add(marker);
91 public void resetData() {
92 for (AltosUIGrapher g : graphers) {
96 if (dataSet != null) {
97 for (AltosUIDataPoint dataPoint : dataSet.dataPoints())
98 for (AltosUIGrapher g : graphers)
101 for (AltosUIGrapher g : graphers) {
103 g.fireSeriesChanged();
107 public void units_changed(boolean imperial_units) {
108 for (AltosUIGrapher g : graphers)
113 public void setName (String name) {
114 chart.setTitle(name);
117 public void setDataSet (AltosUIDataSet dataSet) {
118 this.dataSet = dataSet;
121 setName(dataSet.name());
124 public AltosUIGraph(AltosUIEnable enable) {
126 this.enable = enable;
127 this.graphers = new ArrayList<AltosUIGrapher>();
128 this.series_index = 0;
131 xAxis = new NumberAxis("Time (s)");
133 xAxis.setAutoRangeIncludesZero(true);
136 plot.setDomainAxis(xAxis);
137 plot.setOrientation(PlotOrientation.VERTICAL);
138 plot.setDomainPannable(true);
139 plot.setRangePannable(true);
141 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
144 ChartUtilities.applyCurrentTheme(chart);
146 plot.setDomainGridlinePaint(gridline_color);
147 plot.setRangeGridlinePaint(gridline_color);
148 plot.setBackgroundPaint(background_color);
149 plot.setBackgroundAlpha((float) 1);
151 chart.setBackgroundPaint(background_color);
152 chart.setBorderPaint(border_color);
153 panel = new ChartPanel(chart);
154 panel.setMouseWheelEnabled(true);
155 panel.setPreferredSize(new java.awt.Dimension(800, 500));
157 AltosPreferences.register_units_listener(this);