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
| 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();
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();
userTrackX.set(valueX);
userTrackY.set(valueY);
}
private void handleTrackingMouseExit(final MouseEvent mouseEvent) {
userTrackX.set(0);
userTrackY.set(0);
}
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 ReadOnlyDoubleWrapper userTrackX = new ReadOnlyDoubleWrapper(this, "userTrackX");
public final Double getUserTrackX() {
return userTrackX.get();
}
public final ReadOnlyDoubleProperty userTrackXProperty() {
return userTrackX.getReadOnlyProperty();
}
private final ReadOnlyDoubleWrapper userTrackY = new ReadOnlyDoubleWrapper(this, "userTrackY");
public final Double getUserTrackY() {
return userTrackY.get();
}
public final ReadOnlyDoubleProperty userTrackYProperty() {
return userTrackY.getReadOnlyProperty();
}
private final ReadOnlyDoubleWrapper selectionX = new ReadOnlyDoubleWrapper(this, "selectionX");
public final Double getSelectionX() {
return selectionX.get();
}
public final ReadOnlyDoubleProperty selectionXProperty() {
return selectionX.getReadOnlyProperty();
}
} |
Partager