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

时间:2017-06-14 05:44:42

标签: java css javafx javafx-2 javafx-8

这是我的控制器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();

1 个答案:

答案 0 :(得分:1)

我已经看到了您之前的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;
    }
}

此代码的输出为:

enter image description here