gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:34:59 25 4
gpt4 key购买 nike

这是我的 Controller 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;
}
}

这段代码的输出是:

enter image description here

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com