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

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.

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);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 {
}
}
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));
Step 3: Creating a PieChart Object
By providing the ObservableList object as shown below, create a PieChart.
PieChart pc = new PieChart(pcd);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");
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);
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);
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);
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);
Step 9: Creating a Group Object
In this, we create group object as follows:-
//Group object creation
Group r = new Group(pc);
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);
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");
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);
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();
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);
}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);
}
}
When the above program runs, it creates a JavaFX window that displays a pie chart, as seen below.
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.



