Creating a Pie chart

This chart shows only one dataset as the pie's slices. The bigger a corresponding value, the bigger its slice:

// chapter10/chart/PieChartDemo.java
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Luck", 10),
new PieChart.Data("Skill", 30),
new PieChart.Data("Power of Will", 15),
new PieChart.Data("Pleasure", 5),
new PieChart.Data("Pain", 40));

The JavaFX PieChart for this looks as follows:

Note that numbers in PieChart.Data are not always percents—the total sum is calculated on each new element and each number represents a share of this total sum.

Additionally, all you need to create PieChart is to choose the title and legend options:

PieChart chart = new PieChart(data);
chart.setTitle("Success");
chart.setLegendSide(Side.LEFT);

stage.setScene(new Scene(chart, 530, 400));
stage.setTitle("Pie Chart Demo");
stage.show();

Note that the chart will automatically resize, doing its best to adapt to the new size:

And it can be animated; try this code to see an animation:

chart.setOnMouseClicked((e)-> {
pieChartData.add(new PieChart.Data("Stuff",10));
});

The legend is not that useful in our example, so you can either hide it or the chart labels using the PieChart.setLabelsVisible(boolean value) and PieChart.setLegendVisible(boolean value) methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.15.161