775201ed2605d278d89565b3a43bfc4ef994dd7b
[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_11;
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_11.*;
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 setName (String name) {
99                 chart.setTitle(name);
100         }
101
102         public void set_series(AltosUITimeSeries[] series) {
103                 this.series = series;
104                 boolean any_enabled = false;
105
106                 for (AltosUITimeSeries s : series)
107                         if (s.enable)
108                                 any_enabled = true;
109
110                 if (!any_enabled)
111                         for (AltosUITimeSeries s : series)
112                                 s.set_enable(true);
113
114                 for (AltosUITimeSeries s : series)
115                         addSeries(s);
116
117                 units_changed(false);
118         }
119
120         public AltosUIGraph(AltosUIEnable enable, String title) {
121
122                 this.enable = enable;
123                 this.series = null;
124                 this.axis_index = 0;
125
126                 axes_added = new Hashtable<Integer,Boolean>();
127
128                 xAxis = new NumberAxis("Time (s)");
129
130                 xAxis.setAutoRangeIncludesZero(true);
131
132                 plot = new XYPlot();
133                 plot.setDomainAxis(xAxis);
134                 plot.setOrientation(PlotOrientation.VERTICAL);
135                 plot.setDomainPannable(true);
136                 plot.setRangePannable(true);
137
138                 chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
139                                        plot, true);
140
141                 ChartUtilities.applyCurrentTheme(chart);
142
143                 plot.setDomainGridlinePaint(gridline_color);
144                 plot.setRangeGridlinePaint(gridline_color);
145                 plot.setBackgroundPaint(background_color);
146                 plot.setBackgroundAlpha((float) 1);
147
148                 chart.setBackgroundPaint(background_color);
149                 chart.setBorderPaint(border_color);
150                 panel = new ChartPanel(chart);
151                 panel.setMouseWheelEnabled(true);
152                 panel.setPreferredSize(new java.awt.Dimension(800, 500));
153
154                 AltosPreferences.register_units_listener(this);
155
156         }
157 }