Javafx 2 javafx2.x:将鼠标单击坐标转换为XYChart轴值

Javafx 2 javafx2.x:将鼠标单击坐标转换为XYChart轴值,javafx-2,Javafx 2,在JavaFX2.x中,我使用的是XYChart,我想在鼠标在图表上移动时显示图表的(x,Y)轴坐标值。我在图表上设置了一个事件处理程序来处理setOnMouseMoved事件。但是,我不知道如何将MouseEvent的getX()值转换为图表的坐标值?用于确定鼠标在轴值坐标中的位置: xAxis.getValueForDisplay(mouseEvent.getX()), yAxis.getValueForDisplay(mouseEvent.getY()) 下面是一个示例,它报告鼠标悬停在

在JavaFX2.x中,我使用的是XYChart,我想在鼠标在图表上移动时显示图表的(x,Y)轴坐标值。我在图表上设置了一个事件处理程序来处理setOnMouseMoved事件。但是,我不知道如何将MouseEvent的getX()值转换为图表的坐标值?

用于确定鼠标在轴值坐标中的位置:

xAxis.getValueForDisplay(mouseEvent.getX()),
yAxis.getValueForDisplay(mouseEvent.getY())
下面是一个示例,它报告鼠标悬停在折线图上的坐标。屏幕截图不会捕获鼠标光标-您只需在那里想象它;-)

导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.event.EventHandler;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.*;
导入javafx.scene.chart.*;
导入javafx.scene.control.Label;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
带有HoverCoords的公共类LineChart扩展了应用程序{
@覆盖公共无效开始(阶段){
阶段。设置标题(“折线图样本”);
最终折线图折线图=createChart();
标签cursorCoords=createCursorGraphCoordsMonitorLabel(折线图);
第二阶段(
新景(
布局场景(
线条图,
草书
)
)
);
stage.show();
}
专用VBox布局场景(线条图、线条图、标签光标或坐标){
VBox布局=新的VBox(10);
布局。设置填充(新插图(10));
布局。设置对齐(位置中心);
layout.getChildren().setAll(
草书,
线条图
);
返回布局;
}
专用折线图createChart(){
最终数字axis xAxis=新数字axis();
最终数字axis yAxis=新数字axis();
xAxis.setLabel(“月数”);
最终折线图折线图=
新的线形图(xAxis、yAxis);
线形图.setTitle(“库存监控,2010年”);
XYChart.Series系列=新的XYChart.Series(
“我的投资组合”,FXCollections.observearraylist(
新XYChart.数据(1,23),
新XYChart.数据(2,14),
新XYChart.数据(3,15),
新XYChart.数据(4,24),
新XYChart.数据(5,34),
新XYChart.数据(6,36),
新XYChart.数据(7,22),
新XYChart.数据(8,45),
新XYChart.数据(9,43),
新XYChart.数据(10,17),
新XYChart.数据(11,29),
新XYChart.数据(12,25)
)
);
lineChart.getData().add(系列);
返回线图;
}
专用标签createCursorGraphCoordsMonitorLabel(折线图折线图){
最终轴xAxis=线形图。getXAxis();
最终轴yAxis=线形图。getYAxis();
最终标签cursorCoords=新标签();
最终节点chartBackground=lineChart.lookup(“.chart-plot-background”);
对于(节点n:chartBackground.getParent().getChildrenUnmodifiable()){
如果(n!=chartBackground&&n!=xAxis&&n!=yAxis){
n、 setMouseTransparent(真);
}
}
SetOnMouseCentered(新的EventHandler(){
@重写公共无效句柄(MouseEvent MouseEvent){
cursorCoords.setVisible(true);
}
});
setOnMouseMoved(新的EventHandler(){
@重写公共无效句柄(MouseEvent MouseEvent){
cursorCoords.setText(
字符串格式(
“(.2f,%.2f)”,
xAxis.getValueForDisplay(mouseEvent.getX()),
yAxis.getValueForDisplay(mouseEvent.getY())
)
);
}
});
setOnMouseExited(新的EventHandler(){
@重写公共无效句柄(MouseEvent MouseEvent){
cursorCoords.setVisible(false);
}
});
setOnMouseEntered(新的EventHandler(){
@重写公共无效句柄(MouseEvent MouseEvent){
cursorCoords.setVisible(true);
}
});
setOnMouseMoved(新的EventHandler(){
@重写公共无效句柄(MouseEvent MouseEvent){
cursorCoords.setText(
字符串格式(
“x=%.2f”,
getValueForDisplay(mouseEvent.getX())
)
);
}
});
setOnMouseExited(新的EventHandler(){
@重写公共无效句柄(MouseEvent MouseEvent){
cursorCoords.setVisible(false);
}
});
setOnMouseEntered(新的EventHandler(){
@重写公共无效句柄(MouseEvent MouseEvent){
cursorCoords.setVisible(true);
}
});
setOnMouseMoved(新的EventHandler(){
@重写公共无效句柄(MouseEvent MouseEvent){
cursorCoords.setText(
字符串格式(
“y=%.2f”,
yAxis.getValueForDisplay(mouseEvent.getY())
)
);
}
});
setOnMouseExited(新的EventHandler(){
@重写公共无效句柄(MouseEvent MouseEvent){
cursorCoords.setVisible(false);
}
});
返回光标或坐标;
}
公共静态void main(字符串[]args){
发射(args);
}
}

