JavaFX - Pie Chart

A pie-chart is a representation of values as slices of a circle with different colors. These slices are labeled and the values corresponding to each slice is represented in the chart.
Following is a Pie Chart depicting the mobile sales of various companies at an instance.
In JavaFX, a pie chart is represented by a class named PieChart. This class belongs to the package javafx.scene.chart.
By instantiating this class, you can create a PieChart node in JavaFX.
This class has 5 properties which are as follows −
clockwise − This is a Boolean Operator; on setting this operator true, the data slices in the pie charts will be arranged clockwise starting from the start angle of the pie chart.
clockwise − This is a Boolean Operator; on setting this operator true, the data slices in the pie charts will be arranged clockwise starting from the start angle of the pie chart.
data − This represents an ObservableList object, which holds the data of the pie chart.
data − This represents an ObservableList object, which holds the data of the pie chart.
labelLineLength − An integer operator representing the length of the lines connecting the labels and the slices of the pie chart.
labelLineLength − An integer operator representing the length of the lines connecting the labels and the slices of the pie chart.
labelsVisible − This is a Boolean Operator; on setting this operator true, the labels for the pie charts will be drawn. By default, this operator is set to be true.
labelsVisible − This is a Boolean Operator; on setting this operator true, the labels for the pie charts will be drawn. By default, this operator is set to be true.
startAngle − This is a double type operator, which represents the angle to start the first pie slice at.
startAngle − This is a double type operator, which represents the angle to start the first pie slice at.
To generate a pie chart, Prepare an ObservableList object as shown in the following code block −
//Preparing ObservbleList object         
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList( 
   new PieChart.Data("Iphone 5S", 13), 
   new PieChart.Data("Samsung Grand", 25), 
   new PieChart.Data("MOTO G", 10), 
   new PieChart.Data("Nokia Lumia", 22)); 

After preparing the ObservableList object, pass it as an argument to the constructor of the class PieChart as follows −
//Creating a Pie chart 
PieChart pieChart = new PieChart(pieChartData);

Or, by using the method named setData() of the class named PieChart of the package named javafx.scene.chart.
pieChart.setData(pieChartData);

To generate a PieChart in JavaFX, follow the steps given below.
Create a Java class and inherit the Application class of the package javafx.application and implement the start() method of this class as follows.
public class ClassName extends Application {  
   @Override     
   public void start(Stage primaryStage) throws Exception {     
   }    
}
Prepare an object of the interface ObservableList object by passing the data of the pie chart as shown below −
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList( 
   new PieChart.Data("Iphone 5S", 13), 
   new PieChart.Data("Samsung Grand", 25), 
   new PieChart.Data("MOTO G", 10), 
   new PieChart.Data("Nokia Lumia", 22));

Create a PieChart by passing the ObservableList object as shown below.
//Creating a Pie chart 
PieChart pieChart = new PieChart(pieChartData);

Set the title of the Pie Chart using the setTitle() method of the class PieChart. This belongs to the package javafx.scene.chart −
//Setting the title of the Pie chart 
pieChart.setTitle("Mobile Sales");

Set the slices of the Pie Charts clockwise. This is done by passing Boolean value true to the setClockwise() method of the class PieChart. This belongs to the package javafx.scene.chart −
//setting the direction to arrange the data 
pieChart.setClockwise(true);

Set the length of the label line using the setLabelLineLength() method of the class PieChart which belongs to the package javafx.scene.chart, as follows −
//Setting the length of the label line 
pieChart.setLabelLineLength(50);

Set the labels of the pie chart to visible by passing the Boolean value true to the method setLabelsVisible() of the class PieChart. This belongs to the package javafx.scene.chart −
//Setting the labels of the pie chart visible  
pieChart.setLabelsVisible(true);

Set the Start angle of the pie chart using the setStartAngle() method of the class PieChart. This belongs to the package javafx.scene.chart −
//Setting the start angle of the pie chart 
pieChart.setStartAngle(180); 

In the start() method, create a group object by instantiating the class named Group. This belongs to the package javafx.scene.
Pass the PieChart (node) object, created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows −
Group root = new Group(piechart);

Create a Scene by instantiating the class named Scene, which belongs to the package javafx.scene. To this class, pass the Group object (root) created in the previous step.
In addition to the root object, you can also pass two double parameters representing height and width of the screen, along with the object of the Group class as shown below.
Scene scene = new Scene(group ,600, 300);

You can set the title to the stage using the setTitle() method of the Stage class. The primaryStage is a Stage Object, which is passed to the start method of the scene class as a parameter.
Using the primaryStage object, set the title of the scene as Sample Application as follows.
primaryStage.setTitle("Sample Application");

You can add a Scene object to the stage using the method setScene() of the class named Stage. Add the Scene object prepared in the previous steps using this method as shown below.
primaryStage.setScene(scene);

Display the contents of the scene using the method named show() of the Stage class as follows.
primaryStage.show();

Launch the JavaFX application by calling the static method launch() of the Application class from the main method as follows.
public static void main(String args[]){   
   launch(args);      
}   
The table given below depicts mobile sale with the help of a pie chart. The following table has a list of different mobile brands and their sale (units per day).
Following is a Java program which generates a pie chart, depicting the above data using JavaFX. Save this code in a file with the name PieChartExample.java.
import javafx.application.Application; 
import javafx.collections.FXCollections;  
import javafx.collections.ObservableList; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.chart.PieChart; 
         
public class PieChartExample extends Application {  
   @Override 
   public void start(Stage stage) { 
      //Preparing ObservbleList object         
      ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
         new PieChart.Data("Iphone 5S", 13), 
         new PieChart.Data("Samsung Grand", 25), 
         new PieChart.Data("MOTO G", 10), 
         new PieChart.Data("Nokia Lumia", 22)); 
       
      //Creating a Pie chart 
      PieChart pieChart = new PieChart(pieChartData); 
              
      //Setting the title of the Pie chart 
      pieChart.setTitle("Mobile Sales"); 
       
      //setting the direction to arrange the data 
      pieChart.setClockwise(true); 
       
      //Setting the length of the label line 
      pieChart.setLabelLineLength(50); 

      //Setting the labels of the pie chart visible  
      pieChart.setLabelsVisible(true); 
       
      //Setting the start angle of the pie chart  
      pieChart.setStartAngle(180);     
         
      //Creating a Group object  
      Group root = new Group(pieChart); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 400);  
      
      //Setting title to the Stage 
      stage.setTitle("Pie chart"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show();         
   }     
   public static void main(String args[]){ 
      launch(args); 
   } 
} 
Compile and execute the saved java file from the command prompt using the following commands.
javac PieChartExample.java 
java PieChartExample

On executing, the above program generates a JavaFX window displaying a pie chart as shown below.

    33 Lectures 
     7.5 hours 

 Syed Raza

    64 Lectures 
     12.5 hours 

 Emenwa Global, Ejike IfeanyiChukwu

    20 Lectures 
     4 hours 

 Emenwa Global, Ejike IfeanyiChukwu
 Print
 Add Notes
 Bookmark this page