这是我的控制器Java类,其中动态绘制具有多个系列和多个值的折线图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();

最佳答案

我已经看过您以前的question,对此我在下面的答案中发表。

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

更改generateChart()方法,并在Performance.java类中创建HoveredThresholdNodea类,如下所示:

@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;
    }
}


此代码的输出是:

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

关于java - 将鼠标悬停在javafx中的折线图点上时如何显示值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44536365/

10-09 05:31