-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathZoomManager.java
332 lines (293 loc) · 9.72 KB
/
ZoomManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package com.github.javafx.charts.zooming;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.chart.Axis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class ZoomManager<X, Y> {
static <X, Y> ObservableList<XYChart.Series<X, Y>> deepCopySeries(final Collection<XYChart.Series<X, Y>> data) {
final ObservableList<XYChart.Series<X, Y>> backup = FXCollections.observableArrayList();
for (final Series<X, Y> s : data) {
backup.add(deepCopySeries(s));
}
return backup;
}
static <X, Y> XYChart.Series<X, Y> deepCopySeries(final XYChart.Series<X, Y> series) {
final XYChart.Series<X, Y> result = new XYChart.Series<>();
result.setName(series.getName());
result.setData(deepCopySeriesData(series.getData()));
return result;
}
static <X, Y> ObservableList<XYChart.Data<X, Y>> deepCopySeriesData(
final Collection<? extends XYChart.Data<X, Y>> data) {
final ObservableList<XYChart.Data<X, Y>> result = FXCollections.observableArrayList();
for (final Data<X, Y> i : data) {
result.add(new Data<>(i.getXValue(), i.getYValue()));
}
return result;
}
static <X, Y> ObservableList<X> extractXValues(final ObservableList<Data<X, Y>> data) {
final ObservableList<X> result = FXCollections.observableArrayList();
for (final Data<X, Y> d : data) {
result.add(d.getXValue());
}
return result;
}
static <X, Y> ObservableList<Y> extractYValues(final ObservableList<Data<X, Y>> data) {
final ObservableList<Y> result = FXCollections.observableArrayList();
for (final Data<X, Y> d : data) {
result.add(d.getYValue());
}
return result;
}
static Object getObject(final Axis<?> axis, final double cooridnate) {
final Object object = axis.getValueForDisplay(cooridnate);
return object;
}
static Node getRootNode(final Node node) {
Node n = node;
while (n.getParent() != null) {
n = n.getParent();
}
return n;
}
private final ObservableList<XYChart.Series<X, Y>> series;
private final XYChart<X, Y> chart;
private volatile boolean zoomed;
/**
*
*
* Creates a new {@code ZoomManager}.
* </p>
* <b> Due to a bug, {@code series} must not be added to the chart! </b> If you
* do so, the chart will be emtpy.
* </p>
*
*
* @param chartParent
* the chart's parent {@link Pane}.
* @param chart
* the {@link XYChart} to which zooming functionality is added
* @param series
* collection of chart data to display
* @throws IllegalArgumentException
* if chart data is empty or {@code null}
*/
public ZoomManager(final Pane chartParent, final XYChart<X, Y> chart,
final Collection<? extends Series<X, Y>> series) {
super();
this.chart = Objects.requireNonNull(chart);
if (series == null || series.isEmpty()) {
throw new IllegalArgumentException("No chart data given");
}
this.series = FXCollections.observableArrayList(series);
restoreData();
final Rectangle zoomRect = new Rectangle();
zoomRect.setManaged(false);
zoomRect.setFill(Color.LIGHTSEAGREEN.deriveColor(0, 1, 1, 0.5));
chartParent.getChildren().add(zoomRect);
setUpZooming(zoomRect, chart);
}
/**
*
*
* Creates a new {@code ZoomManager}.
* </p>
* <b> Due to a bug, {@code series} must not be added to the chart! </b> If you
* do so, the chart will be emtpy.
* </p>
*
*
* @param chartParent
* the chart's parent {@link Pane}.
* @param chart
* the {@link XYChart} to which zooming functionality is added
* @param series
* array of chart data to display
* @throws IllegalArgumentException
* if chart data is empty or {@code null}
*/
public ZoomManager(final Pane chartParent, final XYChart<X, Y> chart, final Series<X, Y>... series) {
this(chartParent, chart, Arrays.asList(series));
}
private void doZoom(final boolean x, final Number n1, final Number n2) {
final double min = Math.min(n1.doubleValue(), n2.doubleValue());
final double max = Math.max(n1.doubleValue(), n2.doubleValue());
if (max - min > 1) {
zoomed = true;
final Iterator<XYChart.Series<X, Y>> it = chart.getData().iterator();
while (it.hasNext()) {
final XYChart.Series<X, Y> s = it.next();
final Iterator<XYChart.Data<X, Y>> it2 = s.getData().iterator();
while (it2.hasNext()) {
final XYChart.Data<X, Y> d = it2.next();
final Object value;
if (x) {
value = d.getXValue();
} else {
value = d.getYValue();
}
if (value instanceof Number) {
final Number n = (Number) value;
final double dd = n.doubleValue();
if (dd < min || dd > max) {
it2.remove();
} else {
}
}
if (s.getData().isEmpty()) {
it.remove();
}
}
}
} else {
// System.out.println("Skip tiny zoom");
}
}
private void doZoom(final boolean x, final Object o1, final Object o2) {
if (o1 instanceof Number && o2 instanceof Number) {
doZoom(x, (Number) o1, (Number) o2);
} else if (o1 instanceof String || o2 instanceof String) {
doZoom(x, (String) o1, (String) o2);
} else {
final int wait = 0;
}
}
private void doZoom(final boolean x, String s1, String s2) {
if (s1 == null && s2 == null) {
return;
}
if (s1 == null) {
s1 = s2;
}
if (s2 == null) {
s2 = s1;
}
final Iterator<XYChart.Series<X, Y>> it = chart.getData().iterator();
while (it.hasNext()) {
final XYChart.Series<X, Y> s = it.next();
final List<?> values;
if (x) {
values = extractXValues(s.getData());
} else {
values = extractYValues(s.getData());
}
final int index1 = values.indexOf(s1);
final int index2 = values.indexOf(s2);
final int lower = Math.min(index1, index2);
final int upper = Math.max(index1, index2);
final Iterator<Data<X, Y>> it2 = s.getData().iterator();
while (it2.hasNext()) {
final Data<X, Y> d = it2.next();
final Object value;
if (x) {
value = d.getXValue();
} else {
value = d.getYValue();
}
final int index = values.indexOf(value);
if (index != -1 && (index < lower || index > upper)) {
it2.remove();
}
}
}
}
private void restoreData() {
// make a tmp variable of data, since we will modify it but need to be
// able to restore
final ObservableList<XYChart.Series<X, Y>> backup2 = deepCopySeries(series);
chart.getData().setAll(backup2);
}
private void setUpZooming(final Rectangle rect, final XYChart<X, Y> chart) {
setUpZoomingRectangle(rect);
}
/**
* Displays a colored rectangle that will indicate zooming boundaries
*
* @param rect
*/
private void setUpZoomingRectangle(final Rectangle rect) {
/*
* The chart background. Needed to get the correct rectangle boundaries.
*/
final Node chartBackground = chart.lookup(".chart-plot-background");
final ObjectProperty<Point2D> mouseAnchor = new SimpleObjectProperty<>();
chart.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(final MouseEvent event) {
mouseAnchor.set(new Point2D(event.getX(), event.getY()));
}
});
chart.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(final MouseEvent event) {
if (event.getButton().equals(MouseButton.PRIMARY)) {
if (zoomed && event.getClickCount() == 2) {
restoreData();
zoomed = false;
event.consume();
}
}
}
});
chart.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(final MouseEvent event) {
final double x = event.getX();
final double y = event.getY();
rect.setX(Math.min(x, mouseAnchor.get().getX()));
rect.setY(Math.min(y, mouseAnchor.get().getY()));
rect.setWidth(Math.abs(x - mouseAnchor.get().getX()));
rect.setHeight(Math.abs(y - mouseAnchor.get().getY()));
}
});
chart.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(final MouseEvent event) {
// final Bounds bb = chartBackground.sceneToLocal(rect.getBoundsInLocal());
final Bounds bb = chartBackground.parentToLocal(rect.getBoundsInParent());
final double minx = bb.getMinX();
final double maxx = bb.getMaxX();
final double miny = bb.getMinY();
final double maxy = bb.getMaxY();
doZoom(true, chart.getXAxis().getValueForDisplay(minx), chart.getXAxis().getValueForDisplay(maxx));
doZoom(false, chart.getYAxis().getValueForDisplay(miny), chart.getYAxis().getValueForDisplay(maxy));
rect.setWidth(0);
rect.setHeight(0);
}
});
}
}