]> git.gag.com Git - fw/altos/blob - altosuilib/AltosUIGraph.java
altoslib: Limit simultanous map tile downloads to 128
[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_7;
19
20 import java.io.*;
21 import java.util.ArrayList;
22
23 import java.awt.*;
24 import javax.swing.*;
25 import org.altusmetrum.altoslib_7.*;
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                             axis_index;
47         int                             series_index;
48
49         static final private Color gridline_color = new Color(0, 0, 0);
50         static final private Color border_color = new Color(255, 255, 255);
51         static final private Color background_color = new Color(255, 255, 255);
52
53         public JPanel panel() {
54                 return panel;
55         }
56
57         public AltosUIAxis newAxis(String label, AltosUnits units, Color color, int flags) {
58                 AltosUIAxis axis = new AltosUIAxis(label, units, color, axis_index++, flags);
59                 plot.setRangeAxis(axis.index, axis);
60                 return axis;
61         }
62
63         public AltosUIAxis newAxis(String label, AltosUnits units, Color color) {
64                 return newAxis(label, units, color, AltosUIAxis.axis_default);
65         }
66
67         public void addSeries(String label, int fetch, AltosUnits units, Color color,
68                               boolean enabled, AltosUIAxis axis) {
69                 AltosUISeries           series = new AltosUISeries(label, fetch, units, color, enabled, axis);
70                 XYSeriesCollection      dataset = new XYSeriesCollection(series);
71
72                 series.renderer.setPlot(plot);
73                 plot.setDataset(series_index, dataset);
74                 plot.setRenderer(series_index, series.renderer);
75                 plot.mapDatasetToRangeAxis(series_index, axis.index);
76                 if (enable != null)
77                         enable.add(label, series, enabled);
78                 this.graphers.add(series);
79                 series_index++;
80         }
81
82         public void addSeries(String label, int fetch, AltosUnits units, Color color) {
83                 addSeries(label, fetch, units, color, true, newAxis(label, units, color));
84         }
85
86         public void addMarker(String label, int fetch, Color color) {
87                 AltosUIMarker           marker = new AltosUIMarker(fetch, color, plot);
88                 this.graphers.add(marker);
89         }
90
91         public void resetData() {
92                 for (AltosUIGrapher g : graphers) {
93                         g.clear();
94                         g.setNotify(false);
95                 }
96                 if (dataSet != null) {
97                         for (AltosUIDataPoint dataPoint : dataSet.dataPoints())
98                                 for (AltosUIGrapher g : graphers)
99                                         g.add(dataPoint);
100                 }
101                 for (AltosUIGrapher g : graphers) {
102                         g.setNotify(true);
103                         g.fireSeriesChanged();
104                 }
105         }
106
107         public void units_changed(boolean imperial_units) {
108                 for (AltosUIGrapher g : graphers)
109                         g.set_units();
110                 resetData();
111         }
112
113         public void setName (String name) {
114                 chart.setTitle(name);
115         }
116
117         public void setDataSet (AltosUIDataSet dataSet) {
118                 this.dataSet = dataSet;
119                 resetData();
120                 if (dataSet != null)
121                         setName(dataSet.name());
122         }
123
124         public AltosUIGraph(AltosUIEnable enable) {
125
126                 this.enable = enable;
127                 this.graphers = new ArrayList<AltosUIGrapher>();
128                 this.series_index = 0;
129                 this.axis_index = 0;
130
131                 xAxis = new NumberAxis("Time (s)");
132
133                 xAxis.setAutoRangeIncludesZero(true);
134
135                 plot = new XYPlot();
136                 plot.setDomainAxis(xAxis);
137                 plot.setOrientation(PlotOrientation.VERTICAL);
138                 plot.setDomainPannable(true);
139                 plot.setRangePannable(true);
140
141                 chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
142                                        plot, true);
143
144                 ChartUtilities.applyCurrentTheme(chart);
145
146                 plot.setDomainGridlinePaint(gridline_color);
147                 plot.setRangeGridlinePaint(gridline_color);
148                 plot.setBackgroundPaint(background_color);
149                 plot.setBackgroundAlpha((float) 1);
150
151                 chart.setBackgroundPaint(background_color);
152                 chart.setBorderPaint(border_color);
153                 panel = new ChartPanel(chart);
154                 panel.setMouseWheelEnabled(true);
155                 panel.setPreferredSize(new java.awt.Dimension(800, 500));
156
157                 AltosPreferences.register_units_listener(this);
158         }
159 }