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_12;
23 import java.util.ArrayList;
27 import org.altusmetrum.altoslib_12.*;
29 import org.jfree.ui.*;
30 import org.jfree.chart.*;
31 import org.jfree.chart.plot.*;
32 import org.jfree.chart.axis.*;
33 import org.jfree.chart.renderer.*;
34 import org.jfree.chart.renderer.xy.*;
35 import org.jfree.chart.labels.*;
36 import org.jfree.data.xy.*;
37 import org.jfree.data.*;
39 public class AltosUIGraph implements AltosUnitsListener, AltosShapeListener {
43 public ChartPanel panel;
46 AltosUITimeSeries[] series;
49 Hashtable<Integer,Boolean> axes_added;
51 static final private Color gridline_color = new Color(0, 0, 0);
52 static final private Color border_color = new Color(255, 255, 255);
53 static final private Color background_color = new Color(255, 255, 255);
55 public JPanel panel() {
59 public AltosUIAxis newAxis(String label, AltosUnits units, AltosUILineStyle line_style, int flags) {
60 AltosUIAxis axis = new AltosUIAxis(label, units, line_style, axis_index++, flags);
61 plot.setRangeAxis(axis.index, axis);
65 public AltosUIAxis newAxis(String label, AltosUnits units, AltosUILineStyle line_style) {
66 return newAxis(label, units, line_style, AltosUIAxis.axis_default);
69 void addAxis(AltosUIAxis axis) {
70 if (!axes_added.containsKey(axis.index)) {
71 axes_added.put(axis.index, true);
72 plot.setRangeAxis(axis.index, axis);
76 public void addSeries(AltosUITimeSeries series) {
77 XYSeriesCollection dataset = new XYSeriesCollection(series.xy_series());
81 series.renderer.setPlot(plot);
82 plot.setDataset(series_index, dataset);
83 plot.setRenderer(series_index, series.renderer);
84 plot.mapDatasetToRangeAxis(series_index, series.axis.index);
86 enable.add(series.label, series, series.enable);
90 public void addMarker(AltosUITimeSeries series) {
93 public void units_changed(boolean imperial_units) {
94 for (AltosUITimeSeries s : series)
98 public void filter_changed() {
102 public void set_shapes_visible(boolean visible) {
103 for (AltosUITimeSeries s : series)
104 s.set_shapes_visible(visible);
107 public void set_line_width(float width) {
108 for (AltosUITimeSeries s : series)
109 s.set_line_width(width);
112 public void setName (String name) {
113 chart.setTitle(name);
116 public void set_series(AltosUITimeSeries[] series) {
117 this.series = series;
118 boolean any_enabled = false;
120 for (AltosUITimeSeries s : series)
125 for (AltosUITimeSeries s : series)
128 for (AltosUITimeSeries s : series)
131 units_changed(false);
134 public AltosUIGraph(AltosUIEnable enable, String title) {
136 this.enable = enable;
140 enable.register_shape_listener(this);
142 axes_added = new Hashtable<Integer,Boolean>();
144 xAxis = new NumberAxis("Time (s)");
146 xAxis.setAutoRangeIncludesZero(true);
149 plot.setDomainAxis(xAxis);
150 plot.setOrientation(PlotOrientation.VERTICAL);
151 plot.setDomainPannable(true);
152 plot.setRangePannable(true);
154 chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
157 ChartUtilities.applyCurrentTheme(chart);
159 plot.setDomainGridlinePaint(gridline_color);
160 plot.setRangeGridlinePaint(gridline_color);
161 plot.setBackgroundPaint(background_color);
162 plot.setBackgroundAlpha((float) 1);
164 chart.setBackgroundPaint(background_color);
165 chart.setBorderPaint(border_color);
166 panel = new ChartPanel(chart);
167 panel.setMouseWheelEnabled(true);
168 panel.setPreferredSize(new java.awt.Dimension(800, 500));
170 AltosPreferences.register_units_listener(this);