altosuilib: Adapt to AltosFlightSeries data processing plan
[fw/altos] / altosuilib / AltosUITimeSeries.java
1 /*
2  * Copyright © 2017 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.ArrayList;
23
24 import java.awt.*;
25 import javax.swing.*;
26 import org.altusmetrum.altoslib_11.*;
27
28 import org.jfree.ui.*;
29 import org.jfree.chart.*;
30 import org.jfree.chart.plot.*;
31 import org.jfree.chart.axis.*;
32 import org.jfree.chart.renderer.*;
33 import org.jfree.chart.renderer.xy.*;
34 import org.jfree.chart.labels.*;
35 import org.jfree.data.xy.*;
36 import org.jfree.data.*;
37
38 class AltosXYSeries extends XYSeries {
39
40         public AltosXYSeries(String label) {
41                 super(label);
42         }
43 }
44
45 public class AltosUITimeSeries extends AltosTimeSeries implements AltosUIGrapher {
46         Color           color;
47         boolean         enable;
48         AltosUIAxis     axis;
49         boolean         marker;
50         XYItemRenderer  renderer;
51         XYPlot          plot;
52         AltosXYSeries   xy_series;
53         ArrayList<ValueMarker>  markers;
54
55
56         /* AltosUIGrapher interface */
57         public boolean need_reset() {
58                 return false;
59         }
60
61         public void clear() {
62         }
63
64         public void add(AltosUIDataPoint dataPoint) {
65         }
66
67         public void setNotify(boolean notify) {
68         }
69
70         public void fireSeriesChanged() {
71         }
72
73         void set_data() {
74                 if (marker) {
75                         if (markers != null) {
76                                 for (ValueMarker marker : markers)
77                                         plot.removeDomainMarker(marker);
78                         }
79                         markers = new ArrayList<ValueMarker>();
80                         for (AltosTimeValue v : this) {
81                                 String s = units.string_value(v.value);
82                                 ValueMarker marker = new ValueMarker(v.time);
83                                 marker.setLabel(s);
84                                 marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
85                                 marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
86                                 marker.setPaint(color);
87                                 if (enable)
88                                         plot.addDomainMarker(marker);
89                                 markers.add(marker);
90                         }
91                 } else {
92                         xy_series.clear();
93
94                         for (AltosTimeValue v : this) {
95                                 double value = v.value;
96                                 if (units != null)
97                                         value = units.graph_value(value);
98                                 xy_series.add(v.time, value);
99                         }
100                 }
101         }
102
103         public void set_units() {
104                 axis.set_units();
105                 StandardXYToolTipGenerator      ttg;
106
107                 if (units != null) {
108                         String  time_example = (new AltosUITime()).graph_format(7);
109                         String  example = units.graph_format(7);
110
111                         ttg = new StandardXYToolTipGenerator(String.format("{1}s: {2}%s ({0})",
112                                                                            units.graph_units()),
113                                                              new java.text.DecimalFormat(time_example),
114                                                              new java.text.DecimalFormat(example));
115                         renderer.setBaseToolTipGenerator(ttg);
116                 }
117                 set_data();
118         }
119
120         public AltosXYSeries xy_series() {
121                 return xy_series;
122         }
123
124         public void set_enable(boolean enable) {
125                 if (this.enable != enable) {
126                         this.enable = enable;
127                         if (marker) {
128                                 for (ValueMarker marker : markers) {
129                                         if (enable)
130                                                 plot.addDomainMarker(marker);
131                                         else
132                                                 plot.removeDomainMarker(marker);
133                                 }
134                         } else {
135                                 renderer.setSeriesVisible(0, enable);
136                                 axis.set_enable(enable);
137                         }
138                 }
139         }
140
141         public void set_axis(Color color, boolean enable, AltosUIAxis axis) {
142                 this.color = color;
143                 this.enable = enable;
144                 this.axis = axis;
145                 this.marker = false;
146
147                 axis.ref(this.enable);
148
149                 renderer = new XYLineAndShapeRenderer(true, false);
150                 renderer.setSeriesPaint(0, color);
151                 renderer.setSeriesStroke(0, new BasicStroke(2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
152                 renderer.setSeriesVisible(0, enable);
153                 xy_series = new AltosXYSeries(label);
154         }
155
156         public void set_marker(Color color, boolean enable, XYPlot plot) {
157                 this.color = color;
158                 this.enable = enable;
159                 this.marker = true;
160                 this.plot = plot;
161         }
162
163         public AltosUITimeSeries(String label, AltosUnits units) {
164                 super(label, units);
165         }
166
167         public AltosUITimeSeries(String label, AltosUnits units,
168                                  Color color, boolean enable,
169                                  AltosUIAxis axis) {
170                 this(label, units);
171                 set_axis(color, enable, axis);
172         }
173 }