altos/telefireone-v2.0: Remove build of ao_product.h from Makefile
[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_13;
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_13.*;
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, AltosShapeListener {
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, AltosUILineStyle line_style, int flags) {
60                 AltosUIAxis axis = new AltosUIAxis(label, units, line_style, axis_index++, flags);
61                 plot.setRangeAxis(axis.index, axis);
62                 return axis;
63         }
64
65         public AltosUIAxis newAxis(String label, AltosUnits units, AltosUILineStyle line_style) {
66                 return newAxis(label, units, line_style, 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 set_shapes_visible(boolean visible) {
103                 for (AltosUITimeSeries s : series)
104                         s.set_shapes_visible(visible);
105         }
106
107         public void set_line_width(float width) {
108                 for (AltosUITimeSeries s : series)
109                         s.set_line_width(width);
110         }
111
112         public void setName (String name) {
113                 chart.setTitle(name);
114         }
115
116         public void set_series(AltosUITimeSeries[] series) {
117                 this.series = series;
118                 boolean any_enabled = false;
119
120                 for (AltosUITimeSeries s : series)
121                         if (s.enable)
122                                 any_enabled = true;
123
124                 if (!any_enabled)
125                         for (AltosUITimeSeries s : series)
126                                 s.set_enable(true);
127
128                 for (AltosUITimeSeries s : series)
129                         addSeries(s);
130
131                 units_changed(false);
132         }
133
134         public AltosUIGraph(AltosUIEnable enable, String title) {
135
136                 this.enable = enable;
137                 this.series = null;
138                 this.axis_index = 0;
139
140                 enable.register_shape_listener(this);
141
142                 axes_added = new Hashtable<Integer,Boolean>();
143
144                 xAxis = new NumberAxis("Time (s)");
145
146                 xAxis.setAutoRangeIncludesZero(true);
147
148                 plot = new XYPlot();
149                 plot.setDomainAxis(xAxis);
150                 plot.setOrientation(PlotOrientation.VERTICAL);
151                 plot.setDomainPannable(true);
152                 plot.setRangePannable(true);
153
154                 chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
155                                        plot, true);
156
157                 ChartUtilities.applyCurrentTheme(chart);
158
159                 plot.setDomainGridlinePaint(gridline_color);
160                 plot.setRangeGridlinePaint(gridline_color);
161                 plot.setBackgroundPaint(background_color);
162                 plot.setBackgroundAlpha((float) 1);
163
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));
169
170                 AltosPreferences.register_units_listener(this);
171
172         }
173 }