How to create a stacked bar chart using JavaFX?


The bar chart accepts a series of data points (x, y) as input values, and creates bars representing their values. Typically, these charts are used to represent the value of a category. Depending on the axis of the category the bars of a bar chart can be vertical or horizontal.

StackedBarChart is a variant 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.

In JavaFX, you can create a stacked bar chart by instantiating the javafx.scene.chart.StackedBarChart class.

While instantiating this class you must pass the two objects of the Axis class representing the x and y-axis (as parameters of the constructor). Since the Axis class is abstract you need to pass objects of its concrete subclasses, NumberAxis (for numerical values) or, CategoryAxis (String values).

Once you create the axes you can set labels to them using the setLabel() method.

Setting data

The XYChart.Series represents the series of data items. You can create a series of points for the bars by instantiating this class. This class contains an observable list that holds all the points in the series.

The XYChart.Data represents a specific data point in the x-y plane. To create a point, you need to instantiate this class by passing the x and y values of the particular point.

Therefore, to create data for a bar −

  • Create a required number of points by instantiating the XYChart.Data class.

  • Create a series by instantiating the XYChart.Series class.

  • Get the observable list of the XYChart.Series class using the getData()method.

  • Add the created data points to the list using the add() or addAll() methods.

  • Add the created data series to the area chart as −

barChart.getData().add(series);

Example

import javafx.application.Application;
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;
import javafx.scene.layout.StackPane;
public class StackedBarChartExample extends Application {
   @Override
   public void start(Stage stage) {
      //Defining the axes
      CategoryAxis xAxis = new CategoryAxis();
      xAxis.setLabel("Year");
      NumberAxis yAxis = new NumberAxis();
      yAxis.setLabel("Population (In millions)");
      //Creating the Bar chart
      StackedBarChart stackedBarChart = new StackedBarChart(xAxis, yAxis);
      stackedBarChart.setTitle("World Population by Region");
      //Prepare XYChart.Series objects by setting data
      XYChart.Series series1 = new XYChart.Series<>();
      series1.setName("Asia");
      series1.getData().add(new XYChart.Data<>("2012", 900));
      series1.getData().add(new XYChart.Data<>("2013", 1000));
      series1.getData().add(new XYChart.Data<>("2014", 1170));
      series1.getData().add(new XYChart.Data<>("2015", 1250));
      series1.getData().add(new XYChart.Data<>("2016", 1530));
      XYChart.Series series2 = new XYChart.Series<>();
      series2.setName("Europe");
      series2.getData().add(new XYChart.Data<>("2012", 390));
      series2.getData().add(new XYChart.Data<>("2013", 400));
      series2.getData().add(new XYChart.Data<>("2014", 440));
      series2.getData().add(new XYChart.Data<>("2015", 480));
      series2.getData().add(new XYChart.Data<>("2016", 540));
      //Setting the data to bar chart
      stackedBarChart.getData().addAll(series1, series2);
      //Creating a stack pane to hold the chart
      StackPane pane = new StackPane(stackedBarChart);
      //Setting the Scene
      Scene scene = new Scene(pane, 595, 300);
      stage.setTitle("Stacked Bar Chart");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 19-May-2020

185 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements