×

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of the plane. All methods needed for this purpose are present in the javafx.scene.chart.ScatterChart class.

The constructor of the Scatter Chart:

1. ScatterChart(Axis x, Axis y):

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

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

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

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

Example:

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


public class SliderUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      NumberAxis xaxis = new NumberAxis(5,35,5);  
          NumberAxis yaxis = new NumberAxis(1,2.2,0.1);  
          xaxis.setLabel("Age");  
          yaxis.setLabel("Height");  
            
          ScatterChart s = new ScatterChart(xaxis,yaxis);  
          s.setTitle("Height Chart  with your age");  
            
          XYChart.Series series = new XYChart.Series();  
          series.setName("Height value");  
          series.getData().add(new XYChart.Data(5,1.27));  
          series.getData().add(new XYChart.Data(10,1.35));  
          series.getData().add(new XYChart.Data(15,1.42));  
          series.getData().add(new XYChart.Data(20,1.49));  
          series.getData().add(new XYChart.Data(25,1.50));  
          series.getData().add(new XYChart.Data(30,1.50));  
          series.getData().add(new XYChart.Data(35,1.50));  
         
            
          s.getData().add(series);  
            
          Group root = new Group();  
          root.getChildren().add(s);  
          Scene scene = new Scene(root,600,400);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle("JavaFX Chart - Scatter Chart Example");  
        primaryStage.show();  
          
    }  
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Scatter 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.NumberAxis, javafx.scene.chart.ScatterChart, javafx.scene.chart.XYChart, javafx.stage.Stage.

Then we have created one class named SliderUI 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 Scatter Chart, a ScatterChart object is created which is then passed to the Scene class object.

Scatter 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 – Scatter Chart Example". Also, it displays a scatter chart showing the age and height relationship.

Charts in JavaFX applications- Scatter Chart

Charts in JavaFX applications- Scatter Chart showing Population data:

Example:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.layout.StackPane;




public class SliderUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    CategoryAxis newxAxis = new CategoryAxis();
         NumberAxis newyAxis = new NumberAxis();
         ScatterChart scatterChart = new ScatterChart(newxAxis, newyAxis);
         scatterChart.setData(getChartData());
         scatterChart.setTitle("Population");
         primaryStage.setTitle("Javafx Scatter Chart example");


         StackPane root = new StackPane();
         root.getChildren().add(scatterChart);
         primaryStage.setScene(new Scene(root, 600, 550));
         primaryStage.show();
     }


     private ObservableList<XYChart.Series<String, Double>> getChartData() {
       double xValue = 1.56;
       double yValue = 1.06;
       ObservableList<XYChart.Series<String, Double>> answer = FXCollections.observableArrayList();
       Series<String, Double> xSeries = new Series<String, Double>();
       Series<String, Double> ySeries = new Series<String, Double>();
       xSeries.setName("X");
       
       for (int i = 2001; i < 2011; i++) {
           xSeries.getData().add(new XYChart.Data(Integer.toString(i), xValue));
           xValue = xValue + Math.random() - .5;
           ySeries.getData().add(new XYChart.Data(Integer.toString(i), yValue));
           yValue = yValue + Math.random() - .5;
       }
       answer.addAll(xSeries, ySeries);
       return answer;
     }
   
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

In order to create the Scatter Chart in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.collections.FXCollections, javafx.collections.ObservableList, javafx.scene.Group, javafx.scene.Scene, javafx.scene.chart.CategoryAxis, javafx.scene.chart.NumberAxis, javafx.scene.chart.ScatterChart, javafx.scene.chart.XYChart, javafx.stage.Stage, javafx.scene.chart.XYChart.Series, javafx.scene.layout.StackPane.

Then we have created one class named SliderUI 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 Population Scatter Chart, a ScatterChart object is created which is then passed to the Scene class object.

Scatter 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 – Scatter Chart Example". Also, it displays a scatter chart showing the population among the x and y axis.

Charts in JavaFX applications- Scatter Chart

Related Topics

JavaFX UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.

JavaFX Architecture

JavaFX architecture consists of many built-in components such as JavaFX public API, Scene Graph, Quantum Toolkit, Prism, Glass windowing toolkit, web view, media engines. These components help to develop rich...

3 minutes read.

JavaFX Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

4 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX Scale Transition

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

4 minutes read.

JavaFX Color Input

In the JavaFX application, in order to apply various color input effect, we use the ColorInput class. It has the ability to fill the specified shape with a given color...

4 minutes read.

JavaFX Path Transition

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

3 minutes read.

JavaFX Motion Blur

In the JavaFX application, in order to apply various Motion blur effect, we use Motion Blur class. It has the ability to apply the Motion blur effect on the given...

4 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 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 Box

In the JavaFX application, in order to draw a Box, the Box class is used. The Box allows us to form 3D shape having rectangular faces. All methods needed for...

4 minutes read.

JavaFX ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 minutes read.

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 Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 minutes read.

JavaFX Fade Transition

In the JavaFX application, in order to generate the fade transition, we have to apply fade transition on any particular shape. It has the ability to show the fading colors...

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

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

4 minutes read.

JavaFX Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

4 minutes read.