altoslib: Fix altoslib install
[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<AltosUISeries>        series;
45         AltosUIDataSet                  dataSet;
46
47         static final private Color gridline_color = new Color(0, 0, 0);
48         static final private Color border_color = new Color(255, 255, 255);
49         static final private Color background_color = new Color(255, 255, 255);
50
51         public JPanel panel() {
52                 return panel;
53         }
54
55         public void addSeries(int index, String label, int fetch, AltosUnits units, Color color) {
56                 AltosUISeries           series = new AltosUISeries(label, fetch, units, color);
57                 XYSeriesCollection      dataset = new XYSeriesCollection(series);
58
59                 series.renderer.setPlot(plot);
60                 plot.setRangeAxis(index, series.axis);
61                 plot.setDataset(index, dataset);
62                 plot.setRenderer(index, series.renderer);
63                 plot.mapDatasetToRangeAxis(index, index);
64                 if (enable != null)
65                         enable.add(label, series, true);
66                 this.series.add(series);
67         }
68         
69         public void resetData() {
70                 for (AltosUISeries s : series)
71                         s.clear();
72                 if (dataSet != null) {
73                         for (AltosUIDataPoint dataPoint : dataSet.dataPoints())
74                                 for (AltosUISeries s : series)
75                                         s.add(dataPoint);
76                 }
77         }
78
79         public void units_changed(boolean imperial_units) {
80                 for (AltosUISeries s : series)
81                         s.set_units();
82                 resetData();
83         }
84
85         public void setName (String name) {
86                 chart.setTitle(name);
87         }
88
89         public void setDataSet (AltosUIDataSet dataSet) {
90                 this.dataSet = dataSet;
91                 if (dataSet != null)
92                         setName(dataSet.name());
93                 resetData();
94         }
95
96         public AltosUIGraph(AltosUIEnable enable) {
97
98                 this.enable = enable;
99                 this.series = new ArrayList<AltosUISeries>();
100
101                 xAxis = new NumberAxis("Time (s)");
102                 
103                 xAxis.setAutoRangeIncludesZero(true);
104
105                 plot = new XYPlot();
106                 plot.setDomainAxis(xAxis);
107                 plot.setOrientation(PlotOrientation.VERTICAL);
108                 plot.setDomainPannable(true);
109                 plot.setRangePannable(true);
110
111                 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
112                                        plot, true);
113
114                 ChartUtilities.applyCurrentTheme(chart);
115
116                 plot.setDomainGridlinePaint(gridline_color);
117                 plot.setRangeGridlinePaint(gridline_color);
118                 plot.setBackgroundPaint(background_color);
119                 plot.setBackgroundAlpha((float) 1);
120
121                 chart.setBackgroundPaint(background_color);
122                 chart.setBorderPaint(border_color);
123                 panel = new ChartPanel(chart);
124                 panel.setMouseWheelEnabled(true);
125                 panel.setPreferredSize(new java.awt.Dimension(800, 500));
126
127                 AltosPreferences.register_units_listener(this);
128         }
129 }