Do you think IIT Guwahati certified course can help you in your career?
No
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.
'public BarChart( x-axis, y-axis)': It initialises the bar chart object with the specified axes.
'public StackedAreaChart( x-axis, y-axis, dataList)': It initialises the bar chart object with the specified axes and feeds in data.
'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.
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.
publicstaticvoidmain(String[] args) { launch(); }
CodingNinjas.java
//Coding Ninjas :) //Java code to show the implementation of bar chart in JavaFX package com.example.codingninjas_barchart;
// Class CodingNinjas, which inherits from the Application class publicclassCodingNinjasextendsApplication {
//start method of Application class overriden with custom functionality @Override publicvoidstart(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(); }
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.
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 JavaFX, JavaFX Menus, JavaFX Inline Styles, JavaFX circles etc.