JavaFX Tutorial Index

JavaFX Tutorial JavaFX Installation JavaFX Application Structure JavaFX Architecture

JavaFX Applications Charts

JavaFX Bar Chart JavaFX Bubble Chart JavaFX Pie Chart JavaFX Scatter Chart JavaFX Area Chart JavaFX Line Chart

JavaFX 2D Shapes

JavaFX Arc JavaFX Circle JavaFX Ellipse JavaFX Line JavaFX Polygon JavaFX Rectangle JavaFX Color JavaFX Gradient Color

JavaFX 3D Shapes

JavaFX Box JavaFX Cylinder JavaFX Sphere

JavaFX Animations

JavaFX Rotate Transition JavaFX Translate Transition JavaFX Fade Transition JavaFX Fill Transition JavaFX Parallel Transition JavaFX Path Transition JavaFX Pause Transition JavaFX Scale Transition JavaFX Sequential Transition JavaFX Stroke Transition

JavaFX CSS

JavaFX ID Selector JavaFX Inline Styles JavaFX Selectors

JavaFX Effect

JavaFX Blend JavaFX Bloom JavaFX Color Adjust JavaFX Color Input JavaFX Drop Shadow JavaFX Gaussian Blur JavaFX Glow JavaFX Image Input JavaFX Inner Shadow JavaFX Light Distant JavaFX Light Point JavaFX Light Spot JavaFX Lighting JavaFX Motion Blur JavaFX Reflection JavaFX Shadow

JavaFX Layouts

JavaFX Layouts JavaFX BorderPane JavaFX GridPane JavaFX StackPane JavaFX HBox JavaFX TilePane

JavaFX Event Handling

JavaFX Event Handling JavaFX Event Filters JavaFX Convenience Methods

JavaFX Transformation

JavaFX Transformation JavaFX Scaling JavaFX Rotation JavaFX Translation JavaFX Shear

JavaFX UI

JavaFX Menu JavaFX Button JavaFX Button Styling JavaFX CheckBox JavaFX File Chooser JavaFX HyperLink JavaFX Label JavaFX UI Controls JavaFX PasswordField JavaFX ProgressBar JavaFX RadioButton JavaFX ScrollBar JavaFX Slider JavaFX TextField

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