1 package org.opentrafficsim.draw.graphs;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics2D;
6 import java.awt.geom.AffineTransform;
7 import java.awt.geom.Rectangle2D;
8 import java.awt.image.BufferedImage;
9 import java.io.IOException;
10 import java.util.LinkedHashSet;
11 import java.util.Set;
12 import java.util.UUID;
13
14 import org.djunits.value.vdouble.scalar.Duration;
15 import org.djunits.value.vdouble.scalar.Time;
16 import org.djutils.base.Identifiable;
17 import org.djutils.event.EventType;
18 import org.djutils.metadata.MetaData;
19 import org.djutils.metadata.ObjectDescriptor;
20 import org.jfree.chart.ChartUtils;
21 import org.jfree.chart.JFreeChart;
22 import org.jfree.chart.plot.XYPlot;
23 import org.jfree.chart.title.TextTitle;
24 import org.jfree.data.general.Dataset;
25 import org.jfree.data.general.DatasetChangeEvent;
26 import org.jfree.data.general.DatasetChangeListener;
27 import org.jfree.data.general.DatasetGroup;
28
29
30
31
32
33
34
35
36
37
38
39
40 public abstract class AbstractPlot implements Identifiable, Dataset
41 {
42
43
44
45
46
47 public static final EventType GRAPH_ADD_EVENT = new EventType("GRAPH.ADD",
48 new MetaData("Graph add", "Graph added", new ObjectDescriptor("Graph id", "Id of the graph", String.class)));
49
50
51
52
53
54 public static final EventType GRAPH_REMOVE_EVENT = new EventType("GRAPH.REMOVE",
55 new MetaData("Graph remove", "Graph removed", new ObjectDescriptor("Graph id", "Id of the graph", String.class)));
56
57
58 public static final Time DEFAULT_INITIAL_UPPER_TIME_BOUND = Time.instantiateSI(300.0);
59
60
61 private final PlotScheduler scheduler;
62
63
64 private final String id = UUID.randomUUID().toString();
65
66
67 private final String caption;
68
69
70 private JFreeChart chart;
71
72
73 private Set<DatasetChangeListener> listeners = new LinkedHashSet<>();
74
75
76 private final Duration delay;
77
78
79 private Time updateTime;
80
81
82 private int updates = 0;
83
84
85 private Duration updateInterval;
86
87
88
89
90
91
92
93
94 public AbstractPlot(final PlotScheduler scheduler, final String caption, final Duration updateInterval,
95 final Duration delay)
96 {
97 this.scheduler = scheduler;
98 this.caption = caption;
99 this.updateInterval = updateInterval;
100 this.delay = delay;
101 this.updates = (int) (scheduler.getTime().si / updateInterval.si);
102 update();
103 }
104
105
106
107
108
109 @SuppressWarnings("methodlength")
110 protected void setChart(final JFreeChart chart)
111 {
112 this.chart = chart;
113
114
115 chart.setTitle(new TextTitle(chart.getTitle().getText(), new Font("SansSerif", java.awt.Font.BOLD, 16)));
116
117
118 chart.getPlot().setBackgroundPaint(Color.LIGHT_GRAY);
119 chart.setBackgroundPaint(Color.WHITE);
120 if (chart.getPlot() instanceof XYPlot)
121 {
122 chart.getXYPlot().setDomainGridlinePaint(Color.WHITE);
123 chart.getXYPlot().setRangeGridlinePaint(Color.WHITE);
124 }
125
126 }
127
128
129
130
131
132
133
134
135
136 public byte[] encodeAsPng(final int width, final int height, final double fontSize) throws IOException
137 {
138
139
140 double baseWidth = width / (fontSize / 16);
141 double baseHeight = height / (fontSize / 16);
142
143 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
144 Graphics2D g2 = image.createGraphics();
145
146 AffineTransform saved = g2.getTransform();
147 g2.transform(AffineTransform.getScaleInstance(width / baseWidth, height / baseHeight));
148 getChart().draw(g2, new Rectangle2D.Double(0, 0, baseWidth, baseHeight), null, null);
149 g2.setTransform(saved);
150 g2.dispose();
151 return ChartUtils.encodeAsPNG(image);
152 }
153
154
155 @Override
156 public final DatasetGroup getGroup()
157 {
158 return null;
159 }
160
161
162 @Override
163 public final void setGroup(final DatasetGroup group)
164 {
165
166 }
167
168
169
170
171
172
173
174
175 public void setAutoBoundDomain(final XYPlot plot)
176 {
177
178 }
179
180
181
182
183
184
185
186
187 public void setAutoBoundRange(final XYPlot plot)
188 {
189
190 }
191
192
193
194
195
196 public abstract GraphType getGraphType();
197
198
199
200
201
202
203
204 public abstract String getStatusLabel(double domainValue, double rangeValue);
205
206
207
208
209
210 protected abstract void increaseTime(Time time);
211
212
213
214
215 public final void notifyPlotChange()
216 {
217 DatasetChangeEvent event = new DatasetChangeEvent(this, this);
218 for (DatasetChangeListener dcl : this.listeners)
219 {
220 dcl.datasetChanged(event);
221 }
222 }
223
224
225
226
227
228 public final JFreeChart getChart()
229 {
230 return this.chart;
231 }
232
233
234 @Override
235 public final String getId()
236 {
237 return this.id;
238 }
239
240
241 @Override
242 public final void addChangeListener(final DatasetChangeListener listener)
243 {
244 this.listeners.add(listener);
245 }
246
247
248 @Override
249 public final void removeChangeListener(final DatasetChangeListener listener)
250 {
251 this.listeners.remove(listener);
252 }
253
254
255
256
257
258 public final void setUpdateInterval(final Duration interval)
259 {
260 this.scheduler.cancelEvent(this);
261 this.updates = (int) (this.scheduler.getTime().si / interval.si);
262 this.updateInterval = interval;
263 this.updateTime = Time.instantiateSI(this.updates * this.updateInterval.si);
264 scheduleNextUpdateEvent();
265 }
266
267
268
269
270
271 public final Time getUpdateTime()
272 {
273 return this.updateTime;
274 }
275
276
277
278
279 protected void update()
280 {
281
282 this.updateTime = this.scheduler.getTime();
283 increaseTime(this.updateTime.minus(this.delay));
284 notifyPlotChange();
285 scheduleNextUpdateEvent();
286 }
287
288
289
290
291 private void scheduleNextUpdateEvent()
292 {
293 this.updates++;
294
295 this.scheduler.scheduleUpdate(Time.instantiateSI(this.updateInterval.si * this.updates + this.delay.si), this);
296 }
297
298
299
300
301
302 public String getCaption()
303 {
304 return this.caption;
305 }
306
307 }