How to generate Pie Chart in Excel using Apache POI


In this example we will see how to generate pie chart in excel file using Apache POI library in Java. I will show you how to build this application using both maven and gradle tools. This is a standalone application and you can use this concept of pie chart generation in any Java based application. In this example we will create a 3D pie chart for displaying area-wise top seven countries.

A pie chart is a circular graphic with slices that illustrate the numerical proportions. In a pie chart the length of arch of each slice is proportional to the quantity it represents.

Pie charts are generally used to show percentage or proportional data in circular statistical graphic and usually the percentage represented by each category is provided next to the corresponding slice of pie. Pie charts are good for displaying data for around 6 categories or fewer.


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-pie-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 Pie Chart

Now we will create a Java class to create a pie chart in excel file using Apache POI library. The pie chart will display area-wise top seven countries in the world.

package com.roytuts.apache.poi.pie.chart;


import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
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.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 ApachePoiPieChart {

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

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

			XSSFSheet sheet = wb.createSheet("CountryPieChart");

			// Create row and put some cells in it. Rows and cells are 0 based.
			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);

			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);

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

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

			XDDFChartLegend legend = chart.getOrAddLegend();

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

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

			XDDFChartData data = chart.createData(ChartTypes.PIE3D, null, null);// chart.createData(ChartTypes.PIE,
																				// null, null);
			data.addSeries(countries, values);

			// Write output to an excel file
			try (FileOutputStream fileOut = new FileOutputStream("pie-chart-top-seven-countries.xlsx")) {


I will explain few important lines from the above class.

We create XSSFWorkbook object and create a sheet with name CountryPieChart.

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

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.

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 pie chart.

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

Then we create data on 3D pie chart. If you want normal pie chart then you can change it to only ChartTypes.PIE instead of ChartTypes.PIE3D.

To get different colors on different slices on pie chart we set true to vary color.

Then we plot data onto pie chart and chart gets generated.

Finally we write to the excel file – pie-chart-top-seven-countries.xlsx.

Testing the Application

Running the above main class will generate the pie chart in excel similar to the following image:

generate pie chart in excel using apache poi

Source Code


Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *