×

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 Layouts

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used...

3 minutes read.

JavaFX Stroke Transition

In the JavaFX application, in order to generate the stroke transition, we have to apply stroke transition on any particular shape. It has the ability to give different color strokes...

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 File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

4 minutes read.

JavaFX Scale Transition

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

4 minutes read.

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

3 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 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 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 Cylinder

In the JavaFX application, in order to draw a Cylinder, the Cylinder class is used. The Cylinder allows us to form a 3D shape having rectangular faces and a straight...

4 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 Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX Glow

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

4 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX Inner Shadow

In the JavaFX application, in order to apply various Inner Shadow effect, we use this class. It has the ability to create the inner shadow of the given object. This...

4 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX Light Point

In the JavaFX application, in order to apply various LightPoint effect from a single source, we use this class. It has the ability to apply light from a single source...

4 minutes read.