altoslib,altosuilib: Bump library version numbers
[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_12;
20
21 import java.io.*;
22 import java.util.ArrayList;
23
24 import java.awt.*;
25 import javax.swing.*;
26 import org.altusmetrum.altoslib_12.*;
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 AltosUITime extends AltosUnits {
39         public double value(double v, boolean imperial_units) { return v; }
40
41         public double inverse(double v, boolean imperial_unis) { return v; }
42
43         public String show_units(boolean imperial_units) { return "s"; }
44
45         public String say_units(boolean imperial_units) { return "seconds"; }
46
47         public int show_fraction(int width, boolean imperial_units) {
48                 if (width < 5)
49                         return 0;
50                 return width - 5;
51         }
52
53         public int say_fraction(boolean imperial_units) { return 0; }
54 }
55
56 class AltosXYSeries extends XYSeries {
57
58         public AltosXYSeries(String label) {
59                 super(label);
60         }
61 }
62
63 public class AltosUITimeSeries extends AltosTimeSeries implements AltosUIGrapher {
64         Color           color;
65         boolean         enable;
66         AltosUIAxis     axis;
67         boolean         marker;
68         boolean         marker_top;
69         XYItemRenderer  renderer;
70         XYPlot          plot;
71         AltosXYSeries   xy_series;
72         ArrayList<ValueMarker>  markers;
73
74
75         /* AltosUIGrapher interface */
76         public boolean need_reset() {
77                 return false;
78         }
79
80         public void clear() {
81         }
82
83         public void add(AltosUIDataPoint dataPoint) {
84         }
85
86         public void setNotify(boolean notify) {
87         }
88
89         public void fireSeriesChanged() {
90         }
91
92         void set_data() {
93                 if (marker) {
94                         if (markers != null) {
95                                 for (ValueMarker marker : markers)
96                                         plot.removeDomainMarker(marker);
97                         }
98                         markers = new ArrayList<ValueMarker>();
99                         for (AltosTimeValue v : this) {
100                                 String s = units.string_value(v.value);
101                                 ValueMarker marker = new ValueMarker(v.time);
102                                 marker.setLabel(s);
103                                 if (marker_top) {
104                                         marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
105                                         marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
106                                 } else {
107                                         marker.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
108                                         marker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
109                                 }
110                                 marker.setPaint(color);
111                                 if (enable)
112                                         plot.addDomainMarker(marker);
113                                 markers.add(marker);
114                         }
115                 } else {
116                         xy_series.clear();
117
118                         xy_series.setNotify(false);
119                         for (AltosTimeValue v : this) {
120                                 double value = v.value;
121                                 if (units != null)
122                                         value = units.graph_value(value);
123                                 xy_series.add(v.time, value);
124                         }
125                         xy_series.setNotify(true);
126                 }
127         }
128
129         public void set_units() {
130                 axis.set_units();
131                 StandardXYToolTipGenerator      ttg;
132
133                 if (units != null) {
134                         String  time_example = (new AltosUITime()).graph_format(7);
135                         String  example = units.graph_format(7);
136
137                         ttg = new StandardXYToolTipGenerator(String.format("{1}s: {2}%s ({0})",
138                                                                            units.graph_units()),
139                                                              new java.text.DecimalFormat(time_example),
140                                                              new java.text.DecimalFormat(example));
141                         renderer.setBaseToolTipGenerator(ttg);
142                 }
143                 set_data();
144         }
145
146         public AltosXYSeries xy_series() {
147                 return xy_series;
148         }
149
150         public void set_enable(boolean enable) {
151                 if (this.enable != enable) {
152                         this.enable = enable;
153                         if (marker) {
154                                 for (ValueMarker marker : markers) {
155                                         if (enable)
156                                                 plot.addDomainMarker(marker);
157                                         else
158                                                 plot.removeDomainMarker(marker);
159                                 }
160                         } else {
161                                 renderer.setSeriesVisible(0, enable);
162                                 axis.set_enable(enable);
163                         }
164                 }
165         }
166
167         public void set_axis(Color color, boolean enable, AltosUIAxis axis) {
168                 this.color = color;
169                 this.enable = enable;
170                 this.axis = axis;
171                 this.marker = false;
172
173                 axis.ref(this.enable);
174
175                 renderer = new XYLineAndShapeRenderer(true, false);
176                 renderer.setSeriesPaint(0, color);
177                 renderer.setSeriesStroke(0, new BasicStroke(2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
178                 renderer.setSeriesVisible(0, enable);
179                 xy_series = new AltosXYSeries(label);
180         }
181
182         public void set_marker(Color color, boolean enable, XYPlot plot, boolean marker_top) {
183                 this.color = color;
184                 this.enable = enable;
185                 this.marker = true;
186                 this.plot = plot;
187                 this.marker_top = marker_top;
188         }
189
190         public AltosUITimeSeries(String label, AltosUnits units) {
191                 super(label, units);
192         }
193
194         public AltosUITimeSeries(String label, AltosUnits units,
195                                  Color color, boolean enable,
196                                  AltosUIAxis axis) {
197                 this(label, units);
198                 set_axis(color, enable, axis);
199         }
200 }