Table of contents
1.
Introduction
2.
Bubble Chart
3.
Example on Bubble Chart
4.
Frequently Asked Questions
4.1.
Why is XYChart.Series class is used?
4.2.
What is the use of an Initializable interface?
4.3.
Name the components of JavaFX?
4.4.
What are the different transformations that JavaFX provides?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Bubble Chart Using JavaFX

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This article will teach you how to create a bubble chart using the JavaFX class and its methods.

JavaFX is used to develop and distributes cross-platform desktop and rich web applications. JavaFX is compatible with desktop PCs, web browsers running Microsoft Windows, Linux, and macOS, and mobile devices running iOS and Android.

Bubble Chart

A bubble chart is a diagram that shows three-dimensional data, according to one definition. Each object is represented as a bubble with three triplets (x1, x2, x3). The (X, Y) location of the bubble indicates two of the triplets, while the bubble's radius indicates the third.

Steps to generate data for a bubble and creating a bubble chart:

  • The XYChart.Data class can be instantiated to generate the necessary number of points.
  • By instantiating the XYChart.Series class, a series may be created.
  • Utilize the getData() function to obtain the observable list for the XYChart.Series class.
  • Use the add() or addAll() methods to add the newly formed data points to the list.
  • Create a new data series and add it to the area chart.

Example on Bubble Chart

In this section, we will discuss little examples of what we have learned so far. Here, we will create a simple bubble chart showing the budget for two products, "Product" and "Product_2" on a particular week. Here, series1 indicates the data points of "Product" and series2 indicates the data points of "Product_2".

Code:

package sample;
 
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BubbleChart;
import javafx.scene.chart.XYChart;
 
import java.net.URL;
import java.util.ResourceBundle;
 
public class Controller implements Initializable {
 
@FXML
private BubbleChart<Integer, Integer> bubbleChart;
 
XYChart.Series series1 = new XYChart.Series();
XYChart.Series series2 = new XYChart.Series();
 
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
 
bubbleChart.getXAxis().setLabel("Week");
bubbleChart.getYAxis().setLabel("Budget");
 
series1.setName("Product");
series1.getData().add(new XYChart.Data(1, 35));
series1.getData().add(new XYChart.Data(13, 67, 7));
series1.getData().add(new XYChart.Data(22, 24));
series1.getData().add(new XYChart.Data(24, 12));
series1.getData().add(new XYChart.Data(42, 5));
series1.getData().add(new XYChart.Data(45, 5));
series1.getData().add(new XYChart.Data(49, 32));
series1.getData().add(new XYChart.Data(50, 44));
 
series2.setName("Product_2");
series2.getData().add(new XYChart.Data(12, 33));
series2.getData().add(new XYChart.Data(13, 23, 7));
series2.getData().add(new XYChart.Data(43, 12));
series2.getData().add(new XYChart.Data(23, 43));
series2.getData().add(new XYChart.Data(43, 34));
series2.getData().add(new XYChart.Data(12, 33));
series2.getData().add(new XYChart.Data(43, 3));
series2.getData().add(new XYChart.Data(11, 41));
 
bubbleChart.getData().addAll(series1, series2);
}
}


Output:

Frequently Asked Questions

 

Why is XYChart.Series class is used?

This class creates a Series and fills it with the data from the supplied ObservableList. creates a named Series and fills it with the data from the supplied ObservableList.

 

What is the use of an Initializable interface?

This interface contains a method that is called to initialize a controller after its root element has been completely processed.

Parameters:

Location: is either null if the location is unknown or the location used to resolve relative paths for the root object.

Resources - The resources applied to localizing the root object or null if no localization was done.

 

Name the components of JavaFX?

JavaFX application consists of three components:

  • Stage: Stage includes every JavaFX object.
  • Scene: Scene depicts the actual contents of the JavaFX application.
  • Nodes: Nodes is the JavaFX.scene package that illustrates a node.

 

What are the different transformations that JavaFX provides?

Transformations that JavaFX provides are

  • Translation
  • Scaling up and down
  • Rotation
  • shearing

Conclusion

In this article, we have extensively discussed how to create a bubble chart using JavaFX by using the classes JavaFX offers.

We hope this blog has helped you enhance your knowledge regarding creating bubble charts using JavaFX. Check out the awesome content on the Coding Ninjas Website, JavaFX PolylineJavaFX Arc, JavaFX CircleIntersection operationJavaFX Rotate TransitionCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass