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 Polyline, JavaFX Arc, JavaFX Circle, Intersection operation, JavaFX Rotate Transition, Coding Ninjas Studio Interview Bundle, Coding Ninjas Studio Interview Experiences, Coding Ninjas Courses, Coding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog to help other ninjas grow.
Happy Coding!