×

JavaFX Pie Chart

In the JavaFX application, in order to create Pie Chart, the PieChart class is used. The Pie Chart allows us to plot data along with the sectors of the circle. All methods needed for this purpose are present in the javafx.scene.chart.PieChart class.

The constructor of Pie Chart:

1. PieChart():

It is used to create the empty Pie Chart.

2. PieChart(ObseravableList newlist):

It is used to create Pie Chart with specified new data.

Charts in JavaFX applications- Pie Chart showing constituents of air gases:

Example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class ChartUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      PieChart pieChart = new PieChart();


          PieChart.Data slice1 = new PieChart.Data("Oxygen ", 20);
          PieChart.Data slice2 = new PieChart.Data("Other Gases"  , 1);
          PieChart.Data slice3 = new PieChart.Data("Nitrogen" , 79);


          pieChart.getData().add(slice1);
          pieChart.getData().add(slice2);
          pieChart.getData().add(slice3);


          VBox vbox = new VBox(pieChart);


          Scene scene = new Scene(vbox, 400, 200);


          primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX Chart - Pie Chart Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Pie Chart in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.chart.PieChart, javafx.scene.layout.VBox.

Then we have created one class named ChartUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a Pie Chart with constituents of air in different sectors, a PieChart object is created which is then passed to the Scene class object.

Pie Chart is created with the help of its constructor. Data for the pie chart is added using PieChart.Data is then added to the pie chart object.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX Chart – Pie Chart Example". Also, it displays a pie chart with different air constituents like Oxygen, Nitrogen, Other gases mentioned in different sectors.

Charts in JavaFX applications Pie Chart

Charts in JavaFX applications – Pie Chart showing measure of different language:

Example:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class ChartUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      PieChart chart = new PieChart();  
      
      chart.setData(getChartData());  
      chart.setLegendSide(Side.LEFT);  
      chart.setTitle(" Indian Languages ");  
      chart.setClockwise(false);  
          
        StackPane root = new StackPane();  
          
        root.getChildren().add(chart);  
          
        Scene scene = new Scene(root,800,600);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle("JavaFX Chart - Pie Chart Example");  
        primaryStage.show();  
          
    }  
      
    private ObservableList<Data> getChartData() {  
        ObservableList<Data> list = FXCollections.observableArrayList();  
        list.addAll(new PieChart.Data("Marathi", 30.8),  
                new PieChart.Data("Hindi", 11.8),  
                new PieChart.Data("English", 10.8),  
                new PieChart.Data("Tamil", 11.6),  
                new PieChart.Data("Telgu", 7.2),  
                new PieChart.Data("Gujarathi", 10.7),  
                new PieChart.Data("Bengali", 5.2) 
    );  
        return list;  
    }  
}

Output:

In order to create the Pie Chart in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.chart.PieChart, javafx.scene.layout.VBox, javafx.collections.FXCollections, javafx.collections.ObservableList, javafx.geometry.Side.

Then we have created one class named ChartUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold Pie Chart with Indian languages in different sectors, a PieChart object is created which is then passed to the Scene class object.

Pie Chart is created with the help of its constructor. Data for the pie chart is added using the chart.setData() method. Also setTitle() and setLegendSide() method is used with pie chart.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX Chart – Pie Chart Example". Also, it displays a pie chart with different Indian languages in different sectors of the pie chart.

Charts in JavaFX applications Pie Chart

Related Topics

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX Arc

In the JavaFX application, in order to draw an arc, the Arc class is used. The Arc allows us to join any points in the space. All methods needed for...

5 minutes read.

JavaFX UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.

JavaFX Rotate Transition

In the JavaFX application, in order to generate the rotate transition, we have to apply rotation transition on any particular shape. It has the ability to rotate the shape along...

4 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX Gradient Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the gradient colors in these shapes using the...

7 minutes read.

JavaFX Path Transition

In the JavaFX application, in order to generate the path transition, we have to apply path transition on any particular shape. It has the ability to traverse the given shape...

3 minutes read.

JavaFX Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

4 minutes read.

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.

JavaFX Blend

In the JavaFX application, in order to apply various blend effect, we use Blend class. It has the ability to blend the colors of the shape. All methods needed for...

4 minutes read.

JavaFX - Basic Structure of Application

Every JavaFX application is mainly divided into three main components namely Scene, Stage, and nodes. For using these components we need to import javafx.application.Application class in every javafx application. Life...

4 minutes read.

JavaFX Translate Transition

In the JavaFX application, in order to generate the translate transition, we have to apply translate transition on any particular shape. It has the ability to translate the shape along...

3 minutes read.

JavaFX Translation

In the JavaFX application, in order to apply various Translate effect, we use this class. It has the ability to Translate the given object according to the specified parameter. All...

4 minutes read.

JavaFX Installation

In order to run JavaFX applications, we need to have eclipse and java installed on our machine. Prerequisite: Operating systemAny Operating system (Windows, Linux, Mac)Memory requirementNo minimum requirementDisk SpaceNo minimum requirementJDK versionJDK...

3 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

3 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.

JavaFX Architecture

JavaFX architecture consists of many built-in components such as JavaFX public API, Scene Graph, Quantum Toolkit, Prism, Glass windowing toolkit, web view, media engines. These components help to develop rich...

3 minutes read.

JavaFX Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.