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
| package DataTraitment;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableObjectValue;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.chart.Axis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Region;
import javafx.scene.shape.Line;
public class TrackLineChart<X extends Number, Y extends Number> extends LineChart<X,Y>{
private final Region userTrackArea = new Region();
private final Line lineUserTrackX = new Line();
private final Line lineUserTrackY = new Line();
private ObservableObjectValue<Double> userTrackXProperty;
private ObservableObjectValue<Double> userTrackYProperty;
@SuppressWarnings({ "rawtypes", "unchecked" })
public TrackLineChart(Axis xAxis, Axis yAxis) {
super(xAxis, yAxis);
userTrackXProperty().addListener(observable -> requestChartLayout());
userTrackYProperty().addListener(observable -> requestChartLayout());
userTrackArea.setId("userTrackArea");
userTrackArea.mouseTransparentProperty().bind(trackUserProperty().not());
userTrackArea.setOnMouseMoved(this::handleMouseTrackingMoved);
userTrackArea.setOnMouseExited(this::handleTrackingMouseExit);
userTrackArea.setOnMouseClicked(this::handleMouseTrackingClicked);
lineUserTrackX.setId("lineUserTrackX");
lineUserTrackX.visibleProperty().bind(trackUserProperty());
lineUserTrackY.setId("lineUserTrackY");
lineUserTrackY.visibleProperty().bind(trackUserProperty());
getPlotChildren().addAll(lineUserTrackX, lineUserTrackY, userTrackArea);
}
private void handleMouseTrackingMoved(final MouseEvent mouseEvent) {
final NumberAxis xAxis = (NumberAxis) getXAxis();
final NumberAxis yAxis = (NumberAxis) getYAxis();
final double mouseX = mouseEvent.getX();
final double mouseY = mouseEvent.getY();
final double valueX = xAxis.getValueForDisplay(mouseX).doubleValue();
final double valueY = yAxis.getValueForDisplay(mouseY).doubleValue();
// System.out.printf("Mouse %f %f -> value %f (%f %f) %f (%f %f)", mouseX, mouseY,
// valueX, xAxis.getLowerBound(), xAxis.getUpperBound(),
// valueY, yAxis.getLowerBound(), yAxis.getUpperBound()).println();
lineUserTrackX.visibleProperty().bind(trackUserProperty().and(Bindings.isNotNull(userTrackXProperty)));
lineUserTrackY.visibleProperty().bind(trackUserProperty().and(Bindings.isNotNull(userTrackYProperty)));
}
private void handleTrackingMouseExit(final MouseEvent mouseEvent) {
userTrackX.set(null);
userTrackY.set(null);
}
private void handleMouseTrackingClicked(final MouseEvent mouseEvent) {
final double mouseX = mouseEvent.getX();
final NumberAxis xAxis = (NumberAxis) getXAxis();
final double valueX = xAxis.getValueForDisplay(mouseX).doubleValue();
selectionX.set(valueX);
}
////////////////////////////////////////////////////////////////////////////
@Override
protected void layoutPlotChildren() {
super.layoutPlotChildren();
final NumberAxis xAxis = (NumberAxis) getXAxis();
final NumberAxis yAxis = (NumberAxis) getYAxis();
lineUserTrackX.toFront();
lineUserTrackY.toFront();
userTrackArea.toFront();
final Double userTrackXObj = getUserTrackX();
final double userTrackX = userTrackXObj == null ? 0 : userTrackXObj.doubleValue();
final Double userTrackYObj = getUserTrackY();
final double userTrackY = userTrackYObj == null ? 0 : userTrackYObj.doubleValue();
// System.out.printf("User %f %f", userTrackX, userTrackY).println();
final double x = xAxis.getDisplayPosition(userTrackX);
final double y = yAxis.getDisplayPosition(userTrackY);
final double x1 = xAxis.getDisplayPosition(xAxis.getLowerBound());
final double y1 = yAxis.getDisplayPosition(yAxis.getLowerBound());
final double x2 = xAxis.getDisplayPosition(xAxis.getUpperBound());
final double y2 = yAxis.getDisplayPosition(yAxis.getUpperBound());
//
final double screenOffset = 0.5;
//
layoutInArea(userTrackArea, x1, y2, x2 - x1, y1 - y2, 0, HPos.LEFT, VPos.TOP);
//
lineUserTrackX.setStartX(x + screenOffset);
lineUserTrackX.setStartY(y1 + screenOffset);
lineUserTrackX.setEndX(x + screenOffset);
lineUserTrackX.setEndY(y2 + screenOffset);
//
lineUserTrackY.setStartX(x1 + screenOffset);
lineUserTrackY.setStartY(y + screenOffset);
lineUserTrackY.setEndX(x2);
lineUserTrackY.setEndY(y);
}
////////////////////////////////////////////////////////////////////////////
private final BooleanProperty trackUser = new SimpleBooleanProperty(this, "trackUser", true);
public final boolean isTrackUser() {
return trackUser.get();
}
public final void setTrackUser(final boolean value) {
trackUser.set(value);
}
public final BooleanProperty trackUserProperty() {
return trackUser;
}
private final ReadOnlyObjectWrapper<Double> userTrackX = new ReadOnlyObjectWrapper<Double>(this, "userTrackX");
public final Double getUserTrackX() {
return userTrackX.get();
}
private ReadOnlyObjectProperty<Double> userTrackYProperty() {
return userTrackY.getReadOnlyProperty();
}
private final ReadOnlyObjectWrapper<Double> userTrackY = new ReadOnlyObjectWrapper<Double>(this, "userTrackY");
public final Double getUserTrackY() {
return userTrackY.get();
}
private ReadOnlyObjectProperty<Double> userTrackXProperty() {
return userTrackX.getReadOnlyProperty();
}
private final ReadOnlyObjectWrapper<Double> selectionX = new ReadOnlyObjectWrapper<Double>(this, "selectionXProperty");
public final Double getSelectionX() {
return selectionX.get();
}
public final ReadOnlyObjectProperty<Double> selectionXProperty() {
return selectionX.getReadOnlyProperty();
}
} |
Partager