×

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 of each entity. All methods needed for this purpose are present in the javafx.scene.chart.BarChart class.

The constructor of the Bar Chart:

1. BarChart(Axis x, Axis y):

It is used to create the Bar Chart with specified x and y-axis.

2. BarChart(Axis x, Axis y, ObseravableList newlist):

It is used to create Bar Chart with specified x, y-axis, and specified new data.

Charts in JavaFX applications- Bar Chart showing fruit choices of people:

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;


public class ChartUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
        String Apple = "Apple";  
        String Mango = "Mango";  
        String Banana = "Banana";  
        String Orange= "Orange";  
          
        CategoryAxis newxaxis= new CategoryAxis();  
        NumberAxis yaxis = new NumberAxis(1000,10000,5000);  
        newxaxis.setLabel("Fruits");  
        yaxis.setLabel("Number of people");  
          
        BarChart<String,Float> newbar = new BarChart(newxaxis,yaxis);  
        newbar.setTitle("Fruits Choices of people");  
          
        XYChart.Series<String,Float> series = new XYChart.Series<>();  
        series.getData().add(new XYChart.Data(Apple,1500));  
        series.getData().add(new XYChart.Data(Mango,7300));  
        series.getData().add(new XYChart.Data(Banana,6000));  
        series.getData().add(new XYChart.Data(Orange,3200));  
          
        newbar.getData().add(series);  
          
        Group root = new Group();  
        root.getChildren().add(newbar);  
        Scene scene = new Scene(root,600,500);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle("JavaFX Chart - Bar Chart Example");  
        primaryStage.show();  
          
    }  
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Bar Chart in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Group, javafx.scene.Scene, javafx.scene.chart.BarChart, javafx.scene.chart.CategoryAxis, javafx.scene.chart.NumberAxis, javafx.scene.chart.XYChart, javafx.stage.Stage.

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 Bar Chart, a BarChart object is created which is then passed to the Scene class object.

Bar Chart is created with the help of its constructor. XYSeries data is created and passed to the constructor.

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 – Bar Chart Example". Also, it displays a bar chart showing the number of people who likes a particular fruit.

Charts in JavaFX applications- Bar Chart

Charts in JavaFX applications – Bar Chart showing color choices of students:

Example:

import java.util.Arrays;


import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;


public class ChartUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    CategoryAxis newxAxis = new CategoryAxis();  
        newxAxis.setCategories(FXCollections.<String>
        observableArrayList(Arrays.asList("PreSchool","Primary","Secondary")));
        newxAxis.setLabel("category");
         
        NumberAxis newyAxis = new NumberAxis();
        newyAxis.setLabel("Children");
       
        BarChart<String, Number> newChart = new BarChart<>(newxAxis, newyAxis); 
        newChart.setTitle("Color choice of students");
          
        XYChart.Series<String, Number> newseries1 = new XYChart.Series<>();
        newseries1.setName("Red");
        newseries1.getData().add(new XYChart.Data<>("PreSchool", 10));
        newseries1.getData().add(new XYChart.Data<>("Primary", 30));
        newseries1.getData().add(new XYChart.Data<>("Secondary", 50));
          
        XYChart.Series<String, Number> newseries2 = new XYChart.Series<>();
        newseries2.setName("Orange");
        newseries2.getData().add(new XYChart.Data<>("PreSchool", 50));
        newseries2.getData().add(new XYChart.Data<>("Primary", 60));
        newseries2.getData().add(new XYChart.Data<>("Secondary", 100));


        XYChart.Series<String, Number> newseries3 = new XYChart.Series<>();
        newseries3.setName("Green");
        newseries3.getData().add(new XYChart.Data<>("PreSchool", 40));
        newseries3.getData().add(new XYChart.Data<>("Primary", 20));
        newseries3.getData().add(new XYChart.Data<>("Secondary", 30));
                
        newChart.getData().addAll(newseries1, newseries2, newseries3);
          
        Group root = new Group(newChart);
          
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);  
        primaryStage.setTitle("JavaFX Chart - Bar Chart Example");  
        primaryStage.show();  
          
    }  
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Bar Chart with multiple bars in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Group, javafx.scene.Scene, javafx.scene.chart.BarChart, javafx.scene.chart.CategoryAxis, javafx.scene.chart.NumberAxis, javafx.scene.chart.XYChart, javafx.stage.Stage.

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 multiple Bar Chart, a BarChart object is created which is then passed to the Scene class object.

Bar Chart is created with the help of its constructor. XYSeries data is created and passed to the constructor.

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 – Bar Chart Example". Also, it displays a bar chart showing color choices of students learning in PreSchool, Primaary, and Secondary.

Charts in JavaFX applications- Bar Chart

Related Topics

JavaFX Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 minutes read.

JavaFX Bloom

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

4 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 TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

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

In the JavaFX application, in order to access and use webpages, hyperlinks are used. In HTML anchor tags are used in the same way in JavaFX hyperlinks are used. To...

4 minutes read.

JavaFX Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 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 CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two...

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 Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX Light Distant

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

4 minutes read.

JavaFX Selectors

In the JavaFX application, in order to apply various css effect, we use the various css classes available in the JavaFX. This is generally used to enhance the appearance property...

4 minutes read.

JavaFX 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 colors in these shapes using the setFill()...

6 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

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