How to generate stacked bar chart or column chart in Excel using Apache POI


In this tutorial I will show you how to create stacked bar chart or column chart in excel file using Java based Apache POI library. I will also show you how to build this application using both maven and gradle build tools. This is a standalone application and you can use this concept of stacked bar chart or column chart generation in any Java based application. In this example we will create stacked bar chart and column chart for displaying area-wise top seven countries. Population will be stacked on top of the area.

A stacked bar chart is used to break down and compare parts of a whole. A bar in the chart represents a whole and segments in the bar represent different parts or subcategories of the whole.


At least Java 8, Eclipse 4.12, Apache POI 4.1.1, Gradle 5.6, maven 3.6.1

Create Project

The name of the project is apache-poi-excel-stacked-bar-column-chart.

If you are creating maven based project then use below pom.xml file for standalone maven project in Eclipse.

<project xmlns="" xmlns:xsi=""
		<java.version>1.8 or 12</java.version>
		<!-- required only if you are using jdk 9 or above -->

If you are creating gradle based project in Eclipse, then use below build.gradle script to include the required dependencies.

plugins {
    id 'java-library'

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {
    implementation 'org.apache.poi:poi-ooxml:4.1.1'
    //required only for jdk 9 or above

Create Stacked Bar and Column Chart

Now we will create a Java class to create stacked bar chart and column chart in excel file using Apache POI library. The charts will display area-wise top seven countries in the world. On top of the area population will be stacked.



import org.apache.poi.xddf.usermodel.chart.AxisPosition;
import org.apache.poi.xddf.usermodel.chart.BarDirection;
import org.apache.poi.xddf.usermodel.chart.BarGrouping;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ApachePoiStackedBarChart {

	public static void main(String[] args) throws IOException {

	public static void stackedBarChart() throws FileNotFoundException, IOException {
		try (XSSFWorkbook wb = new XSSFWorkbook()) {

			String sheetName = "CountryStackedBarChart";// "CountryStackedColumnChart";

			XSSFSheet sheet = wb.createSheet(sheetName);

			// Country Names
			Row row = sheet.createRow((short) 0);

			Cell cell = row.createCell((short) 0);

			cell = row.createCell((short) 1);

			cell = row.createCell((short) 2);

			cell = row.createCell((short) 3);

			cell = row.createCell((short) 4);

			cell = row.createCell((short) 5);

			cell = row.createCell((short) 6);

			// Country Area
			row = sheet.createRow((short) 1);

			cell = row.createCell((short) 0);

			cell = row.createCell((short) 1);

			cell = row.createCell((short) 2);

			cell = row.createCell((short) 3);

			cell = row.createCell((short) 4);

			cell = row.createCell((short) 5);

			cell = row.createCell((short) 6);

			// Country Population
			row = sheet.createRow((short) 2);

			cell = row.createCell((short) 0);

			cell = row.createCell((short) 1);

			cell = row.createCell((short) 2);

			cell = row.createCell((short) 3);

			cell = row.createCell((short) 4);

			cell = row.createCell((short) 5);

			cell = row.createCell((short) 6);

			XSSFDrawing drawing = sheet.createDrawingPatriarch();
			XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 4, 7, 26);

			XSSFChart chart = drawing.createChart(anchor);
			chart.setTitleText("Area-wise Top Seven Countries");

			XDDFChartLegend legend = chart.getOrAddLegend();

			XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
			XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
			leftAxis.setTitle("Area & Population");

			XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromStringCellRange(sheet,
					new CellRangeAddress(0, 0, 0, 6));

			XDDFNumericalDataSource<Double> area = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
					new CellRangeAddress(1, 1, 0, 6));

			XDDFNumericalDataSource<Double> population = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
					new CellRangeAddress(2, 2, 0, 6));

			XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
			XDDFChartData.Series series1 = data.addSeries(countries, area);
			series1.setTitle("Area", null);
			XDDFChartData.Series series2 = data.addSeries(countries, population);
			series2.setTitle("Population", null);

			// correcting the overlap so bars really are stacked and not side by side
			chart.getCTChart().getPlotArea().getBarChartArray(0).addNewOverlap().setVal((byte) 100);

			// in order to transform a bar chart into a column chart, you just need to
			// change the bar direction
			XDDFBarChartData bar = (XDDFBarChartData) data;
			// bar.setBarDirection(BarDirection.COL);

			// Write output to an excel file
			String filename = "bar-chart-top-seven-countries.xlsx";// "column-chart-top-seven-countries.xlsx";
			try (FileOutputStream fileOut = new FileOutputStream(filename)) {


We create XSSFWorkbook object and create a sheet with name CountryStackedBarChart or CountryStackedColumnChart.

Next we create three rows and 7 columns in the excel sheet. First row contains country names, second row contains each country’s area and third row contains each country’s population.

Please note the population data are not correct and it’s just representational purpose.

Then we create XSSFDrawing object and XSSFClientAnchor object.

In XSSFClientAnchor we set the range of the rows and columns or cells. The first two values indicate coordinates of x and y of first cell respectively. The next two values indicate coordinates of x and y of second cell respectively. The next two values indicate starting row index and column index of chart area. The next two values indicate ending row index and column index of chart area.

Related Posts:

Now we create chart from the XSSFClientAnchor object using createChart(). We set title to the chart.

The legends are shown on right side of the bar chart or column chart.

Then we create bottom axis and left axis. Bottom axis adds label “Country” and left axis adds label “Area & Population”.

Next we define data source for drawing chart. We set country names as string and area & population as numeric values.

We add numeric values in series as stacked bar.

Then we plot the data onto bar chart or column chart.

Next we need to set direction whether we want it to be bar chart or column chart.

Finally we write the chart into excel file.

Testing the Application

Running the above main class will generate the stacked bar chart and column chart in excel similar to the following images.

Stacked Bar Chart

stacked bar chart or column chart in excel using apache poi

Stacked Column Chart

stacked bar chart or column chart in excel using apache poi

Source Code


Thanks for reading.

Related posts

One Thought to “How to generate stacked bar chart or column chart in Excel using Apache POI”

  1. Ira

    I am using the above code for clustered bar chart grouping.

    But my output is eliminating data for first and last value.
    Can you please share the proper code or changes I should make for clustered bar graph.

Leave a Comment