JavaFX - Stacked Bar Chart



StackedBarChart is a variation of a BarChart, which plots bars indicating data values for a category. The bars can be vertical or horizontal depending on which axis is the category axis. The bar for each series is stacked on top of the previous series.

The following is a Stacked Bar Chart, which depicts the population growth.

Stacked Bar Chart

In JavaFX, a Stacked Bar Chart is represented by a class named StackedBarChart. This class belongs to the package javafx.scene.chart. By instantiating this class, you can create a StackedBarChart node in JavaFX.

Steps to Generate Stacked Bar Chart

To generate a Stacked Bar Chart in JavaFX, follow the steps given below.

Step 1: Creating a Class

Create a Java class and inherit the Application class of the package javafx.application. You can then implement the start() method of this class as follows.

public class ClassName extends Application { 
   @Override     
   public void start(Stage primaryStage) throws Exception {     
   }    
}

Step 2: Defining the Axis

Define the X and Y axis of the stacked bar chart and set labels to them. In our example, X axis represents the continents and the y axis represents the population in millions.

//Defining the x axis               
CategoryAxis xAxis = new CategoryAxis();    

xAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList
   ("Africa", "America", "Asia", "Europe", "Oceania"))); 
xAxis.setLabel("category");  

//Defining the y axis 
NumberAxis yAxis = new NumberAxis(); 
yAxis.setLabel("Population (In millions)");

Step 3: Creating the Stacked Bar Chart

Create a line chart by instantiating the class named StackedBarChart of the package javafx.scene.chart. To the constructor of this class, pass the objects representing the X and Y axis created in the previous step.

//Creating the Bar chart 
StackedBarChart<String, Number> stackedBarChart = 
new StackedBarChart<>(xAxis, yAxis);         
stackedBarChart.setTitle("Historic World Population by Region"); 

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 −

//Prepare XYChart.Series objects by setting data 
XYChart.Series<String, Number> series1 = new XYChart.Series<>(); 
series1.setName("1800"); 
series1.getData().add(new XYChart.Data<>("Africa", 107)); 
series1.getData().add(new XYChart.Data<>("America", 31)); 
series1.getData().add(new XYChart.Data<>("Asia", 635)); 
series1.getData().add(new XYChart.Data<>("Europe", 203)); 
series1.getData().add(new XYChart.Data<>("Oceania", 2)); 
XYChart.Series<String, Number> series2 = new XYChart.Series<>();  

series2.setName("1900"); 
series2.getData().add(new XYChart.Data<>("Africa", 133)); 
series2.getData().add(new XYChart.Data<>("America", 156)); 
series2.getData().add(new XYChart.Data<>("Asia", 947)); 
series2.getData().add(new XYChart.Data<>("Europe", 408)); 
series1.getData().add(new XYChart.Data<>("Oceania", 6));  

XYChart.Series<String, Number> series3 = new XYChart.Series<>(); 
series3.setName("2008"); 
series3.getData().add(new XYChart.Data<>("Africa", 973)); 
series3.getData().add(new XYChart.Data<>("America", 914)); 
series3.getData().add(new XYChart.Data<>("Asia", 4054)); 
series3.getData().add(new XYChart.Data<>("Europe", 732)); 
series1.getData().add(new XYChart.Data<>("Oceania", 34));

Step 5: Add Data to the Stacked Bar Chart

Add the data series prepared in the previous step to the bar chart as follows −

//Setting the data to bar chart        
stackedBarChart.getData().addAll(series1, series2, series3); 

Step 6: Creating a Group Object

In the start() method, create a group object by instantiating the class named Group. This belongs to the package javafx.scene.

Pass the StackedBarChart (node) object created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows −

Group root = new Group(stackedBarChart); 

Step 7: Creating a Scene Object

Create a Scene by 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.

In addition to the root object, you can also pass two double parameters representing height and width of the screen, along with the object of the Group class as follows.

Scene scene = new Scene(group ,600, 300);

Step 8: Setting the Title of the Stage

You can set the title to the stage using the setTitle() method of the Stage class. This primaryStage is a Stage object, which is passed to the start method of the scene class as a parameter.

Using the primaryStage object, set the title of the scene as Sample Application as follows.

primaryStage.setTitle("Sample Application"); 

Step 9: Adding Scene to the Stage

You can add a Scene object to the stage using the method setScene() of the class named Stage. Add the Scene object prepared in the previous steps using this method as follows.

primaryStage.setScene(scene);

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.

primaryStage.show();

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.

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

Example

The following table lists out the population in various continents in the years 1800, 1900 and 2008.

Africa America Asia Europe Oceania
1800 107 31 635 203 2
1900 133 156 947 408 6
2008 973 914 4054 732 34

Following is a Java program that generates a stacked bar chart depicting the above data, using JavaFX.

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

import java.util.Arrays; 
import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.chart.CategoryAxis; 
import javafx.stage.Stage; 
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.StackedBarChart; 
import javafx.scene.chart.XYChart; 
         
public class StackedBarChartExample extends Application { 
   @Override 
   public void start(Stage stage) {     
      //Defining the axes               
      CategoryAxis xAxis = new CategoryAxis();    
      xAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList
         ("Africa", "America", "Asia", "Europe", "Oceania"))); 
      
      xAxis.setLabel("category");
      NumberAxis yAxis = new NumberAxis(); 
      yAxis.setLabel("Population (In millions)");    
         
      //Creating the Bar chart 
      StackedBarChart<String, Number> stackedBarChart = 
         new StackedBarChart<>(xAxis, yAxis);         
      stackedBarChart.setTitle("Historic World Population by Region"); 
         
      //Prepare XYChart.Series objects by setting data 
      XYChart.Series<String, Number> series1 = new XYChart.Series<>();  
      series1.setName("1800"); 
      series1.getData().add(new XYChart.Data<>("Africa", 107)); 
      series1.getData().add(new XYChart.Data<>("America", 31));  
      series1.getData().add(new XYChart.Data<>("Asia", 635)); 
      series1.getData().add(new XYChart.Data<>("Europe", 203)); 
      series1.getData().add(new XYChart.Data<>("Oceania", 2)); 
         
      XYChart.Series<String, Number> series2 = new XYChart.Series<>(); 
      series2.setName("1900"); 
      series2.getData().add(new XYChart.Data<>("Africa", 133)); 
      series2.getData().add(new XYChart.Data<>("America", 156)); 
      series2.getData().add(new XYChart.Data<>("Asia", 947)); 
      series2.getData().add(new XYChart.Data<>("Europe", 408)); 
      series1.getData().add(new XYChart.Data<>("Oceania", 6));  
     
      XYChart.Series<String, Number> series3 = new XYChart.Series<>(); 
      series3.setName("2008"); 
      series3.getData().add(new XYChart.Data<>("Africa", 973)); 
      series3.getData().add(new XYChart.Data<>("America", 914)); 
      series3.getData().add(new XYChart.Data<>("Asia", 4054)); 
      series3.getData().add(new XYChart.Data<>("Europe", 732)); 
      series1.getData().add(new XYChart.Data<>("Oceania", 34)); 
         
      //Setting the data to bar chart
      stackedBarChart.getData().addAll(series1, series2, series3); 
         
      //Creating a Group object  
      Group root = new Group(stackedBarChart); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 400);  
      
      //Setting title to the Stage 
      stage.setTitle("stackedBarChart"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show();         
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}

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

javac StackedBarChartExample.java 
java StackedBarChartExample 

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

Stacked Bar Example
javafx_charts.htm
Advertisements