7c48264e25092953bbfbb9a4caef7cecb351ee2e
[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         boolean         marker_top;
51         XYItemRenderer  renderer;
52         XYPlot          plot;
53         AltosXYSeries   xy_series;
54         ArrayList<ValueMarker>  markers;
55
56
57         /* AltosUIGrapher interface */
58         public boolean need_reset() {
59                 return false;
60         }
61
62         public void clear() {
63         }
64
65         public void add(AltosUIDataPoint dataPoint) {
66         }
67
68         public void setNotify(boolean notify) {
69         }
70
71         public void fireSeriesChanged() {
72         }
73
74         void set_data() {
75                 if (marker) {
76                         if (markers != null) {
77                                 for (ValueMarker marker : markers)
78                                         plot.removeDomainMarker(marker);
79                         }
80                         markers = new ArrayList<ValueMarker>();
81                         for (AltosTimeValue v : this) {
82                                 String s = units.string_value(v.value);
83                                 ValueMarker marker = new ValueMarker(v.time);
84                                 marker.setLabel(s);
85                                 if (marker_top) {
86                                         marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
87                                         marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
88                                 } else {
89                                         marker.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
90                                         marker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
91                                 }
92                                 marker.setPaint(color);
93                                 if (enable)
94                                         plot.addDomainMarker(marker);
95                                 markers.add(marker);
96                         }
97                 } else {
98                         xy_series.clear();
99
100                         xy_series.setNotify(false);
101                         for (AltosTimeValue v : this) {
102                                 double value = v.value;
103                                 if (units != null)
104                                         value = units.graph_value(value);
105                                 xy_series.add(v.time, value);
106                         }
107                         xy_series.setNotify(true);
108                 }
109         }
110
111         public void set_units() {
112                 axis.set_units();
113                 StandardXYToolTipGenerator      ttg;
114
115                 if (units != null) {
116                         String  time_example = (new AltosUITime()).graph_format(7);
117                         String  example = units.graph_format(7);
118
119                         ttg = new StandardXYToolTipGenerator(String.format("{1}s: {2}%s ({0})",
120                                                                            units.graph_units()),
121                                                              new java.text.DecimalFormat(time_example),
122                                                              new java.text.DecimalFormat(example));
123                         renderer.setBaseToolTipGenerator(ttg);
124                 }
125                 set_data();
126         }
127
128         public AltosXYSeries xy_series() {
129                 return xy_series;
130         }
131
132         public void set_enable(boolean enable) {
133                 if (this.enable != enable) {
134                         this.enable = enable;
135                         if (marker) {
136                                 for (ValueMarker marker : markers) {
137                                         if (enable)
138                                                 plot.addDomainMarker(marker);
139                                         else
140                                                 plot.removeDomainMarker(marker);
141                                 }
142                         } else {
143                                 renderer.setSeriesVisible(0, enable);
144                                 axis.set_enable(enable);
145                         }
146                 }
147         }
148
149         public void set_axis(Color color, boolean enable, AltosUIAxis axis) {
150                 this.color = color;
151                 this.enable = enable;
152                 this.axis = axis;
153                 this.marker = false;
154
155                 axis.ref(this.enable);
156
157                 renderer = new XYLineAndShapeRenderer(true, false);
158                 renderer.setSeriesPaint(0, color);
159                 renderer.setSeriesStroke(0, new BasicStroke(2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
160                 renderer.setSeriesVisible(0, enable);
161                 xy_series = new AltosXYSeries(label);
162         }
163
164         public void set_marker(Color color, boolean enable, XYPlot plot, boolean marker_top) {
165                 this.color = color;
166                 this.enable = enable;
167                 this.marker = true;
168                 this.plot = plot;
169                 this.marker_top = marker_top;
170         }
171
172         public AltosUITimeSeries(String label, AltosUnits units) {
173                 super(label, units);
174         }
175
176         public AltosUITimeSeries(String label, AltosUnits units,
177                                  Color color, boolean enable,
178                                  AltosUIAxis axis) {
179                 this(label, units);
180                 set_axis(color, enable, axis);
181         }
182 }