Table of contents
1.
Introduction
2.
Scatter Chart
3.
Constructors
4.
Sample Program
5.
Frequently Asked Questions
5.1.
What is JavaFX?
5.2.
What is the @override statement?
5.3.
What are import statements?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX Scatter Chart

Author Manish Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Have you ever plotted a graph on a cartesian plane !? I am sure you have. We all have plotted a graph at least once. In contrast, there is often a requirement for charts in developing applications. So how do we do that? JavaFX has a scatter chart class that does all the hard work and gives the developer simple functions to achieve this. Let's jump right into the details of the Scatter Chart class.

Scatter Chart

A ScatterChart class draws discrete 2D graphs in a cartesian plane. It is available in the JavaFX library under the 'scene' class. It visualises the relationship between two variables. In JavaFX, the Scatter Chart is represented by the â€˜javafx.scene.chart.ScatterChart’ class.

Constructors

There are two constructors in the class:

  1. public ScatterChart( x-axis, y-axis): It initialises the scatter chart object with the specified axes.
  2. public ScatterChart( x-axis, y-axis, dataList): It initialises the scatter chart object with the specified axes and feeds in data.

Sample Program

To understand the implementation of a scatter chart, let us create a sample project to plot the subscriber gain of the coding-ninjas youtube channel for the year 2021. 

Step 1: Create a class and extend it to the Application class in the 'javafx.application' package. After this, use the 'start' method of the 'application' class. 

public class CodingNinjas extends Application {  
  @Override     
  public void start(Stage view) throws IOException {      
  }    
}

 

Step 2: Defining the x-axis and y-axis

Use the NumberAxis method to create axes and set start, end and partition sizes.

//Setting configurations rules for both axes
NumberAxis xaxis = new NumberAxis(1,12,1);
NumberAxis yaxis = new NumberAxis(0,20000,1000);
xaxis.setLabel("Months");
yaxis.setLabel("Users");

 

Step 3: Create a new scatter chart object and give a title

Use the ScatterChart method of the javafx.scene.chart package to create a new object. Pass x and y-axis objects as arguments.

ScatterChart CodingNinjasScatterChart = new ScatterChart(xaxis,yaxis);
CodingNinjasScatterChart.setTitle("Subscribers gained on Coding Ninjas Youtube Channel (2021)");

 

Step 4: Configuring Series and adding data to the series       

XYChart.Series data = new XYChart.Series();
data.setName("Subscribers Gained");

data.getData().add(new XYChart.Data(1,2500));
data.getData().add(new XYChart.Data(2,3100));

 

Step 5: Attach data to the scatter chart object

CodingNinjasScatterChart.getData().add(data);

 

Step 6: Setting up the group and viewport properties

Use the Group method and the root object to add the ScatterChart object. Create the scene object and add it to the stage object (here view).       

Group root = new Group();
root.getChildren().add(CodingNinjasScatterChart);
Scene scene = new Scene(root,600,400);
view.setScene(scene);
view.setTitle("Coding Ninjas ScatterChart Example");
view.show();

 

Step 7: Launch the application

Launch the application using the launch method of the Application class from main method.

public static void main(String[] args) {
        launch();
    }

 

CodingNinjas.java

//This program shows implementation of scatter chart in JavaFX
package com.example.scatter_chart;

//All libraries required for the program to work are imported below
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;

import java.io.IOException;

//Main Java class is implemented below
public class CodingNinjas extends Application {
    @Override
    //start method with Stage object as parameter
    public void start(Stage view) throws IOException {

        //Setting configurations rules for both axes
        NumberAxis xaxis = new NumberAxis(1,12,1);
        NumberAxis yaxis = new NumberAxis(0,20000,1000);
        xaxis.setLabel("Months");
        yaxis.setLabel("Users");

        //Creating new scatter-chart object and giving title
        ScatterChart CodingNinjasScatterChart = new ScatterChart(xaxis,yaxis);
        CodingNinjasScatterChart.setTitle("Subscribers gained on Coding Ninjas Youtube Channel (2021)");

        //Setting up Series and adding data to the series
        XYChart.Series data = new XYChart.Series();
        data.setName("Subscribers Gained");

        //Adding data to newly created XY Chart
        data.getData().add(new XYChart.Data(1,2500));
        data.getData().add(new XYChart.Data(2,3100));
        data.getData().add(new XYChart.Data(3,2400));
        data.getData().add(new XYChart.Data(4,3300));
        data.getData().add(new XYChart.Data(5,9000));
        data.getData().add(new XYChart.Data(6,9900));
        data.getData().add(new XYChart.Data(7,17500));
        data.getData().add(new XYChart.Data(8,16300));
        data.getData().add(new XYChart.Data(9,15000));
        data.getData().add(new XYChart.Data(10,14000));
        data.getData().add(new XYChart.Data(11,18000));
        data.getData().add(new XYChart.Data(12,17000));


        //Adding series to the ScatterChart object
        CodingNinjasScatterChart.getData().add(data);

        //Setting group and view port properties
        Group root = new Group();
        root.getChildren().add(CodingNinjasScatterChart);
        Scene scene = new Scene(root,600,400);
        view.setScene(scene);
        view.setTitle("Coding Ninjas ScatterChart Example");
        view.show();
    }
    public static void main(String[] args) {
        launch();
    }
}

 

Output:

 

Frequently Asked Questions

What is JavaFX?

JavaFX is an open-source Software Development Kit for building feature-rich and modern GUI desktop and web applications. It is the outcome of the collaboration of a large pool of developers and companies to produce a toolkit to develop modern client applications.

What is the @override statement?

When inheriting to over-write an existing class, an overriding statement is used. Using @override neglects the method definition available in the parent class and uses the local definition.

What are import statements?

Import statement fetches pre-written libraries, which help in the faster development of applications. Organisations or language developers provide these libraries.

Conclusion

Finally, you have made it to the end of this article. Congratulations!! In this blog, you learnt about ScatterChart class in JavaFX. You went through the class, its constructors and a sample program.

After reading these interview questions, are you not feeling excited to read more articles on the topic of JavaFX? Don't worry; Coding Ninjas has you covered. To learn, see the JavaFX.

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enrol in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Please do upvote our blogs if you find them helpful and informative!

Happy learning!

Live masterclass