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
import com.aspose.cells.License; | |
import com.aspose.cells.Cells; | |
import com.aspose.cells.Chart; | |
import com.aspose.cells.ChartType; | |
import com.aspose.cells.Color; | |
import com.aspose.cells.DataLabels; | |
import com.aspose.cells.Workbook; | |
import com.aspose.cells.Worksheet; | |
public class CreateExcelChartInJava { | |
public static void main(String[] args) throws Exception { //main function for CreateExcelChartInJava | |
// Initialize a license to avoid trial version watermark while creating Excel chart | |
License license = new License(); | |
license.setLicense("Aspose.Cells.lic"); | |
// Create an empty Excel Workbook | |
Workbook ExcelWorkbookForChart = new Workbook(); | |
// Get reference to the first worksheet for creating chart | |
Worksheet ExcelChartWorksheet = ExcelWorkbookForChart.getWorksheets().get(0); | |
// Set sheet name | |
ExcelChartWorksheet.setName("PieChart"); | |
// Get worksheet cells collection to set values | |
Cells WorksheetCells = ExcelChartWorksheet.getCells(); | |
// Set values in the cells to create a pie chart | |
WorksheetCells.get("A1").putValue("Quarters"); | |
WorksheetCells.get("A2").putValue("1st_Qtr"); | |
WorksheetCells.get("A3").putValue("2nd_Qtr"); | |
WorksheetCells.get("A4").putValue("3rd_Qtr"); | |
WorksheetCells.get("A5").putValue("4th_Qtr"); | |
WorksheetCells.get("B1").putValue("Sales"); | |
WorksheetCells.get("B2").putValue(6.3); | |
WorksheetCells.get("B3").putValue(3.1); | |
WorksheetCells.get("B4").putValue(2.2); | |
WorksheetCells.get("B5").putValue(1.9); | |
// Create a Pie chart and get its reference for setting chart properties | |
int chart_Index = 0; | |
chart_Index = ExcelChartWorksheet.getCharts().add(ChartType.PIE, 10, 2, 34, 13); | |
Chart WorksheetChart = ExcelChartWorksheet.getCharts().get(chart_Index); | |
// Set the chart data series and category | |
WorksheetChart.getNSeries().add("B2:B5", true); | |
WorksheetChart.getNSeries().setCategoryData("A2:A5"); | |
// Set properties of chart title | |
WorksheetChart.getTitle().setText("Sales By Quarter"); | |
WorksheetChart.getTitle().getFont().setColor(Color.getBlue()); | |
WorksheetChart.getTitle().getFont().setBold(true); | |
WorksheetChart.getTitle().getFont().setSize(11); | |
// Set the data labels of each pie chart slice | |
DataLabels data_labels; | |
for (int i = 0; i < WorksheetChart.getNSeries().getCount(); i++) | |
{ | |
data_labels = WorksheetChart.getNSeries().get(i).getDataLabels(); | |
data_labels.setShowValue(true); | |
data_labels.setShowPercentage(true); | |
} | |
// Save the workbook containing the chart | |
ExcelWorkbookForChart.save("pie_chart.xlsx"); | |
} | |
} |
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.