我有一个带有月份与值的 JavaFXline 图表。每次单击图表时,我都试图在控制台中打印月份和折线图中的值。问题是,当我getValueForDisplay在CategoryAxis对象中调用该方法时,它会返回null。
当我单击 (x,y):jan,3
我想打印时:
X value = jan Y value: 3
但程序正在打印:
X value = **null** Y value: 2.7796610169491527  
另一个问题; 
2)为什么在尝试打印坐标时它似乎没有校准(如果我点击 3 程序打印2.779661016949152?您会注意到这个问题检查控制台上打印的值。
这是我正在使用的代码:
public class LineChartTest extends Application {
String xValue;
Number yValue;
private void init(Stage primaryStage) {
    Group root = new Group();
    primaryStage.setScene(new Scene(root));
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis("Values for Y-Axis", 0, 3, 1);
    yAxis.setUpperBound(5);
    ObservableList<XYChart.Series<String,Double>> lineChartData = FXCollections.observableArrayList(
        new LineChart.Series<String,Double>("en", FXCollections.observableArrayList(
            new XYChart.Data<String,Double>("jan", 1.0),
            new XYChart.Data<String,Double>("feb", 2.0),
            new XYChart.Data<String,Double>("mar", 3.0),
            new XYChart.Data<String,Double>("apr", 4.0),
            new XYChart.Data<String,Double>("may", 0.5)
        )),
        new LineChart.Series<String,Double>("to", FXCollections.observableArrayList(
            new XYChart.Data<String,Double>("jan", 1.6),
            new XYChart.Data<String,Double>("feb", 0.4),
            new XYChart.Data<String,Double>("mar", 2.9),
            new XYChart.Data<String,Double>("apr", 1.3),
            new XYChart.Data<String,Double>("may", 0.9)
        ))
    );
    //define a new line
    final Series<String,Double> otherSeries = new Series<String,Double>();
    //set points
    otherSeries.getData().add(new XYChart.Data("jan",3));
    otherSeries.getData().add(new XYChart.Data("feb",2));
    otherSeries.getData().add(new XYChart.Data("mar",4));
    //set name to the line
    otherSeries.setName("tre");
    //add the new line to the graph
    lineChartData.add(otherSeries);
    final LineChart chart = new LineChart(xAxis, yAxis, lineChartData);
    //set title to the figure
    chart.setTitle("Line Chart Test");
    root.getChildren().add(chart);
    chart.setOnMouseClicked(new EventHandler<MouseEvent>() {
          @Override
        public void handle(MouseEvent t) {
            //Why this seems to be not working properly?
            double sceneX = t.getSceneX();
            double sceneY = t.getSceneY();
            //gives the point of the graph
            xValue = xAxis.getValueForDisplay(sceneX);
            yValue = yAxis.getValueForDisplay(sceneY);
            System.out.println("sceneX-25: "+ sceneX);
            System.out.println("getDisplayPosition: " + xAxis.getValueForDisplay(sceneX));
            //Test
            System.out.println(" X value = " +  
            xValue + " Y value: " + yValue );  
            }
    });
}
@Override public void start(Stage primaryStage) throws Exception {
    init(primaryStage);
    primaryStage.show();
}
    public static void main(String[] args) {
        launch(args);
    }
}
回答:
感谢 Sergey Grinev 和 Uluk Biy
在前面的代码中你必须修改:
折线图代码:
    chart.setOnMouseClicked(new EventHandler<MouseEvent>() {
          @Override
          public void handle(MouseEvent e) {
            Node chartPlotBackground = chart.lookup(".chart-plot-background");
            final double shiftX = xSceneShift(chartPlotBackground);
            final double shiftY = ySceneShift(chartPlotBackground);    
            double x = e.getSceneX() - shiftX;
            double y = e.getSceneY() - shiftY;
            xValue = xAxis.getValueForDisplay(x);
            yValue = yAxis.getValueForDisplay(y );
            System.out.println("shiftX = " + shiftX + " shiftY: " + shiftY);
            System.out.println("X value = "
                + xValue + " \nY value: " + yValue);
          }
    });
}
//recursive calls    
private double xSceneShift(Node node) {
return node.getParent() == null ? 0 : node.getBoundsInParent().getMinX() +    xSceneShift(node.getParent());
}
private double ySceneShift(Node node) {
return node.getParent() == null ? 0 : node.getBoundsInParent().getMinY() + ySceneShift(node.getParent());
}
    @Override 
    public void start(Stage primaryStage) throws Exception {
    init(primaryStage);
    primaryStage.show();
}
public static void main(String[] args) {
    launch(args);
    }
}
在CategoryAxis您必须修改的源代码中(查看评论!):
 @Override public String getValueForDisplay(double displayPosition) {
    if (getSide().equals(Side.TOP) || getSide().equals(Side.BOTTOM)) { // HORIZONTAL
        if (displayPosition < 0 || displayPosition > getHeight()) return null; //   <-------------- WRONG SHOULD BE displayPosition > getWidth()
        double d = (displayPosition - firstCategoryPos.get()) / categorySpacing.get();
        return toRealValue(d);
    } else { // VERTICAL
        if (displayPosition < 0 || displayPosition > getWidth()) return null; // <-------------- WRONG SHOULD BE displayPosition > getHeight()
        double d = (displayPosition - firstCategoryPos.get()) / (categorySpacing.get() * -1);
        return toRealValue(d);
    }
} 
你可以在这里找到源代码: