Table of contents
1.
Introduction💁
2.
JavaFX
3.
PieChart Class✨
3.1.
Commonly Used Methods:
3.2.
Steps to Generate Pie Chart
3.3.
Step 1: Creating a Class
3.4.
Step 2: Preparing the ObservableList Object
3.5.
Step 3: Creating a PieChart Object
3.6.
Step 4: Set the Title of the Pie Chart
3.7.
Step 5: Setting the Slices Clockwise
3.8.
Step 6: Setting the Length of the Label Line
3.9.
Step 7: Setting the Labels Visible
3.10.
Step 8: Setting the Start Angle of the Pie Chart
3.11.
Step 9: Creating a Group Object
3.12.
Step 10: Creating a Scene Object
3.13.
Step 11: Setting the Title of the Stage
3.14.
Step 12: Adding Scene to the Stage
3.15.
Step 13: Display the Contents of the Stage
3.16.
Step 14: Launching the Application
4.
Example 👀
4.1.
Output
5.
Frequently Asked Questions
5.1.
What kind of data is best for a pie chart?
5.2.
Are pie charts comparable?
5.3.
What three elements make up a JavaFX application?
5.4.
What are the three life cycle methods of the JavaFX application?
5.5.
Who is the developer of JavaFX?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX Pie Chart

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction💁

A Java library called JavaFX is used to create desktop and Rich Internet Applications (RIA). You will learn enough about the JavaFx Pie Chart from this article to advance to more advanced levels of competence. Before reading this article, you should have a basic understanding of JavaFX. It will be pretty simple for you to grasp the principles. If that is not the case, you can return here after reading our JavaFX article.

JavaFX logo

JavaFX

A Java library called JavaFX is used to create both desktop and Rich Internet Applications (RIA). The JavaFX-built apps can be used to operate on desktop, mobile, and web platforms. Swing is set to be replaced by JavaFX as the GUI framework in Java applications. It has more features than Swing, though. JavaFX provides its own components and is independent of the operating system, just as Swing. It is hardware accelerated and lightweight. Various operating systems, including Windows, Linux, and Mac OS, are supported.

JavaFX Pie Chart

PieChart Class

The JavaFX PieChart class is a component. A pie chart is made using the PieChart class. Based on the data set on the PieChart, pie slices are used to fill the chart's content. The pie chart's default arrangement is clockwise. Chart class is inherited by PieChart.

Pie Chart

Constructors of the class are:

  • PieChart(): Creates an empty instance of the pie chart.
  • PieChart(ObservableList data): Creates an instance of a pie chart with given data.
     

The syntax listed below can be used to generate a pie chart.

ObservableList<PieChart.Data> pcd = FXCollections.observableArrayList(
	new PieChart.Data("Lucky",50),
	new PieChart.Data("Unlucky",50),
);
PieChart pc = new PieChart(pcd);
You can also try this code with Online Java Compiler
Run Code

Commonly Used Methods:

                  Methods                                                       Explanation
getData() returns the data of the pie chart
getLabelLineLength() return the pie chart's label length.
setClockWise(Boolean value) It is a property of the boolean type. The slices are arranged clockwise from the initial angle according to their true value.
isClockwise() Return whether the pie chart is rotating clockwise or not
getStartAngle() It returns the start angle of the pie chart


Steps to Generate Pie Chart

The steps listed below should be followed to create a PieChart in JavaFX.

Step 1: Creating a Class

First create a Java class, and inherit the Application class from the javafx.application package, and use the start() method to implement the following code.

public class ClassName extends Application {  
   @Override     
   public void start(Stage primaryStage) throws Exception {     
   }    
}
You can also try this code with Online Java Compiler
Run Code

 

Step 2: Preparing the ObservableList Object

By supplying the data from the pie chart as shown below, create an object with the interface ObservableList.

ObservableList<PieChart.Data> pcd = FXCollections.observableArrayList(
		new PieChart.Data("Thriller",50),
		new PieChart.Data("Horror", 30),
		new PieChart.Data("Action", 10),
		new PieChart.Data("Comedy", 30));
		new PieChart.Data("Scifi", 20));
You can also try this code with Online Java Compiler
Run Code

 

Step 3: Creating a PieChart Object

By providing the ObservableList object as shown below, create a PieChart.

PieChart pc = new PieChart(pcd);
You can also try this code with Online Java Compiler
Run Code

Step 4: Set the Title of the Pie Chart

Using the setTitle() method of the PieChart class, you can change the Pie Chart's title. This is a component of the javafx.scene.chart package.

pc.setTitle("Emotions of humans");
You can also try this code with Online Java Compiler
Run Code

 

Step 5: Setting the Slices Clockwise

Rotate the pie chart segments in a clockwise direction. This is accomplished by calling the class PieChart's setClockwise() function with the Boolean value true. This is a component of the javafx.scene.chart package.

//setting the direction to arrange the data
pc.setClockwise(true);
You can also try this code with Online Java Compiler
Run Code

 

Step 6: Setting the Length of the Label Line

Set the label line's length using the setLabelLineLength() function of the PieChart class, which is found in the javafx.scene.chart package.

//Set the length of the label line
pc.setLabelLineLength(45);
You can also try this code with Online Java Compiler
Run Code

 

Step 7: Setting the Labels Visible

By giving the Boolean value true to the method setLabelsVisible() of the class PieChart, you can make the pie chart's labels visible. This is a component of the javafx.scene.chart package.

//Setting the labels of the pie chart visible
pc.setLabelsVisible(true);
You can also try this code with Online Java Compiler
Run Code

 

Step 8: Setting the Start Angle of the Pie Chart

Utilize the setStartAngle() function of the PieChart class to modify the pie chart's Start angle. This is a component of the javafx.scene.chart package.

//Set the start angle
pc.setStartAngle(180);
You can also try this code with Online Java Compiler
Run Code

 

Step 9: Creating a Group Object

In this, we create group object as follows:-

//Group object creation
Group r = new Group(pc);
You can also try this code with Online Java Compiler
Run Code

 

Step 10: Creating a Scene Object

By instantiating the Scene class from the javafx.scene package, you can create a Scene.

//scene object creation
Scene sc = new Scene(r, 600, 400);
You can also try this code with Online Java Compiler
Run Code

 

Step 11: Setting the Title of the Stage

Using the setTitle() method of the Stage class, you can change the stage's title.

//set title for the stage
s.setTitle("Pie chart");
You can also try this code with Online Java Compiler
Run Code

 

Step 12: Adding Scene to the Stage

Using the setScene() method of the Stage class, you may add a Scene object to the stage. Using the technique demonstrated below, add the Scene object that was ready in the earlier phases.

//Add scene
s.setScene(sc);
You can also try this code with Online Java Compiler
Run Code

 

Step 13: Display the Contents of the Stage

The Stage class's show() method can be used to display the scene's contents.

//Displaying the results
s.show();
You can also try this code with Online Java Compiler
Run Code

 

Step 14: Launching the Application

The static method launch() of the Application class should be called from the main function to start the JavaFX application.

public static void main(String args[]){
	launch(args);
}
You can also try this code with Online Java Compiler
Run Code

Example 👀

Below is the Java program, which generates a pie chart. Put this code in a file called JavaFXPieChartExample.java and save it.
 

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;

//class that extends application class
public class JavaFXPieChartExample extends Application {
	//application starting point
	@Override
	public void start(Stage s) {
		//object of the ObservbleList with each slice percentage
		ObservableList<PieChart.Data> pcd = FXCollections.observableArrayList(
		new PieChart.Data("Thriller",50),
		new PieChart.Data("Horror", 30),
		new PieChart.Data("Action", 10),
		new PieChart.Data("Comedy", 30));
		new PieChart.Data("Scifi", 20));
		
		//piechart creation
		PieChart pc = new PieChart(pcd);
		
		//Set title of the chart
		pc.setTitle("Movie Genres");
		
		//direction of the created piechart
		pc.setClockwise(true);
		
		//length of the label lines in piechart
		pc.setLabelLineLength(45);
		
		//visibility of labels in the chart
		pc.setLabelsVisible(true);
		
		//start angle that has to be used in chart
		pc.setStartAngle(180);
		
		//Group object creation
		Group r = new Group(pc);
		
		//scene object creation
		Scene sc = new Scene(r, 600, 400);
		
		//set title for the stage
		s.setTitle("Pie Chart");
		
		//Add scene
		s.setScene(sc);
		
		//Displaying the results
		s.show();
	}
	
	public static void main(String args[]){
	launch(args);
	}
}
You can also try this code with Online Java Compiler
Run Code


When the above program runs, it creates a JavaFX window that displays a pie chart, as seen below.

Output

Pie Chart Output

 

Frequently Asked Questions

What kind of data is best for a pie chart?

For categorical or nominal data, pie charts make sense to illustrate a parts-to-whole connection. Typically, the pie's segments correspond to percentages of the whole. When using categorical data, the sample is frequently sorted into categories, and the responses follow a predetermined hierarchy.

Are pie charts comparable?

Pies shouldn't typically be used to compare data across pies, judge the relative sizes of categories, or show percentages that don't add up to 100%.

What three elements make up a JavaFX application?

A JavaFX application will typically consist of three main parts: Stage, Scene, and Nodes.

What are the three life cycle methods of the JavaFX application?

The three life cycle methods of JavaFX application are:-

  • public void init()
  • public abstract void start(Stage primaryStage)
  • public void stop()

Who is the developer of JavaFX?

Chris Oliver created JavaFX when he was employed for See Beyond Technology Corporation, which Sun Microsystems eventually purchased in the year 2005.

Conclusion

In this article, we discussed all the points about the JavaFX pie chart. We also discussed the various methods present in the JavaFX pie chart class. We also looked at an example and a few code snippets of the JavaFX pie chart.

We hope this article helps you to learn something new. And if you're interested in learning more, see our posts on JSP15 Best Java Frameworks To Use In 2021Java Development Kit(JDK)Lambda Expressions in JavaJava knowledge for your first coding job.

Visit our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Thank you for reading this post :-)

Feel free to upvote and share this article if it has been useful for you.

Live masterclass