altosuilib/micropeak: Add state markers to micropeak graph
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_1;
19
20 import java.io.*;
21 import java.util.ArrayList;
22
23 import java.awt.*;
24 import javax.swing.*;
25 import org.altusmetrum.altoslib_1.*;
26
27 import org.jfree.ui.*;
28 import org.jfree.chart.*;
29 import org.jfree.chart.plot.*;
30 import org.jfree.chart.axis.*;
31 import org.jfree.chart.renderer.*;
32 import org.jfree.chart.renderer.xy.*;
33 import org.jfree.chart.labels.*;
34 import org.jfree.data.xy.*;
35 import org.jfree.data.*;
36
37 public class AltosUIGraph implements AltosUnitsListener {
38
39         XYPlot                          plot;
40         JFreeChart                      chart;
41         public ChartPanel               panel;
42         NumberAxis                      xAxis;
43         AltosUIEnable                   enable;
44         ArrayList<AltosUIGrapher>       graphers;
45         AltosUIDataSet                  dataSet;
46         int                             index;
47
48         static final private Color gridline_color = new Color(0, 0, 0);
49         static final private Color border_color = new Color(255, 255, 255);
50         static final private Color background_color = new Color(255, 255, 255);
51
52         public JPanel panel() {
53                 return panel;
54         }
55
56         public void addSeries(String label, int fetch, AltosUnits units, Color color) {
57                 AltosUISeries           series = new AltosUISeries(label, fetch, units, color);
58                 XYSeriesCollection      dataset = new XYSeriesCollection(series);
59
60                 series.renderer.setPlot(plot);
61                 plot.setRangeAxis(index, series.axis);
62                 plot.setDataset(index, dataset);
63                 plot.setRenderer(index, series.renderer);
64                 plot.mapDatasetToRangeAxis(index, index);
65                 if (enable != null)
66                         enable.add(label, series, true);
67                 this.graphers.add(series);
68                 index++;
69         }
70         
71         public void addMarker(String label, int fetch, Color color) {
72                 AltosUIMarker           marker = new AltosUIMarker(fetch, color, plot);
73                 if (enable != null)
74                         enable.add(label, marker, true);
75                 this.graphers.add(marker);
76         }
77
78         public void resetData() {
79                 for (AltosUIGrapher g : graphers)
80                         g.clear();
81                 if (dataSet != null) {
82                         for (AltosUIDataPoint dataPoint : dataSet.dataPoints())
83                                 for (AltosUIGrapher g : graphers)
84                                         g.add(dataPoint);
85                 }
86         }
87
88         public void units_changed(boolean imperial_units) {
89                 for (AltosUIGrapher g : graphers)
90                         g.set_units();
91                 resetData();
92         }
93
94         public void setName (String name) {
95                 chart.setTitle(name);
96         }
97
98         public void setDataSet (AltosUIDataSet dataSet) {
99                 this.dataSet = dataSet;
100                 if (dataSet != null)
101                         setName(dataSet.name());
102                 resetData();
103         }
104
105         public AltosUIGraph(AltosUIEnable enable) {
106
107                 this.enable = enable;
108                 this.graphers = new ArrayList<AltosUIGrapher>();
109                 this.index = 0;
110
111                 xAxis = new NumberAxis("Time (s)");
112                 
113                 xAxis.setAutoRangeIncludesZero(true);
114
115                 plot = new XYPlot();
116                 plot.setDomainAxis(xAxis);
117                 plot.setOrientation(PlotOrientation.VERTICAL);
118                 plot.setDomainPannable(true);
119                 plot.setRangePannable(true);
120
121                 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
122                                        plot, true);
123
124                 ChartUtilities.applyCurrentTheme(chart);
125
126                 plot.setDomainGridlinePaint(gridline_color);
127                 plot.setRangeGridlinePaint(gridline_color);
128                 plot.setBackgroundPaint(background_color);
129                 plot.setBackgroundAlpha((float) 1);
130
131                 chart.setBackgroundPaint(background_color);
132                 chart.setBorderPaint(border_color);
133                 panel = new ChartPanel(chart);
134                 panel.setMouseWheelEnabled(true);
135                 panel.setPreferredSize(new java.awt.Dimension(800, 500));
136
137                 AltosPreferences.register_units_listener(this);
138         }
139 }