我想写一条评论,但不知道怎么写。这是一个额外的评论,这样下一个遇到问题的人就不会花几个小时来找出原因。
答案是正确的,但使用ImageCursor时要小心,因为它给出了错误的坐标。在禁用缩放、滚动、日期和所有自定义之后,我发现使用自定义光标会破坏“getValueForDisplay”的坐标

关于在图表背景区域上设置事件处理程序,需要注意的一点是,该区域元素不是图表数据的父元素,而是同级元素。在图表数据元素(或标签或路径)上发生的鼠标事件将不会(在筛选或冒泡步骤中)传递到此处理程序。
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.chart.*;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class LineChartWithHoverCoords extends Application {

  @Override public void start(Stage stage) {
    stage.setTitle("Line Chart Sample");

    final LineChart<Number, Number> lineChart = createChart();
    Label cursorCoords = createCursorGraphCoordsMonitorLabel(lineChart);

    stage.setScene(
      new Scene(
        layoutScene(
          lineChart, 
          cursorCoords
        )
      )
    );
    stage.show();
  }

  private VBox layoutScene(LineChart<Number, Number> lineChart, Label cursorCoords) {
    VBox layout = new VBox(10);
    layout.setPadding(new Insets(10));
    layout.setAlignment(Pos.CENTER);
    layout.getChildren().setAll(
      cursorCoords,
      lineChart
    );
    return layout;
  }

  private LineChart<Number, Number> createChart() {
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Number of Month");
    final LineChart<Number,Number> lineChart =
        new LineChart<>(xAxis,yAxis);

    lineChart.setTitle("Stock Monitoring, 2010");
    XYChart.Series<Number, Number> series = new XYChart.Series<>(
      "My portfolio", FXCollections.<XYChart.Data<Number, Number>>observableArrayList(
        new XYChart.Data<Number, Number>(1, 23),
        new XYChart.Data<Number, Number>(2, 14),
        new XYChart.Data<Number, Number>(3, 15),
        new XYChart.Data<Number, Number>(4, 24),
        new XYChart.Data<Number, Number>(5, 34),
        new XYChart.Data<Number, Number>(6, 36),
        new XYChart.Data<Number, Number>(7, 22),
        new XYChart.Data<Number, Number>(8, 45),
        new XYChart.Data<Number, Number>(9, 43),
        new XYChart.Data<Number, Number>(10, 17),
        new XYChart.Data<Number, Number>(11, 29),
        new XYChart.Data<Number, Number>(12, 25)
    )
    );

    lineChart.getData().add(series);
    return lineChart;
  }

  private Label createCursorGraphCoordsMonitorLabel(LineChart<Number, Number> lineChart) {
    final Axis<Number> xAxis = lineChart.getXAxis();
    final Axis<Number> yAxis = lineChart.getYAxis();

    final Label cursorCoords = new Label();

    final Node chartBackground = lineChart.lookup(".chart-plot-background");
    for (Node n: chartBackground.getParent().getChildrenUnmodifiable()) {
      if (n != chartBackground && n != xAxis && n != yAxis) {
        n.setMouseTransparent(true);
      }
    }

    chartBackground.setOnMouseEntered(new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent mouseEvent) {
        cursorCoords.setVisible(true);
      }
    });

    chartBackground.setOnMouseMoved(new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent mouseEvent) {
        cursorCoords.setText(
          String.format(
            "(%.2f, %.2f)",
            xAxis.getValueForDisplay(mouseEvent.getX()),
            yAxis.getValueForDisplay(mouseEvent.getY())
          )
        );
      }
    });

    chartBackground.setOnMouseExited(new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent mouseEvent) {
        cursorCoords.setVisible(false);
      }
    });

    xAxis.setOnMouseEntered(new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent mouseEvent) {
        cursorCoords.setVisible(true);
      }
    });

    xAxis.setOnMouseMoved(new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent mouseEvent) {
        cursorCoords.setText(
          String.format(
            "x = %.2f",
            xAxis.getValueForDisplay(mouseEvent.getX())
          )
        );
      }
    });

    xAxis.setOnMouseExited(new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent mouseEvent) {
        cursorCoords.setVisible(false);
      }
    });

    yAxis.setOnMouseEntered(new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent mouseEvent) {
        cursorCoords.setVisible(true);
      }
    });

    yAxis.setOnMouseMoved(new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent mouseEvent) {
        cursorCoords.setText(
          String.format(
            "y = %.2f",
            yAxis.getValueForDisplay(mouseEvent.getY())
          )
        );
      }
    });

    yAxis.setOnMouseExited(new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent mouseEvent) {
        cursorCoords.setVisible(false);
      }
    });

    return cursorCoords;
  }

  public static void main(String[] args) {
    launch(args);
  }
}