In this brief tutorial, you will learn how to create Excel chart in Java by following a step-by-step approach. It is very common to create a workbook programmatically and then fill data manually or import data from some external source to create a chart. At the end of the process, this Excel file is saved as an XLSX file on a disc.
Steps to Create Excel Chart in Java
- First, using the Maven repository, add a reference to Aspose.Cells library in the project
- Add a reference to Workbook, Worksheet, and other required classes using Import in the program
- Create an empty workbook having a worksheet by default
- Get a reference to the first sheet and fill in data to be rendered in the chart
- In the first sheet, create a chart say a pie chart with the initial settings
- Set chart data series, category, chart title, and data labels of each pie chart slice
- Save the workbook having data and chart
Using the above steps you can create an Excel chart by simply creating a workbook and then filling sample data for the chart. Once the data is filled, a pie chart is created with basic settings, and then chart data series, category, title, and data labels of each slice are added. You can set many other properties of this chart as well as demonstrated in the following sample code.
Code to Make a Chart in Excel in Java
In this sample code, we learned how to make a graph in Excel in Java and save it as an XLSX file. However, if you want to convert this chart to an image, refer to the article on how to convert Excel chart to JPG in Java.
Note that no other component or libraries like Interop or MS Excel is required for running the above code.