com.silicosciences.javafx.charts.zooming.ZoomManager Maven / Gradle / Ivy
The newest version!
/**
* 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.silicosciences.javafx.charts.zooming;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
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 {
static ObservableList> deepCopySeries(final Collection> data) {
final ObservableList> backup = FXCollections.observableArrayList();
for (final Series s : data) {
backup.add(deepCopySeries(s));
}
return backup;
}
static XYChart.Series deepCopySeries(final XYChart.Series series) {
final XYChart.Series result = new XYChart.Series();
result.setName(series.getName());
result.setData(deepCopySeriesData(series.getData()));
return result;
}
static ObservableList> deepCopySeriesData(
final Collection extends XYChart.Data> data) {
final ObservableList> result = FXCollections.observableArrayList();
for (final Data i : data) {
result.add(new Data(i.getXValue(), i.getYValue()));
}
return result;
}
static ObservableList extractXValues(final ObservableList> data) {
final ObservableList result = FXCollections.observableArrayList();
for (final Data d : data) {
result.add(d.getXValue());
}
return result;
}
static ObservableList extractYValues(final ObservableList> data) {
final ObservableList result = FXCollections.observableArrayList();
for (final Data 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> series;
private final XYChart chart;
private volatile boolean zoomed;
public ZoomManager(final Pane chartParent, final XYChart chart,
final Collection extends Series> series) {
super();
this.chart = chart;
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);
}
public ZoomManager(final Pane chartParent, final XYChart chart, final Series... 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> it = chart.getData().iterator();
while (it.hasNext()) {
final XYChart.Series s = it.next();
final Iterator> it2 = s.getData().iterator();
while (it2.hasNext()) {
final XYChart.Data 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> it = chart.getData().iterator();
while (it.hasNext()) {
final XYChart.Series 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> it2 = s.getData().iterator();
while (it2.hasNext()) {
final Data 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 synchronized void restoreData() {
// make a tmp variable of data, since we will modify it but need to be
// able to restore
final ObservableList> backup2 = deepCopySeries(series);
chart.getData().setAll(backup2);
}
private void setUpZooming(final Rectangle rect, final XYChart chart) {
setUpZoomingRectangle(rect);
}
/**
* Displays a colored rectangle that will indicate zooming boundaries
*
* @param rect
*/
private void setUpZoomingRectangle(final Rectangle rect) {
final Node chartBackground = chart.lookup(".chart-plot-background");
final ObjectProperty mouseAnchor = new SimpleObjectProperty<>();
chart.setOnMousePressed(new EventHandler() {
@Override
public void handle(final MouseEvent event) {
mouseAnchor.set(new Point2D(event.getX(), event.getY()));
}
});
chart.setOnMouseClicked(new EventHandler() {
@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() {
@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() {
@Override
public void handle(final MouseEvent event) {
final Bounds bb = chartBackground.sceneToLocal(rect.getBoundsInLocal());
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);
}
});
}
}