JavaFX Charts Stacked Area Chart - JavaFX

What is JavaFX Stacked Area Chart?

StackedArea Chart is a variation of the region Chart that shows trends of the contribution of every value (for example – overtime). The areas are stacked so that every series adjoins, but does not overlap the preceding series. This contrasts with the area chart in which each collection overlays the previous series.

Following is a Stacked chart depicting population growth.

JavaFX - Stacked Area Chart

In JavaFX, a Stacked location chart is represented by way of a class named StackedAreaChart. This class belongs to the package javafx.scene.chart. through instantiating this class, you may create a StackedAreaChart node in JavaFX.

Steps to Generate Stacked area Chart

To generate a stacked place chart in JavaFX, follow the steps given under.

Step 1: Creating a Class

Create a Java class and inherit the Application class of the package javafx.application. Then you may implement the start() technique of this class as follows.

Step 2: Defining the Axis

Define the X and Y axis of the stacked area chart and set labels to them. In our example, X axis represents various years from 1750 to 2050. These have major tick units for every 50 years. While the Y axis represents the population growth in millions.

Step 3: Creating the Stacked Area Chart

Create a line chart through instantiating the class named StackedAreaChart of the package javafx.scene.chart. To the constructor of this class, skip the objects representing the X and Y axis created in the preceding step.

Step 4: Preparing the Data

Instantiate the XYChart.Series class and add the data (a series of, x and y coordinates) to the Observable list of this class as follows −

Step 5: Add Data to the Stacked Area Chart

Add the data series prepared in the previous step to the stacked area chart as follows –

Step 6: Creating a Group Object

In the start() technique, create a set object through instantiating the class named Group, which belongs to the package javafx.scene.

Skip the StackedAreaChart (node) item created within the previous step as a parameter to the constructor of the group class. This need to be performed in order to add it to the group as follows −

Step 7: Creating a Scene Object

Create a Scene through instantiating the class named Scene, which belongs to the package javafx.scene. To this class, pass the group object (root) created in the previous step.

Similarly to the basis item, you may additionally pass double parameters representing height and width of the screen, along with the object of the group class as follows.

Step 8: Setting the Title of the Stage

You may set the name to the stage the use of the setTitle() technique of the stage class. The primaryStage is a stage object, that's passed to the start technique of the scene class as a parameter.

Using the primaryStage object, set the title of the scene as sample application as follows.

Step 9: Adding Scene to the Stage

You may add a Scene object to the stage using the technique setScene() of the class named stage. upload the Scene item prepared in the preceding steps the usage of this technique as follows.

Step 10: Displaying the Contents of the Stage

Display the contents of the scene using the method named show() of the Stage class as follows.

Step 11: Launching the Application

Launch the JavaFX application by calling the static method launch() of the Application class from the main method as follows.

Example

The following table lists out the population of different continents from year 1750 till year 2050.

Asia

Africa

Europe

America

Oceania

1750

502

106

163

18

2

1800

635

107

203

31

2

1850

809

111

276

54

2

1900

947

133

408

156

6

1950

1402

221

547

339

13

1999

3634

767

729

818

30

2050

5268

1766

628

1201

46

Following is a Java program which generates a stacked area chart depicting the above data using JavaFX.

Save this code in a file with the name StackedAreaChartExample.java.

Collect and execute the saved java file from the command prompt using the following commands.

On executing, the above program generates a JavaFX window displaying a stacked area chart as shown below.

JavaFX - Stacked Area Chart

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JavaFX Topics