JavaFX chart (1)

Translated from JavaFX – Charts

Typically, a chart is a graphical representation of data. There are various charts to represent data, such as bar chart, pie chart, line chart, scatter chart, etc.

JavaFX supports various pie charts and XY charts. Charts represented on the XY plane include AreaChart, BarChart, BubbleChart, LineChart, ScatterChart, StackedAreaChart, StackedBarChart, etc.

Each chart is represented by a class, and all such charts belong to the package javafx.scene.chart. The class named Chart is the base class for all charts in JavaFX, and XYChart is the base class for all charts drawn on the XY plane.

Create a chart

To create a graph you need ?

  • define the axes of the chart
  • Instantiate the corresponding class
  • Prepare and pass data to the chart

Instantiate each class

To create a chart, instantiate its respective class. For example, if you want to create a line chart, you need to instantiate a class named Line as follows ?

LineChart linechart = new LineChart(xAxis, yAxis);

As shown in the above code, when instantiating, two objects representing the X-axis and Y-axis of the chart need to be passed.

Define the axis

In general, the axes of a graph can be represented as ?

  • Figures such as population, age and population
  • Categories such as days of the week, countries.

In JavaFX, an axis is an abstract class representing an X or Y axis. It has two subclasses to define each type of axis, namely CategoryAxis and NumberAxis, as shown in the following figure ?

Category Axis – By instantiating this class, you can define (create) an X-axis or a Y-axis, with each value representing a category. You can define a category axis by instantiating this class as follows ?

CategoryAxis xAxis = new CategoryAxis();

For this axis, you need to set the list of categories and labels to the axis as follows ?

//setting the list of categories.
xAxis.setCategories(FXCollections.<String>observableArrayList
   (Arrays. asList("n ame1", "name2"….)));

//Setting label to the axis
xAxis.setLabel("name of the axis ");

NumberAxis – By instantiating this class, you can define (create) an X-axis or a Y-axis, each value representing a numeric value. You can use any Number type for this Axis, Long, Double, BigDecimal, etc. You can define Number axis by instantiating this class as follows ?

//Defining the axis
NumberAxis yAxis = new NumberAxis();

//Setting label to the axis
yAxis.setLabel("name of the axis");

Pass data to XY chart

All XY charts are represented along the XY plane. To plot a set of points in a chart, we need to specify a series of XY coordinates.

The class of the javafx.scene.chart package is a class with which data can be sent to the chart. This class contains an observable list of named series. You can get this list using getData() method of XYChart.Series class as follows ?

ObservableList list = series. getData();

Among them, series is an object of the XYChart.Series class. You can add data to this list using the add() method as follows ?

list.add(new XYChart.Data(x-axis data, y-axis data));

These two lines can be written together as follows ?

series.getData().add(new XYChart.Data(x-axis data, y-axis data));

The following table gives a description of the various charts (classes) provided by JavaFX

S.No Graphics and description
1 pie chart

A pie chart is a slice that represents values as circles with different colors. These slices are labeled, and the values corresponding to each slice are represented in the graph.

In JavaFX, a pie chart is represented by a class named PieChart. This class belongs to package javafx.scene.chart.

2 line chart

A line or line chart displays information as a series of data points (markers) connected by straight line segments. A line chart shows how data changes at equal time frequencies.

In JavaFX, a line chart is represented by a class named LineChart. This class belongs to package javafx.scene.chart. By instantiating this class, you can create a LineChart node in JavaFX.

3 area chart

Area charts are used to draw area-based charts. It plots the area between the given series point and the axis. Typically, this chart is used to compare two quantities.

In JavaFX, an Area chart is represented by a class named AreaChart. This class belongs to package javafx.scene.chart. By instantiating this class, you can create AreaChart nodes in JavaFX.

4 bar graph

Bar charts are used to represent grouped data using rectangular bars. The length of the bars depicts these values. Bars in a bar chart can be drawn vertically or horizontally.

In JavaFX, a bar chart is represented by a class named BarChart. This class belongs to package javafx.scene.chart. By instantiating this class, you can create a BarChart node in JavaFX.

5 bubble chart

Bubble charts are used to tile 3D data. The third dimension will be represented by the size (radius) of the bubble.

In JavaFX, a bubble chart is represented by a class named BubbleChart. This class belongs to package javafx.scene.chart. By instantiating this class, you can create BubbleChart nodes in JavaFX.

6 Scatterplot

A scatterplot is a graph that uses the values of two variables plotted on a Cartesian plane. It is often used to find the relationship between two variables.

In JavaFX, a Scatter chart is represented by a class named ScatterChart. This class belongs to package javafx.scene.chart. By instantiating this class, you can create a ScatterChart node in JavaFX.

7 Stacked area chart

In JavaFX, a stacked area chart is represented by a class named StackedAreaChart.

This class belongs to package javafx.scene.chart. By instantiating this class, you can create a StackedAreaChart node in JavaFX.

8 Stacked bar chart

In JavaFX, a Stacked Bar chart is represented by a class named StackedBarChart.

This class belongs to package javafx.scene.chart. By instantiating this class, you can create a StackedBarChart node in JavaFX.