How to create a Pie chart using JavaFX?

JavaFXObject Oriented ProgrammingProgramming

In the pie chart, we represent the data values as slices of a circle. Each slice is differentiated from other (typically by color). In JavaFX, you can create a pie chart by instantiating the javafx.scene.chart.PieChart class.

This class provides various properties, by setting values to them using their respective setter methods, you can customize the pie chart.

  • The slices of the pie chart are placed clockwise (from the start angle) by default. You can arrange them anti-clockwise by setting the clockwise property to false, using the setClockwise() method.

  • Each slice is associated with a label. (name of the slice as value) By default, these labels are visible. You can make them invisible by setting the labels visible property to false using the setLabelsVisible() method.

  • Each label has a line pointing out from the respective slice to it. You can set the length of this line using the setLabelLineLength() method.

  • The PieChart class contains an observable list of type (PieChart.Data) that holds the data items (for slices). You can set the value to a pie chart using the setData() method. This method accepts an object of the type ObservableList<PieChart.Data> as parameter.

Setting data to the pie chart

The class PieChart.Data represents a slice in the pie chart you can create a slice (data item) by instantiating it. The constructor of this class accepts the name of the slice(String) and its value as parameters.

To set data to the pie chart, create all the required slices by instantiating this class, create an observable list using the created objects as −

PieChart.Data obj1 = new PieChart.Data(name, value);
PieChart.Data obj2 = new PieChart.Data(name, value);…
…
ObservableList<PieChart.Data> data = FXCollections.observableArrayList(onj1, obj2,
obj3, etc….);

Then, set the list as a value to the data property using the setData() method.

Example

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.chart.PieChart;
import javafx.scene.layout.StackPane;
public class PieChartExample extends Application {
   public void start(Stage stage) {
      //Creating a Pie chart
      PieChart pieChart = new PieChart();
      //Setting data
      ObservableList<PieChart.Data> data = FXCollections.observableArrayList(
         new PieChart.Data("Work", 10),
         new PieChart.Data("Chores", 2),
         new PieChart.Data("Sleep", 8),
         new PieChart.Data("Others", 4)
      );
      pieChart.setData(data);
      //Setting the other properties
      pieChart.setTitle("Activities");
      pieChart.setClockwise(true);
      pieChart.setLabelLineLength(10);
      pieChart.setLabelsVisible(true);
      pieChart.setStartAngle(360);
      //Creating a stack pane to hold the pie chart
      StackPane pane = new StackPane(pieChart);
      //Setting the Scene
      Scene scene = new Scene(pane, 600, 350);
      stage.setTitle("Pie chart");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

raja
Published on 19-May-2020 16:00:38
Advertisements