简体   繁体   中英

JavaFX - Add new PieChart.Data to existing Pie Chart

I am curious how to add something to an already existing PieChart in JavaFx(i think im using 2.2.25, but i could update if it helps and if there is a newer version).

For instance:

ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(new PieChart.Data("HD 7990", 65), new PieChart.Data("GTX 690", 35));

Now i want to 'append' another 'piece' to the cake, how to do that? (btw i am using FXML from Scene Builder)

(Already tried this but it did not work(shortened version):

oldchart = pieChartData.getData();
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(oldchart, new PieChart.Data("GTX 690", 35));

)

Thanks :D

Just do

pieChartData.add(new PieChart.Data("GTX 690", 35));

To remove last added one

pieChartData.remove(pieChartData.size() - 1);

To clear all 'pieces'

pieChartData.clear();

Since ,as you noticed, pieChartData is not an java.util.ArrayList but an javafx.collections.ObservableList , any changes made to pieChartData collection list will be reflected to the PieChart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM