JavaFX Bar Chart - JavaFX

What is JavaFX Bar Chart?

A bar chart is used to represent grouped information the use of rectangular bars. The length of those bars depicts the values. The bars in the bar chart may be plotted vertically or horizontally.

Following is a bar chart, comparing various car brands.

JavaFX - Bar Chart

In JavaFX, a Bar chart is represented by way of a class named BarChart. This class belongs to the package javafx.scene.chart. by using instantiating this class, you may create an BarChart node in JavaFX.

Steps to Generate Bar Chart

To generate a bar chart in JavaFX, observe the steps given below.

Step 1: Creating a Class

Create a Java class and inherit the Application class of the package javafx.application. you may then implement the start() technique of this class as follows.

Step 2: Defining the Axis

Define the X and Y axis of the bar chart and set labels to them. In our example, X axis represents the category of contrast and the y axis represents the score.

Step 3: Creating the Bar Chart

Create a line chart by instantiating the class named BarChart of the package javafx.scene.chart. To the constructor of this class, pass the objects representing the X and Y axis created in the previous step.

Step 4: Preparing the Data

Instantiate the XYChart.Series class and add the data (a series of, x and y coordinates) to the Observable list of this class as follows –

Step 5: Add Data to the Bar Chart

Add the data series prepared in the previous step to the bar chart as follows –

Step 6: Creating a Group Object

In the start() technique, create a group object by means of instantiating the class named group. This belongs to the package javafx.scene.

Pass the BarChart (node) object, created in the preceding step as a parameter to the constructor of the group class. This must be carried out if you want to upload it to the group as follows –

Step 7: Creating a Scene Object

Create a Scene by using instantiating the class named Scene, which belongs to the package javafx.scene. To this class, pass the group item (root) created in the preceding step.

Similarly to the foundation object, you may also pass two double parameters representing height and width of the screen, along with the object of the group class as follows.

Step 8: Setting the Title of the Stage

You can set the title to the stage using the setTitle()technique of the Stage class. The primaryStage is a Stage object, which is passed to the start technique of the scene class as a parameter.

Using the primaryStage object, set the title of the scene as Sample Application as follows.

Step 9: Adding Scene to the Stage

You may add a Scene object to the stage using the technique setScene() of the class named Stage. Add the Scene object prepared in the previous steps using the following technique.

Step 10: Displaying the Contents of the Stage

Display the contents of the scene using the technique named show() of the Stage class as follows.

Step 11: Launching the Application

Launch the JavaFX application by calling the static technique launch() of the Application class from the main technique as follows.

Example

The following example depicts numerous car statistics with the help of a bar chart. Following is a list of car brands in conjunction with their one of a kind traits, which we will show using a bar chart –

Car

Speed

User Rating

Millage

Safety

Fiat

1.0

3.0

5.0

5.0

Audi

5.0

6.0

10.0

4.0

Ford

4.0

2.0

3.0

6.0

Following is a Java program which generates a bar chart, depicting the above data using JavaFX.

Save this code in a file with the name BarChartExample.java.

Collect and execute the saved java file from the command prompt using the following commands.

On executing, the above program generates a JavaFX window displaying an area chart as shown below.

JavaFX - Bar Chart

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JavaFX Topics