Charts and graphs are used to summarize and visually represent the data. They give an insight that can further be used to make the decisions. Charts are considered to be an integral part of Excel spreadsheets and are widely used in various applications. In this article, you will learn how to generate charts programmatically from the data provided in the Excel worksheets. Particularly, the article covers how to create different types of charts in Excel using Java.
- Java API to Create Excel Charts
- Create a Chart in Excel using Java
- Create a Line Chart in Excel using Java
- Generate a Pyramid Chart in Excel using Java
Java API to Create Excel Charts
In order to create charts in Excel files, we’ll use Aspose.Cells for Java. It is a powerful API that allows you to implement Excel automation features in your Java applications. Furthermore, it lets you generate a variety of charts within a few lines of code. You can either download the API or install it using the following Maven configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>21.6</version>
</dependency>
Supported Excel Chart Types
Aspose.Cells for Java provides a complete set of standard chart types. The list includes, but not limited to:
- Column
- Bar
- Line
- Pie
- Scatter
- Area
- Doughnut
- Radar
- Surface 3D
- Bubble
- Stock
- Cylinder
- Cone
- Pyramid
For more details about the supported Excel charts, visit this article.
Create a Chart in Excel using Java
The following are the steps to create a chart in an Excel file using Java.
- Create an instance of the Workbook class to load an existing or create a new Excel file.
- Get the reference of the desired worksheet into a Worksheet object.
- Add data to the worksheet (optional).
- Create a new chart by specifying its type using Worksheet.getCharts().add() method.
- Get the reference of the added chart into a Chart object.
- Set data for the chart using Chart.setChartDataRange(String, boolean) method.
- Save the Excel workbook using Workbook.save(String, SaveFormat) method.
The following code sample shows how to create a chart in Excel using Java.
For demonstration, lets now create some other types of charts in an Excel workbook.
Create a Line Chart in Excel using Java
The following are the steps to create a line chart in Excel using Java.
- Create an instance of the Workbook class to load an existing or create a new Excel file.
- Get the reference of the desired worksheet into a Worksheet object.
- Add data to the worksheet (optional).
- Create a new Line chart by specifying the type ChartType.LINE using Worksheet.getCharts().add() method.
- Get the reference of the added chart into a Chart object.
- Set data for the chart using Chart.setChartDataRange(String, boolean) method.
- Save the Excel workbook using Workbook.save(String, SaveFormat) method.
The following code sample shows how to create a line chart in Excel using Java.
Create a Pyramid Chart in Excel using Java
The following are the steps to create a pyramid chart in Excel using Java.
- Create an instance of the Workbook class to load an existing or create a new Excel file.
- Get the reference of the desired worksheet into a Worksheet object.
- Add data to the worksheet (optional).
- Create a new Pyramid chart by specifying the type ChartType.PYRAMID using Worksheet.getCharts().add() method.
- Get the reference of the added chart into a Chart object.
- Add NSeries (chart data source) to the chart using Chart.getNSeries().add(String, boolean) method.
- Save the Excel workbook using Workbook.save(String, SaveFormat) method.
The following code sample shows how to create a pyramid chart in Excel using Java.
Get a Free API License
You can use Aspose.Cells for Java without evaluation limitations by getting a temporary license.
Conclusion
In this article, you have seen how to create charts in Excel worksheets using Java. Particularly, you have learned how to create column, line, and pyramid charts in Excel programmatically. You can use the same method to create other types of charts in Excel worksheets. To learn more about the API, visit the documentation. In case you would have any queries, contact us via our forum.