Table of contents
1.
Introduction
2.
BarChart
3.
Constructors
4.
Sample Program
5.
Frequently Asked Questions
5.1.
What is JavaFX?
5.2.
What is a package?
5.3.
What are import statements?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX BarChart

Author Manish Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Remember the bar graphs and histograms you read in probably 9th or 10th grade? A bar chart is that same graph with vertical or horizontal bars representing various quantities in the form of bars. While developing UI using JavaFX, you might need to use bar charts. In this blog, you will learn about bar charts in JavaFX, its implementation and the sample program. Let's jump right into the details.

BarChart

A bar chart is a 2D model of different elements against a common domain. It uses vertical or horizontal bars to visualise data. The javafx.scene.chart.BarChart class represents BarChart in JavaFX. As mentioned above, we need to make objects of the class to build a bar chart.

Constructors

There are three constructors in the class:

  1. 'public BarChart( x-axis, y-axis)': It initialises the bar chart object with the specified axes.
  2. 'public StackedAreaChart( x-axis, y-axis, dataList)': It initialises the bar chart object with the specified axes and feeds in data.
  3. 'public StackedAreaChart( x-axis, y-axis, dataList, categoryGap)': It is the same as above, but you can also mention the gap here.

Sample Program

To understand the implementation of a bar chart, let us create a sample project to plot the five most populated countries worldwide.

Step 1: Create a class and extend it to the Application class in the 'javafx.application' package. After this, use the 'start' method of the 'application' class. 

public class CodingNinjas extends Application {  
  @Override     
  public void start(Stage view) throws IOException {      
  }    
}

 

Step 2: Create the x-axis labels in form of Strings

//Naming x-axis elements in form of String data type
String India = "India";
String China = "China";
String United_States = "United States";
String Pakistan= "Pakistan";
String Indonesia="Indonesia";

 

Step 3: Defining the x-axis and y-axis

Use the CategoryAxis and NumberAxis methods to create axes and set start, end and partition sizes.

//Setting up x and y axes
CategoryAxis xaxis= new CategoryAxis();
NumberAxis yaxis = new NumberAxis(20,150,10);
xaxis.setLabel("Countries");
yaxis.setLabel("Population in Cr");

 

Step 4: Create a new bar chart object and give a title

Use the BarChart method of the javafx.scene.chart package to create a new object. Pass x and y-axis objects as arguments.

//Defining BarChart
BarChart<String,Float> infoBar = new BarChart(xaxis,yaxis);
infoBar.setTitle("Most Populous Nations");

 

Step 5: Configuring' data' and adding data to the series

//Configuring data for XY chart
XYChart.Series<String,Float> data = new XYChart.Series<>();
data.getData().add(new XYChart.Data(India,138));
data.getData().add(new XYChart.Data(China,142));
data.getData().add(new XYChart.Data(United_States,33));
data.getData().add(new XYChart.Data(Pakistan,27));
data.getData().add(new XYChart.Data(Indonesia,22));

 

Step 6: Attach data to the bar chart object

//Adding data to the barchart
infoBar.getData().add(data);

 

Step 7: Setting up the group and viewport properties

Use the Group method and the root object to add the BarChart object. Create the scene object and add it to the stage object (here view).

//Setting up group and scene and passing it to view
Group root = new Group();
root.getChildren().add(infoBar);
Scene scene = new Scene(root,600,400);
view.setScene(scene);
view.setTitle("BarChart Example");
view.show();

 

Step 8: Launch the application

Launch the application using the launch method of the Application class from the main method.

public static void main(String[] args) {
        launch();
    }

 

CodingNinjas.java

//Coding Ninjas :)
//Java code to show the implementation of bar chart in JavaFX
package com.example.codingninjas_barchart;

//Import statements to import various libraries
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;

import java.io.IOException;

// Class CodingNinjas, which inherits from the Application class
public class CodingNinjas extends Application {

    //start method of Application class overriden with custom functionality
    @Override
    public void start(Stage view) throws IOException {
        //Naming x-axis elements in form of String data type
        String India = "India";
        String China = "China";
        String United_States = "United States";
        String Pakistan= "Pakistan";
        String Indonesia="Indonesia";

        //Setting up x and y axes
        CategoryAxis xaxis= new CategoryAxis();
        NumberAxis yaxis = new NumberAxis(20,150,10);
        xaxis.setLabel("Countries");
        yaxis.setLabel("Population in Cr");

        //Defining BarChart
        BarChart<String,Float> infoBar = new BarChart(xaxis,yaxis);
        infoBar.setTitle("Most Populous Nations");

        //Configuring data for XY chart
        XYChart.Series<String,Float> data = new XYChart.Series<>();
        data.getData().add(new XYChart.Data(India,138));
        data.getData().add(new XYChart.Data(China,142));
        data.getData().add(new XYChart.Data(United_States,33));
        data.getData().add(new XYChart.Data(Pakistan,27));
        data.getData().add(new XYChart.Data(Indonesia,22));

        //Adding data to the barchart
        infoBar.getData().add(data);

        //Setting up group and scene and passing it to view
        Group root = new Group();
        root.getChildren().add(infoBar);
        Scene scene = new Scene(root,600,400);
        view.setScene(scene);
        view.setTitle("BarChart Example");
        view.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

Output: 


Check out this problem - Largest Rectangle in Histogram

Frequently Asked Questions

What is JavaFX?

JavaFX is an open-source Software Development Kit for building feature-rich and modern GUI desktop and web applications. It is the outcome of the collaboration of a large pool of developers and companies to produce a toolkit to develop modern client applications.

What is a package?

A package is a namespace similar to a folder where you can put your source files. It organises related classes and interfaces. You might keep java code in one folder, images in other and so on.

What are import statements?

Import statement fetches pre-written libraries, which help in the faster development of applications. Organisations or language developers provide these libraries.

Conclusion

Finally, you have made it to the end of this article. Congratulations!! In this blog, you learnt about the BarChart class in JavaFX. You went through the class, its constructors and a sample program.

source: thecoderpedia.com

After reading these interview questions, are you not feeling excited to read more articles on the topic of JavaFX? Don't worry; Coding Ninjas has you covered. To learn, see the JavaFXJavaFX MenusJavaFX Inline StylesJavaFX circles etc.

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enrol in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Please do upvote our blogs if you find them helpful and informative!

Happy learning!

Live masterclass