×

JavaFX Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this purpose are present in the javafx.scene.chart.BubbleChart class.

The constructor of the Bubble Chart:

1. BubbleChart(Axis x, Axis y):

It is used to create the bubble Chart with specified x and y-axis.

2. BubbleChart(Axis x, Axis y, ObseravableList newlist):

It is used to create a bubble Chart with specified x, y-axis, and specified new data.

Charts in JavaFX applications- Bubble Chart showing Age-Height relation:

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.BubbleChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;


public class ChartUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    NumberAxis newxAxis = new NumberAxis(0, 35, 5);        
         newxAxis.setLabel("Age"); 
           
         NumberAxis newyAxis = new NumberAxis(10, 180, 10); 
         newyAxis.setLabel("Height"); 
         
         BubbleChart bubbleChart = new BubbleChart(newxAxis, newyAxis);    
            
         XYChart.Series series = new XYChart.Series();  
         series.setName("Age-Height chart"); 
            
         series.getData().add(new XYChart.Data(5,20,2));  
         series.getData().add(new XYChart.Data(10,70,4)); 
         series.getData().add(new XYChart.Data(15,90,5)); 
         series.getData().add(new XYChart.Data(20,150,6));    
         series.getData().add(new XYChart.Data(25,170,7));        
         
         bubbleChart.getData().add(series); 
            
         Group root = new Group(bubbleChart); 
            
         Scene scene = new Scene(root, 600, 400);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle("JavaFX Chart - Bubble Chart Example");  
        primaryStage.show();  
          
    }  
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Bubble Chart in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Group, javafx.scene.Scene, javafx.scene.chart.BubbleChart, javafx.scene.chart.CategoryAxis, javafx.scene.chart.NumberAxis, javafx.scene.chart.XYChart, javafx.stage.Stage.

Then we have created one class named ChartUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a Bubble Chart, a BubbleChart object is created which is then passed to the Scene class object.

Bubble Chart is created with the help of its constructor with the x axis and y axis passed in the constructor. XYSeries data is created and passed to the constructor.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX Chart – Area Chart Example". Also, it displays an area chart showing the rainfall along different months.

Charts in JavaFX applications- Bubble Chart

Charts in JavaFX applications- Area Chart showing sales of 2 shops:

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.BubbleChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.paint.Color;
import javafx.stage.Stage;


public class ChartUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      NumberAxis newxAxis = new NumberAxis(1, 53, 4);
          NumberAxis newyAxis = new NumberAxis(0, 80, 10);
          BubbleChart<Number,Number> chart = 
             new BubbleChart<Number,Number>(newxAxis,newyAxis);
         newxAxis.setLabel("Week");
         newxAxis.setTickLabelFill(Color.RED);
         newxAxis.setMinorTickCount(4);
        
         newyAxis.setLabel("Sale");
         newyAxis.setTickLabelFill(Color.BLUE);
         newyAxis.setTickLabelGap(10);
         newyAxis.setTickLabelFormatter(
             new NumberAxis.DefaultFormatter(newyAxis,"$ ",null)
         );
                               
         chart.setTitle("Sales Chart");
         
         XYChart.Series series1 = new XYChart.Series();
         series1.setName("Shop 1");
         series1.getData().add(new XYChart.Data(3, 35, 2));
         series1.getData().add(new XYChart.Data(12, 60, 1.8));
         series1.getData().add(new XYChart.Data(15, 15, 7));
         series1.getData().add(new XYChart.Data(22, 30, 2.5));
         series1.getData().add(new XYChart.Data(28, 20, 1));
         series1.getData().add(new XYChart.Data(35, 41, 5.5));
        
                 
         XYChart.Series series2 = new XYChart.Series();
         series2.setName("Shop 2");
         series2.getData().add(new XYChart.Data(8, 15, 2));
         series2.getData().add(new XYChart.Data(13, 23, 1));
         series2.getData().add(new XYChart.Data(15, 45, 3));
         series2.getData().add(new XYChart.Data(24, 30, 4.5));
         series2.getData().add(new XYChart.Data(38, 78, 1));
         series2.getData().add(new XYChart.Data(40, 41, 7.5));
         chart.getData().addAll(series1, series2);     


                        
         Scene scene  = new Scene(chart);
        primaryStage.setScene(scene);  
        primaryStage.setTitle("JavaFX Chart - Bubble Chart Example");  
        primaryStage.show();  
          
    }  
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Area Chart in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Group, javafx.scene.Scene, javafx.scene.chart.AreaChart, javafx.scene.chart.CategoryAxis, javafx.scene.chart.NumberAxis, javafx.scene.chart.XYChart, javafx.stage.Stage.

Then we have created one class named ChartUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold the sales Area Chart, an AreaChart object is created which is then passed to the Scene class object.

Area Chart is created with the help of its constructor with the x axis and y axis passed in the constructor. XYSeries data is created and passed to the constructor.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX Chart – Area Chart Example". Also, it displays an area chart showing the sales of 2 shops. It shows the sales comparision between 2 shops.

Charts in JavaFX applications- Bubble Chart

Related Topics

JavaFX File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

4 minutes read.

JavaFX Circle

In the JavaFX application, in order to draw a circle, the Circle class is used. The Circle allows us to join any points in the space. All methods needed for...

4 minutes read.

JavaFX Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 minutes read.

JavaFX Shear

In the JavaFX application, in order to apply various shear effect, we use this class. It has the ability to shear the given object along the x or y axis....

3 minutes read.

JavaFX PasswordField

In the JavaFX application, to get the user information and password as input from the user, a form is created using textfields and password fields which allows the user to...

4 minutes read.

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.

JavaFX Pie Chart

In the JavaFX application, in order to create Pie Chart, the PieChart class is used. The Pie Chart allows us to plot data along with the sectors of the circle....

3 minutes read.

JavaFX - Basic Structure of Application

Every JavaFX application is mainly divided into three main components namely Scene, Stage, and nodes. For using these components we need to import javafx.application.Application class in every javafx application. Life...

4 minutes read.

JavaFX Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX Glow

In the JavaFX application, in order to apply various glow effect, we use Glow class. It has the ability to glow the colors of the shape. All methods needed for...

4 minutes read.

JavaFX HBox

In the JavaFX application, in order to set HBox as a layout, the HBox class is used. The HBox layout allows us to arrange the components in one row. All...

3 minutes read.

JavaFX Arc

In the JavaFX application, in order to draw an arc, the Arc class is used. The Arc allows us to join any points in the space. All methods needed for...

5 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Polygon

In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods...

4 minutes read.

JavaFX Rectangle

In the JavaFX application, in order to draw a rectangle, the Rectangle class is used. The Rectangle allows us to join any four points with x and y coordinates in...

4 minutes read.

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.