A pie chart, also known as a circle chart, which allow you to directly graph the overall proportions of each component. Pie chart helps you to analyze the abstract data more quickly and intuitively. This article will demonstrate how to create a pie chart in Excel in Java applications with the help of Spire.XLS for Java.
- Create a circle chart
- Create an exploded pie chart
- Create a pie chart with one exploded slice
- Create a Doughnut Chart
Install Spire.XLS for Java
First, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>12.7.0</version>
</dependency>
</dependencies>
Create a circle chart
Circle chart is a regular pie chart, which is used to display a single data series in a circle-type structure, with each sector representing a different category. Spire.XLS for Java offers the Worksheet.getCharts().add(ExcelChartType.Pie) method to add a pie chart to Excel worksheet.
- Create a Workbook instance.
- Get a specified worksheet using Workbook.getWorksheets().get(sheetIndex) method.
- Add some data to the worksheet and set values, styles and number format of the Excel cells.
- Add a pie chart to the worksheet using Worksheet.getCharts().add(ExcelChartType.Pie) method.
- Set data range for the chart through Chart.setDataRange() method.
- Set position, title for the chart.
- Save the document to file using Workbook.saveToFile() method.
import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;
import java.awt.*;
public class pieChart {
public static void main(String[] args) throws Exception {
//create a Workbook
Workbook workbook = new Workbook();
//get the first sheet and set its name
Worksheet sheet = workbook.getWorksheets().get(0);
sheet.setName("Pie Chart");
//set value of specified cell
sheet.getCellRange("A1").setValue("System");
sheet.getCellRange("A2").setValue("Linux");
sheet.getCellRange("A3").setValue("Mac OS");
sheet.getCellRange("A4").setValue("Windows");
sheet.getCellRange("A5").setValue("Unix");
sheet.getCellRange("B1").setValue("Users");
sheet.getCellRange("B2").setNumberValue(0.08);
sheet.getCellRange("B3").setNumberValue(0.15);
sheet.getCellRange("B4").setNumberValue(0.75);
sheet.getCellRange("B5").setNumberValue(0.02);
//set style of cells
sheet.getCellRange("A1:B1").setRowHeight(15);
sheet.getCellRange("A1:B1").getCellStyle().setColor(Color.darkGray);
sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white);
sheet.getCellRange("A1:B1").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
sheet.getCellRange("A1:B1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);
//Set number format of cells
sheet.getCellRange("B2:C5").getCellStyle().setNumberFormat("0%");
//Add a pie chart
Chart piechart = sheet.getCharts().add(ExcelChartType.Pie);
//Set data range for the chart
piechart.setDataRange(sheet.getCellRange("B2:B5"));
piechart.setSeriesDataFromRange(false);
//Set position of the chart
piechart.setLeftColumn(4);
piechart.setTopRow(1);
piechart.setRightColumn(10);
piechart.setBottomRow(20);
//Set and format chart title
piechart.setChartTitle("Users of System");
piechart.getChartTitleArea().isBold(true);
piechart.getChartTitleArea().setSize(12);
ChartSerie cs = piechart.getSeries().get(0);
cs.setCategoryLabels(sheet.getCellRange("A2:A5"));
cs.setValues(sheet.getCellRange("B2:B5"));
cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);
piechart.getPlotArea().getFill().setVisible(false);
//Save the result file
workbook.saveToFile("PieChart.xlsx", ExcelVersion.Version2016);
workbook.dispose();
}
}
Create an exploded pie chart.
The following are the main steps to create an exploded pie chart. Exploded Pie chart is used to pull all of the slices out of a pie chart and view the sectors separately in pieces.
- Create a Workbook instance.
- Get a specified worksheet using Workbook.getWorksheets().get(sheetIndex) method.
- Add some data to the worksheet and set values, styles and number format of the Excel cells.
- Add an exploded pie chart to the worksheet using Worksheet.getCharts().add(ExcelChartType.PieExploded) method.
- Set data range for the chart through Chart.setDataRange() method.
- Set position, title for the chart.
- Save the document to file using Workbook.saveToFile() method.
import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;
import java.awt.*;
public class pieExplodedchart {
public static void main(String[] args) throws Exception {
//create a Workbook
Workbook workbook = new Workbook();
//get the first sheet and set its name
Worksheet sheet = workbook.getWorksheets().get(0);
sheet.setName("Pie Chart");
//set value of specified cell
sheet.getCellRange("A1").setValue("System");
sheet.getCellRange("A2").setValue("Linux");
sheet.getCellRange("A3").setValue("Mac OS");
sheet.getCellRange("A4").setValue("Windows");
sheet.getCellRange("A5").setValue("Unix");
sheet.getCellRange("B1").setValue("Users");
sheet.getCellRange("B2").setNumberValue(0.08);
sheet.getCellRange("B3").setNumberValue(0.15);
sheet.getCellRange("B4").setNumberValue(0.75);
sheet.getCellRange("B5").setNumberValue(0.02);
//set style of cells
sheet.getCellRange("A1:B1").setRowHeight(15);
sheet.getCellRange("A1:B1").getCellStyle().setColor(Color.darkGray);
sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white);
sheet.getCellRange("A1:B1").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
sheet.getCellRange("A1:B1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);
//Set number format of cells
sheet.getCellRange("B2:C5").getCellStyle().setNumberFormat("0%");
//Add a pie chart
Chart piechart = sheet.getCharts().add(ExcelChartType.PieExploded);
//Set data range for the chart
piechart.setDataRange(sheet.getCellRange("B2:B5"));
piechart.setSeriesDataFromRange(false);
//Set position of the chart
piechart.setLeftColumn(4);
piechart.setTopRow(1);
piechart.setRightColumn(10);
piechart.setBottomRow(20);
//Set and format chart title
piechart.setChartTitle("Users of System");
piechart.getChartTitleArea().isBold(true);
piechart.getChartTitleArea().setSize(12);
ChartSerie cs = piechart.getSeries().get(0);
cs.setCategoryLabels(sheet.getCellRange("A2:A5"));
cs.setValues(sheet.getCellRange("B2:B5"));
cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);
piechart.getPlotArea().getFill().setVisible(false);
//Save the result file
workbook.saveToFile("PieExplodedChart.xlsx", ExcelVersion.Version2016);
workbook.dispose();
}
}
Create a pie chart with one exploded slice
It is very similar with the circle chart. You only need to exploded one slices from the whole pie chart.
- Create a Workbook instance.
- Get a specified worksheet using Workbook.getWorksheets().get(sheetIndex) method.
- Add some data to the worksheet and set values, styles and number format of the Excel cells.
- Add a pie chart to the worksheet using Worksheet.getCharts().add(ExcelChartType.Pie) method.
- Set data range for the chart through Chart.setDataRange() method.
- Set position, title for the chart.
- Exploded one specific slice from the whole pie chart using Chart.getSeries().get(0).getDataPoints().get(1).getDataFormat().setPercent(20) method.
- Save the document to file using Workbook.saveToFile() method.
import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;
import java.awt.*;
public class explodedPieChart {
public static void main(String[] args) throws Exception {
//create a Workbook
Workbook workbook = new Workbook();
//get the first sheet and set its name
Worksheet sheet = workbook.getWorksheets().get(0);
sheet.setName("Pie Chart");
//set value of specified cell
sheet.getCellRange("A1").setValue("System");
sheet.getCellRange("A2").setValue("Linux");
sheet.getCellRange("A3").setValue("Mac OS");
sheet.getCellRange("A4").setValue("Windows");
sheet.getCellRange("A5").setValue("Unix");
sheet.getCellRange("B1").setValue("Users");
sheet.getCellRange("B2").setNumberValue(0.08);
sheet.getCellRange("B3").setNumberValue(0.15);
sheet.getCellRange("B4").setNumberValue(0.75);
sheet.getCellRange("B5").setNumberValue(0.02);
//set style of cells
sheet.getCellRange("A1:B1").setRowHeight(15);
sheet.getCellRange("A1:B1").getCellStyle().setColor(Color.darkGray);
sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white);
sheet.getCellRange("A1:B1").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
sheet.getCellRange("A1:B1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);
//Set number format of cells
sheet.getCellRange("B2:C5").getCellStyle().setNumberFormat("0%");
//Add a pie chart
Chart piechart = sheet.getCharts().add(ExcelChartType.Pie);
//Set data range for the chart
piechart.setDataRange(sheet.getCellRange("B2:B5"));
piechart.setSeriesDataFromRange(false);
//Set position of the chart
piechart.setLeftColumn(4);
piechart.setTopRow(1);
piechart.setRightColumn(10);
piechart.setBottomRow(20);
//Set and format chart title
piechart.setChartTitle("Users of System");
piechart.getChartTitleArea().isBold(true);
piechart.getChartTitleArea().setSize(12);
ChartSerie cs = piechart.getSeries().get(0);
cs.setCategoryLabels(sheet.getCellRange("A2:A5"));
cs.setValues(sheet.getCellRange("B2:B5"));
cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);
piechart.getPlotArea().getFill().setVisible(false);
//Exploded the specific slice from the pie chart
piechart.getSeries().get(0).getDataPoints().get(1).getDataFormat().setPercent(20);
//Save the result file
workbook.saveToFile("ExplodedPieChart2.xlsx", ExcelVersion.Version2016);
workbook.dispose();
}
}
Create a Doughnut Chart
A Doughnut pie chart, with the center circle of the pie chart is missing, is a chart has the shape of a Doughnut.
- Create a Workbook instance.
- Get a specified worksheet using Workbook.getWorksheets().get(sheetIndex) method.
- Add some data to the worksheet and set values, styles and number format of the Excel cells.
- Add a doughnut chart to the worksheet using Worksheet.getCharts().add(ExcelChartType.Doughnut) method.
- Set data range for the chart through Chart.setDataRange() method.
- Set position, title for the chart.
- Save the document to file using Workbook.saveToFile() method.
import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;
import java.awt.*;
public class doughnutChart {
public static void main(String[] args) throws Exception {
//create a Workbook
Workbook workbook = new Workbook();
//get the first sheet and set its name
Worksheet sheet = workbook.getWorksheets().get(0);
sheet.setName("Doughnut Chart");
//set value of specified cell
sheet.getCellRange("A1").setValue("System");
sheet.getCellRange("A2").setValue("Linux");
sheet.getCellRange("A3").setValue("Mac OS");
sheet.getCellRange("A4").setValue("Windows");
sheet.getCellRange("A5").setValue("Unix");
sheet.getCellRange("B1").setValue("Users");
sheet.getCellRange("B2").setNumberValue(0.08);
sheet.getCellRange("B3").setNumberValue(0.15);
sheet.getCellRange("B4").setNumberValue(0.75);
sheet.getCellRange("B5").setNumberValue(0.02);
//set style of cells
sheet.getCellRange("A1:B1").setRowHeight(15);
sheet.getCellRange("A1:B1").getCellStyle().setColor(Color.darkGray);
sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white);
sheet.getCellRange("A1:B1").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
sheet.getCellRange("A1:B1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);
//Set number format of cells
sheet.getCellRange("B2:C5").getCellStyle().setNumberFormat("0%");
//Add a pie chart
Chart piechart = sheet.getCharts().add(ExcelChartType.Doughnut);
//Set data range for the chart
piechart.setDataRange(sheet.getCellRange("B2:B5"));
piechart.setSeriesDataFromRange(false);
//Set position of the chart
piechart.setLeftColumn(4);
piechart.setTopRow(1);
piechart.setRightColumn(10);
piechart.setBottomRow(20);
//Set and format chart title
piechart.setChartTitle("Users of System");
piechart.getChartTitleArea().isBold(true);
piechart.getChartTitleArea().setSize(12);
ChartSerie cs = piechart.getSeries().get(0);
cs.setCategoryLabels(sheet.getCellRange("A2:A5"));
cs.setValues(sheet.getCellRange("B2:B5"));
cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);
piechart.getPlotArea().getFill().setVisible(false);
//Save the result file
workbook.saveToFile("DoughnutChart.xlsx", ExcelVersion.Version2016);
workbook.dispose();
}
}
Conclusion
In this article, we have demonstrated how to create pie chart in Excel in Java. Spire.XLS for Java also support to create column chart, scatter chart or line chart to Excel worksheets. You can check the Excel forum for more features to adding more chart types to Excel files.
Top comments (0)