×

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 Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

4 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 Reflection

In the JavaFX application, in order to apply various reflection effect, we use this class. It has the ability to create the reflection of the given object. This effect adds...

4 minutes read.

JavaFX Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

4 minutes read.

JavaFX Gradient Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the gradient colors in these shapes using the...

7 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

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 Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX TilePane

In the JavaFX application, in order to set TilePane as a layout, the TilePane class is used. The TilePane layout allows us to arrange the components in the same sized...

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 Translate Transition

In the JavaFX application, in order to generate the translate transition, we have to apply translate transition on any particular shape. It has the ability to translate the shape along...

3 minutes read.

JavaFX Fill Transition

In the JavaFX application, in order to generate the fill transition, we have to apply fill transition on any particular shape. It has the ability to fill the given shape...

4 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX Inner Shadow

In the JavaFX application, in order to apply various Inner Shadow effect, we use this class. It has the ability to create the inner shadow of the given object. This...

4 minutes read.

JavaFX Light Point

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

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 CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two...

4 minutes read.

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.