gpt4 book ai didi

charts - JavaFX 2.x : How to Remove XY Line Chart once plotted?

转载 作者:行者123 更新时间:2023-12-02 06:53:32 25 4
gpt4 key购买 nike

我在 XY 折线图上绘制了一个简单的系列,如下所示

public class SimpleXYLineChart extends Application {

@Override
public void start(Stage stage) {
stage.setTitle("Line plot");

final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis(1, 22, 0.5);

yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis){
@Override
public String toString(Number object){
return String.format("%7.2f", object);
}
});
final LineChart<String, Number>lineChart = new LineChart<String, Number>(xAxis, yAxis);

lineChart.setCreateSymbols(false);
lineChart.setAlternativeRowFillVisible(false);
lineChart.setLegendVisible(false);
lineChart.setTitle("LineChart");

XYChart.Series series1 = new XYChart.Series();

series1.getData().add(new XYChart.Data("Jan", 1));
series1.getData().add(new XYChart.Data("Feb", 1.5));
series1.getData().add(new XYChart.Data("Mar", 2));
series1.getData().add(new XYChart.Data("Apr", 2.5));
series1.getData().add(new XYChart.Data("May", 3));
series1.getData().add(new XYChart.Data("Jun", 4));
series1.getData().add(new XYChart.Data("Jul", 6));
series1.getData().add(new XYChart.Data("Aug", 9));
series1.getData().add(new XYChart.Data("Sep", 12));
series1.getData().add(new XYChart.Data("Oct", 15));
series1.getData().add(new XYChart.Data("Nov", 20));
series1.getData().add(new XYChart.Data("Dec", 22));

lineChart.getData().addAll(series1);

Scene scene = new Scene(new Group(), 800, 600);
final VBox vbox = new VBox();
final HBox hbox = new HBox();

final Button remove = new Button("Remove Series");
remove.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
if (!lineChart.getData().isEmpty()){
System.out.println("Remove Series");
lineChart.getData().remove((lineChart.getData().size()-1),0);
}
}
});

hbox.setSpacing(10);
hbox.getChildren().addAll(remove);

vbox.getChildren().addAll(lineChart, hbox);
hbox.setPadding(new Insets(10, 10, 10, 50));

((Group)scene.getRoot()).getChildren().add(vbox);
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(args);
}

}

我想删除绘制的线(通过单击按钮或标记复选框)并获得如下所示的空场景

enter image description here

我不知道如何删除绘制后的线条,没有找到任何 .remove() 或 .delete() 方法。

如何实现这一目标?

谢谢

最佳答案

问题现已解决,看来是JavaFX的一个bug。

我已经通过添加解决了

lineChart.setAnimated(false);一切都很好。在 JIRA Kenai 中,它已通过注释 lineChart.setCreateSymbols(false); 被提议为错误;

这也有效,但我认为这不是正确的答案,我可以使用 .setCreateSymbols(false) 或 (true) 设置 setAnimated(false);

关于charts - JavaFX 2.x : How to Remove XY Line Chart once plotted?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12093556/

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