altoslib: Add user-selectable filter width for data smoothing
[fw/altos] / altosuilib / AltosUIGraph.java
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altosuilib_12;
20
21 import java.io.*;
22 import java.util.*;
23 import java.util.ArrayList;
24
25 import java.awt.*;
26 import javax.swing.*;
27 import org.altusmetrum.altoslib_12.*;
28
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.*;
38
39 public class AltosUIGraph implements AltosUnitsListener {
40
41         XYPlot                          plot;
42         JFreeChart                      chart;
43         public ChartPanel               panel;
44         NumberAxis                      xAxis;
45         AltosUIEnable                   enable;
46         AltosUITimeSeries[]             series;
47         int                             axis_index;
48         int                             series_index;
49         Hashtable<Integer,Boolean>      axes_added;
50
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);
54
55         public JPanel panel() {
56                 return panel;
57         }
58
59         public AltosUIAxis newAxis(String label, AltosUnits units, Color color, int flags) {
60                 AltosUIAxis axis = new AltosUIAxis(label, units, color, axis_index++, flags);
61                 plot.setRangeAxis(axis.index, axis);
62                 return axis;
63         }
64
65         public AltosUIAxis newAxis(String label, AltosUnits units, Color color) {
66                 return newAxis(label, units, color, AltosUIAxis.axis_default);
67         }
68
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);
73                 }
74         }
75
76         public void addSeries(AltosUITimeSeries series) {
77                 XYSeriesCollection      dataset = new XYSeriesCollection(series.xy_series());
78
79                 addAxis(series.axis);
80
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);
85                 if (enable != null)
86                         enable.add(series.label, series, series.enable);
87                 series_index++;
88         }
89
90         public void addMarker(AltosUITimeSeries series) {
91         }
92
93         public void units_changed(boolean imperial_units) {
94                 for (AltosUITimeSeries s : series)
95                         s.set_units();
96         }
97
98         public void filter_changed() {
99                 units_changed(false);
100         }
101
102         public void setName (String name) {
103                 chart.setTitle(name);
104         }
105
106         public void set_series(AltosUITimeSeries[] series) {
107                 this.series = series;
108                 boolean any_enabled = false;
109
110                 for (AltosUITimeSeries s : series)
111                         if (s.enable)
112                                 any_enabled = true;
113
114                 if (!any_enabled)
115                         for (AltosUITimeSeries s : series)
116                                 s.set_enable(true);
117
118                 for (AltosUITimeSeries s : series)
119                         addSeries(s);
120
121                 units_changed(false);
122         }
123
124         public AltosUIGraph(AltosUIEnable enable, String title) {
125
126                 this.enable = enable;
127                 this.series = null;
128                 this.axis_index = 0;
129
130                 axes_added = new Hashtable<Integer,Boolean>();
131
132                 xAxis = new NumberAxis("Time (s)");
133
134                 xAxis.setAutoRangeIncludesZero(true);
135
136                 plot = new XYPlot();
137                 plot.setDomainAxis(xAxis);
138                 plot.setOrientation(PlotOrientation.VERTICAL);
139                 plot.setDomainPannable(true);
140                 plot.setRangePannable(true);
141
142                 chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
143                                        plot, true);
144
145                 ChartUtilities.applyCurrentTheme(chart);
146
147                 plot.setDomainGridlinePaint(gridline_color);
148                 plot.setRangeGridlinePaint(gridline_color);
149                 plot.setBackgroundPaint(background_color);
150                 plot.setBackgroundAlpha((float) 1);
151
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));
157
158                 AltosPreferences.register_units_listener(this);
159
160         }
161 }