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.

Mobilesales Pie Chart

Pie Chart in JavaFX

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.

  • 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.

  • 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.

To generate a pie chart, Prepare an ObservableList object. After preparing the ObservableList object, pass it as an argument to the constructor of the class PieChart; Or, by using the method named setData().

Steps to Generate Pie Chart

To generate a PieChart in JavaFX, follow the steps given below.

Step 1: Creating a Class

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 {     
   }    
}

Step 2: Preparing the ObservableList Object

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));

Step 3: Creating a PieChart Object

Create a PieChart by passing the ObservableList object as shown below.

//Creating a Pie chart 
PieChart pieChart = new PieChart(pieChartData);

Step 4: Setting the Title of the Pie Chart

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");

Step 5: Setting the Slices Clockwise

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);

Step 6: Set the Length of the Label Line

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);

Step 7: Set the Labels Visible

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);

Step 8: Set the Start Angle of the Pie Chart

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); 

Step 9: Creating a Group Object

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);

Step 10: Launching Application

Lastly, follow the given steps below to launch the application properly −

  • Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters.

  • Then, set the title to the stage using the setTitle() method of the Stage class.

  • Now, a Scene object is added to the stage using the setScene() method of the class named Stage.

  • Display the contents of the scene using the method named show().

  • Lastly, the application is launched with the help of the launch() method.

Example

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).

S.No Mobile Brands Sales (Units per day)
1 Iphone 5S 20
2 Samsung Grand 20
3 MOTO G 40
4 Nokia Lumia 10

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 --module-path %PATH_TO_FX% --add-modules javafx.controls PieChartExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls PieChartExample

Output

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

Pie Chart

Example

Let us see another example to draw a JavaFX pie chart illustrating monthly expenses of a private employee whose salary is 25,000 INR per month. Save the file under the name PieChartEmployee.java.

S.No Necessities Expenses
1 Rent 7500
2 Groceries 1000
3 Transport 1500
4 Savings 10000
5 Miscellaneous 5000
 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 PieChartEmployee extends Application {  
   @Override 
   public void start(Stage stage) { 
      //Preparing ObservbleList object         
      ObservableList pieChartData = FXCollections.observableArrayList(
         new PieChart.Data("Rent", 7500), 
         new PieChart.Data("Groceries", 1000), 
         new PieChart.Data("Transport", 1500), 
         new PieChart.Data("Savings", 10000),
		 new PieChart.Data("Miscellaneous", 5000)); 
       
      //Creating a Pie chart 
      PieChart pieChart = new PieChart(pieChartData); 
              
      //Setting the title of the Pie chart 
      pieChart.setTitle("Monthly Expenses"); 
       
      //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 --module-path %PATH_TO_FX% --add-modules javafx.controls PieChartEmployee.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls PieChartEmployee

Output

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

Pie Chart
Advertisements