How to clear text added in a javafx barchart?

后端 未结 3 1723
既然无缘
既然无缘 2021-01-16 07:02

I add some text at the top of bars (the value of each bar). It\'s working but the problem is I want to remove this text each time I update the chart. In fact, text stays aft

3条回答
  •  甜味超标
    2021-01-16 07:52

    I have found only one solution, which is to clear every parentNode of each Data when you receive an update. You'll reconstruct the whole series then. As follow, it works but I didn't check the performance. On my environment it's pretty fast :

    ObservableList> allSeries = yourBarChart.getData();
        if (updateReceived) {
            for (XYChart.Series series : allSeries) {
                for (XYChart.Data data : series.getData()) {
                    Node node = data.getNode();
                    Parent parent = node.parentProperty().get();
                    if (parent != null && parent instanceof Group) {
                        Group group = (Group) parent;
                        group.getChildren().clear();
                    }
                }
            }
            allSeries.clear();
        }
    

提交回复
热议问题