altos/telefireone-v2.0: Remove build of ao_product.h from Makefile
[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_13;
20
21 import java.io.*;
22 import java.util.ArrayList;
23
24 import java.awt.*;
25 import javax.swing.*;
26 import org.altusmetrum.altoslib_13.*;
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         AltosUILineStyle        line_style;
65         boolean                 enable;
66         boolean                 custom_axis_set;
67         AltosUIAxis             axis;
68         boolean                 marker;
69         boolean                 marker_top;
70         XYLineAndShapeRenderer  renderer;
71         XYPlot                  plot;
72         AltosXYSeries           xy_series;
73         ArrayList<ValueMarker>  markers;
74         float                   width;
75
76         /* AltosUIGrapher interface */
77         public boolean need_reset() {
78                 return false;
79         }
80
81         public void clear() {
82         }
83
84         public void add(AltosUIDataPoint dataPoint) {
85         }
86
87         public void setNotify(boolean notify) {
88         }
89
90         public void fireSeriesChanged() {
91         }
92
93         public void set_data() {
94                 if (marker) {
95                         if (markers != null) {
96                                 for (ValueMarker marker : markers)
97                                         plot.removeDomainMarker(marker);
98                         }
99                         markers = new ArrayList<ValueMarker>();
100                         for (AltosTimeValue v : this) {
101                                 String s = units.string_value(v.value);
102                                 ValueMarker marker = new ValueMarker(v.time);
103                                 marker.setLabel(s);
104                                 if (marker_top) {
105                                         marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
106                                         marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
107                                 } else {
108                                         marker.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
109                                         marker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
110                                 }
111                                 marker.setPaint(line_style.color);
112                                 marker.setStroke(new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
113                                 if (enable)
114                                         plot.addDomainMarker(marker);
115                                 markers.add(marker);
116                         }
117                 } else {
118                         xy_series.clear();
119
120                         xy_series.setNotify(false);
121                         for (AltosTimeValue v : this) {
122                                 double value = v.value;
123                                 if (units != null)
124                                         value = units.graph_value(value);
125                                 xy_series.add(v.time, value);
126                         }
127                         xy_series.setNotify(true);
128                 }
129                 clear_changed();
130         }
131
132         public void set_units() {
133                 axis.set_units();
134                 StandardXYToolTipGenerator      ttg;
135
136                 if (units != null) {
137                         String  time_example = (new AltosUITime()).graph_format(7);
138                         String  example = units.graph_format(7);
139
140                         ttg = new StandardXYToolTipGenerator(String.format("{1}s: {2}%s ({0})",
141                                                                            units.graph_units()),
142                                                              new java.text.DecimalFormat(time_example),
143                                                              new java.text.DecimalFormat(example));
144                         renderer.setBaseToolTipGenerator(ttg);
145                 }
146                 set_data();
147         }
148
149         public AltosXYSeries xy_series() {
150                 return xy_series;
151         }
152
153         public void set_enable(boolean enable) {
154                 if (this.enable != enable) {
155                         this.enable = enable;
156                         if (marker) {
157                                 for (ValueMarker marker : markers) {
158                                         if (enable)
159                                                 plot.addDomainMarker(marker);
160                                         else
161                                                 plot.removeDomainMarker(marker);
162                                 }
163                         } else {
164                                 renderer.setSeriesVisible(0, enable);
165                                 axis.set_enable(enable);
166                         }
167                 }
168         }
169
170         // public BasicStroke(float width, int cap, int join, float miterlimit,
171         // float dash[], float dash_phase)
172
173         public void set_line_width(float width) {
174                 this.width = width;
175                 if (markers != null) {
176                         for (ValueMarker marker : markers) {
177                                 marker.setStroke(new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
178                         }
179                 } else {
180                         if (line_style.dash[0] == 0.0)
181                                 renderer.setSeriesStroke(0, new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
182                         else
183                                 renderer.setSeriesStroke(0, new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10.0f, line_style.dash, 0.0f));
184                 }
185         }
186
187         public void set_axis(AltosUILineStyle line_style, boolean enable, AltosUIAxis axis) {
188                 this.line_style = line_style;
189                 this.enable = enable;
190                 this.axis = axis;
191                 this.marker = false;
192                 this.width = 1.0f;
193
194                 axis.ref(this.enable);
195
196                 renderer = new XYLineAndShapeRenderer(true, false);
197                 renderer.setSeriesPaint(0, line_style.color);
198                 set_line_width(this.width);
199                 renderer.setSeriesVisible(0, enable);
200                 xy_series = new AltosXYSeries(label);
201         }
202
203         public void set_marker(AltosUILineStyle line_style, boolean enable, XYPlot plot, boolean marker_top) {
204                 this.line_style = line_style;
205                 this.enable = enable;
206                 this.marker = true;
207                 this.plot = plot;
208                 this.marker_top = marker_top;
209         }
210
211         public void set_shapes_visible(boolean shapes_visible) {
212                 renderer.setSeriesShapesVisible(0, shapes_visible);
213         }
214
215         public AltosUITimeSeries(String label, AltosUnits units) {
216                 super(label, units);
217         }
218
219         public AltosUITimeSeries(String label, AltosUnits units,
220                                  AltosUILineStyle line_style, boolean enable,
221                                  AltosUIAxis axis) {
222                 this(label, units);
223                 set_axis(line_style, enable, axis);
224         }
225 }