JavaFX - Pie Chart


Advertisements


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

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

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: Creating a Scene Object

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

Step 11: Setting the Title of the Stage

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

Step 12: Adding Scene to the Stage

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

Step 13: Displaying the Contents of the Stage

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

primaryStage.show();

Step 14: Launching the Application

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

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 PieChartExample.java 
java PieChartExample

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

Pie Chart
javafx_charts.htm

Advertisements