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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_11;
22 import java.util.ArrayList;
26 import org.altusmetrum.altoslib_11.*;
28 import org.jfree.ui.*;
29 import org.jfree.chart.*;
30 import org.jfree.chart.plot.*;
31 import org.jfree.chart.axis.*;
32 import org.jfree.chart.renderer.*;
33 import org.jfree.chart.renderer.xy.*;
34 import org.jfree.chart.labels.*;
35 import org.jfree.data.xy.*;
36 import org.jfree.data.*;
38 public class AltosUIGraph implements AltosUnitsListener {
42 public ChartPanel panel;
45 ArrayList<AltosUIGrapher> graphers;
46 AltosUIDataSet dataSet;
50 static final private Color gridline_color = new Color(0, 0, 0);
51 static final private Color border_color = new Color(255, 255, 255);
52 static final private Color background_color = new Color(255, 255, 255);
54 public JPanel panel() {
58 public AltosUIAxis newAxis(String label, AltosUnits units, Color color, int flags) {
59 AltosUIAxis axis = new AltosUIAxis(label, units, color, axis_index++, flags);
60 plot.setRangeAxis(axis.index, axis);
64 public AltosUIAxis newAxis(String label, AltosUnits units, Color color) {
65 return newAxis(label, units, color, AltosUIAxis.axis_default);
68 public void addSeries(String label, int fetch, AltosUnits units, Color color,
69 boolean enabled, AltosUIAxis axis) {
70 AltosUISeries series = new AltosUISeries(label, fetch, units, color, enabled, axis);
71 XYSeriesCollection dataset = new XYSeriesCollection(series);
73 series.renderer.setPlot(plot);
74 plot.setDataset(series_index, dataset);
75 plot.setRenderer(series_index, series.renderer);
76 plot.mapDatasetToRangeAxis(series_index, axis.index);
78 enable.add(label, series, enabled);
79 this.graphers.add(series);
83 public void addSeries(String label, int fetch, AltosUnits units, Color color) {
84 addSeries(label, fetch, units, color, true, newAxis(label, units, color));
87 public void addMarker(String label, int fetch, Color color) {
88 AltosUIMarker marker = new AltosUIMarker(fetch, color, plot);
89 this.graphers.add(marker);
92 public void resetData() {
93 for (AltosUIGrapher g : graphers) {
97 if (dataSet != null) {
98 for (AltosUIDataPoint dataPoint : dataSet.dataPoints())
99 for (AltosUIGrapher g : graphers)
102 for (AltosUIGrapher g : graphers) {
104 g.fireSeriesChanged();
108 public void units_changed(boolean imperial_units) {
109 for (AltosUIGrapher g : graphers)
114 public void setName (String name) {
115 chart.setTitle(name);
118 public void setDataSet (AltosUIDataSet dataSet) {
119 this.dataSet = dataSet;
122 setName(dataSet.name());
125 public AltosUIGraph(AltosUIEnable enable) {
127 this.enable = enable;
128 this.graphers = new ArrayList<AltosUIGrapher>();
129 this.series_index = 0;
132 xAxis = new NumberAxis("Time (s)");
134 xAxis.setAutoRangeIncludesZero(true);
137 plot.setDomainAxis(xAxis);
138 plot.setOrientation(PlotOrientation.VERTICAL);
139 plot.setDomainPannable(true);
140 plot.setRangePannable(true);
142 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
145 ChartUtilities.applyCurrentTheme(chart);
147 plot.setDomainGridlinePaint(gridline_color);
148 plot.setRangeGridlinePaint(gridline_color);
149 plot.setBackgroundPaint(background_color);
150 plot.setBackgroundAlpha((float) 1);
152 chart.setBackgroundPaint(background_color);
153 chart.setBorderPaint(border_color);
154 panel = new ChartPanel(chart);
155 panel.setMouseWheelEnabled(true);
156 panel.setPreferredSize(new java.awt.Dimension(800, 500));
158 AltosPreferences.register_units_listener(this);