如何计算鼠标相对于折线图原点的位置?
下面的代码获取鼠标光标相对于图表布局原点的 x 位置。我想知道鼠标相对于笛卡尔原点的位置。
public class Chart extends Application {
private NumberAxis xAxis;
private NumberAxis yAxis;
private LineChart<Number,Number> lineChart;
private Label cursorPosition;
private Label xAxisPosition;
@Override
public void start(Stage primaryStage) {
VBox root = new VBox();
xAxis = new NumberAxis("Date", 0.f, 100.f, 10.f);
yAxis = new NumberAxis("Value", 0.f, 100.f, 10.f);
lineChart = new LineChart<>(xAxis, yAxis);
Series series = new Series();
for (int ii = 1; ii <= 100; ii++) {
series.getData().add(new Data(ii, Math.random()*20.));
}
lineChart.getData().add(series);
lineChart.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
lineChart.setCursor(Cursor.CROSSHAIR);
cursorPosition.setText(String.valueOf(event.getX()));
}
});
lineChart.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
lineChart.setCursor(Cursor.DEFAULT);
}
});
cursorPosition = new Label();
root.getChildren().addAll(lineChart, cursorPosition,);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}