将鼠标悬停在 javafx 中的折线图点上时如何显示值?

How can I display value while hovering on the line chart points in javafx?

这是我的控制器 java class 我正在绘制具有多个系列和多个值的折线图动态 X 类别轴,Y 是数字轴,我想在悬停时显示这些值点。我有多个系列和多个值,我该怎么做

Stage stage = new Stage();

stage.setTitle("Performance Analysis");

final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();

    xAxis.setLabel("Values");
    //yAxis.setLabel("Engines");

    LineChart<String, Number> lineChart = new LineChart<String,Number>(xAxis,yAxis);

    Scene scene  = new Scene(lineChart,800,600); 

    lineChart.setTitle("Performance Analysis");

    lineChart.setCursor(Cursor.CROSSHAIR);

     ParameterSelectionController psc = new ParameterSelectionController();


     performance performvalues = new performance(lineChart, refined, collection, al4); //this is another class which in parameter

     performvalues.generateChart();

     stage.setScene(scene);
     stage.show();

我看过你之前的,关于这个我有post下面的答案。

this 示例相同,您可以创建 chart.XYChart.Data 对象并使用 HoveredThresholdNodea class 构造函数以这种方式在特定点设置节点

更改 generateChart() 方法并在 performance.java class 中创建 HoveredThresholdNodea class 为:

@SuppressWarnings({ "unchecked", "rawtypes" })
public void generateChart() {
    for (int i = 0; i < engines.size(); i++) {
        XYChart.Series series = new XYChart.Series();
        series.setName(engines.get(i).toString());
        for (int j = 0; j < parameters.size(); j++) {
            final Data<String, Object> data = new XYChart.Data<>(parameters.get(j).toString(), param.get(i).get(j));
            data.setNode(new HoveredThresholdNodea(engines.get(i).toString(), param.get(i).get(j)));
            series.getData().add(data);
        }
        lineChart.getData().add(series);
        System.out.println(lineChart);
    }
}

class HoveredThresholdNodea extends StackPane {

    public HoveredThresholdNodea(String string, Object object) {
        setPrefSize(15, 15);

        final Label label = createDataThresholdLabel(string, object);

        setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                getChildren().setAll(label);
                setCursor(Cursor.NONE);
                toFront();
            }
        });
        setOnMouseExited(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                getChildren().clear();
            }
        });
    }

    private Label createDataThresholdLabel(String string, Object object) {
        final Label label = new Label(object + "");
        label.getStyleClass().addAll("default-color0", "chart-line-symbol", "chart-series-line");
        label.setStyle("-fx-font-size: 20; -fx-font-weight: bold;");

        System.out.println(string);
        if (string.equals("engine1")) {
            label.setTextFill(Color.RED);
            label.setStyle("-fx-border-color: RED;");
        } else if (string.equals("engine2")) {
            label.setTextFill(Color.ORANGE);
            label.setStyle("-fx-border-color: ORANGE;");
        } else {
            label.setTextFill(Color.GREEN);
            label.setStyle("-fx-border-color: GREEN;");
        }

        label.setMinSize(Label.USE_PREF_SIZE, Label.USE_PREF_SIZE);
        return label;
    }
}

这段代码的输出是: