JavaFX Tutorial Index

JavaFX Tutorial JavaFX Installation JavaFX Application Structure JavaFX Architecture

JavaFX Applications Charts

JavaFX Bar Chart JavaFX Bubble Chart JavaFX Pie Chart JavaFX Scatter Chart JavaFX Area Chart JavaFX Line Chart

JavaFX 2D Shapes

JavaFX Arc JavaFX Circle JavaFX Ellipse JavaFX Line JavaFX Polygon JavaFX Rectangle JavaFX Color JavaFX Gradient Color

JavaFX 3D Shapes

JavaFX Box JavaFX Cylinder JavaFX Sphere

JavaFX Animations

JavaFX Rotate Transition JavaFX Translate Transition JavaFX Fade Transition JavaFX Fill Transition JavaFX Parallel Transition JavaFX Path Transition JavaFX Pause Transition JavaFX Scale Transition JavaFX Sequential Transition JavaFX Stroke Transition

JavaFX CSS

JavaFX ID Selector JavaFX Inline Styles JavaFX Selectors

JavaFX Effect

JavaFX Blend JavaFX Bloom JavaFX Color Adjust JavaFX Color Input JavaFX Drop Shadow JavaFX Gaussian Blur JavaFX Glow JavaFX Image Input JavaFX Inner Shadow JavaFX Light Distant JavaFX Light Point JavaFX Light Spot JavaFX Lighting JavaFX Motion Blur JavaFX Reflection JavaFX Shadow

JavaFX Layouts

JavaFX Layouts JavaFX BorderPane JavaFX GridPane JavaFX StackPane JavaFX HBox JavaFX TilePane

JavaFX Event Handling

JavaFX Event Handling JavaFX Event Filters JavaFX Convenience Methods

JavaFX Transformation

JavaFX Transformation JavaFX Scaling JavaFX Rotation JavaFX Translation JavaFX Shear

JavaFX UI

JavaFX Menu JavaFX Button JavaFX Button Styling JavaFX CheckBox JavaFX File Chooser JavaFX HyperLink JavaFX Label JavaFX UI Controls JavaFX PasswordField JavaFX ProgressBar JavaFX RadioButton JavaFX ScrollBar JavaFX Slider JavaFX TextField

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