Do you think IIT Guwahati certified course can help you in your career?
No
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:
public ScatterChart( x-axis, y-axis): It initialises the scatter chart object with the specified axes.
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.
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");
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.
publicstaticvoidmain(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 publicclassCodingNinjasextendsApplication { @Override //start method with Stage object as parameter publicvoidstart(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 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(); } publicstaticvoidmain(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.