Charts and graphs are used to summarize and visually represent data. They provide insights that can be further used to make decisions. Charts are considered an integral part of Excel spreadsheets and are used in a wide variety of applications. In this article, you’ll learn how to programmatically generate charts from data provided in Excel worksheets. In particular, this article showed you how to use Java to create different types of charts in Excel.

  • Create charts in Excel using Java
  • Use Java to create line charts in Excel
  • Generate pyramids in Excel using Java

To create charts in Excel files, we’ll use Aspose.Cells for Java, a powerful API that allows you to implement Excel automation in Your Java applications. In addition, it allows you to generate various charts in a few lines of code. Download the latest version of Aspose.Cells for Java

Create charts in Excel using Java

Here are the steps to create a chart in an Excel file using Java.

  • Create an instance of the Workbook class to load an existing Excel file or create a new Excel file.
  • Get a reference to the required Worksheet into the Worksheet object.
  • Add data to a worksheet (optional).
  • Create a new chart by specifying its type using the worksheet.getCharts ().add() method.
  • Gets a reference to the added Chart into the Chart object.
  • Set the data for the Chart using the chart.setChartDatarange (String, Boolean) method.
  • Save the Excel Workbook using the workbook.save (String, SaveFormat) method.

The following code example shows how to create a chart in Excel using Java.

// Create a new workbook
Workbook workbook = new Workbook();

// Obtain the reference of the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);

// Add sample values to cells
worksheet.getCells().get("A2").putValue("Category1");
worksheet.getCells().get("A3").putValue("Category2");
worksheet.getCells().get("A4").putValue("Category3");

worksheet.getCells().get("B1").putValue("Column1");
worksheet.getCells().get("B2").putValue(4);
worksheet.getCells().get("B3").putValue(20);
worksheet.getCells().get("B4").putValue(50);
worksheet.getCells().get("C1").putValue("Column2");
worksheet.getCells().get("C2").putValue(50);
worksheet.getCells().get("C3").putValue(100);
worksheet.getCells().get("C4").putValue(150);

// Add a chart to the worksheet
int chartIndex = worksheet.getCharts().add(ChartType.COLUMN, 5, 0, 15, 5);

// Access the instance of the newly added chart
Chart chart = worksheet.getCharts().get(chartIndex);

// Set chart data source as the range "A1:C4"
chart.setChartDataRange("A1:C4", true);
workbook.save("Column-Chart.xlsx", SaveFormat.XLSX);
Copy the code

Use Java to create line charts in Excel

Here are the steps to create a line chart in Excel using Java.

  • Create an instance of the Workbook class to load an existing one or create a new Excel file.
  • Gets a reference to the required worksheet into a worksheet object.
  • Add data to the worksheet (optional).
  • Use the worksheet.getCharts ().add() method to specify the charttype.line type to create a new LINE chart.
  • Get a reference to the added Chart in the Chart object.
  • Set the data for the Chart using the chart.setChartDatarange (String, Boolean) method.
  • Save the Excel Workbook using the workbook.save (String, SaveFormat) method.

The following code example shows how to create line charts in Excel using Java.

// Instantiate a Workbook object
Workbook workbook = new Workbook();

// Obtain the reference of the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);

// Add sample values to cells
worksheet.getCells().get("A2").putValue("Category1");
worksheet.getCells().get("A3").putValue("Category2");
worksheet.getCells().get("A4").putValue("Category3");

worksheet.getCells().get("B1").putValue("Column1");
worksheet.getCells().get("B2").putValue(4);
worksheet.getCells().get("B3").putValue(20);
worksheet.getCells().get("B4").putValue(50);
worksheet.getCells().get("C1").putValue("Column2");
worksheet.getCells().get("C2").putValue(50);
worksheet.getCells().get("C3").putValue(100);
worksheet.getCells().get("C4").putValue(150);

// Add a chart to the worksheet
int chartIndex = worksheet.getCharts().add(ChartType.LINE, 5, 0, 15, 5);

// Access the instance of the newly added chart
Chart chart = worksheet.getCharts().get(chartIndex);

// Set chart data source as the range "A1:C4"
chart.setChartDataRange("A1:C4", true);

// Save the Excel file
workbook.save("Line-Chart.xls", SaveFormat.XLSX);
Copy the code

Create pyramids in Excel using Java

Here are the steps to create a pyramid in Excel using Java.

  • Create an instance of the Workbook class to load an existing one or create a new Excel file.
  • Gets a reference to the required worksheet into a worksheet object.
  • Add data to the worksheet (optional).
  • Create a new PYRAMID chart by specifying type charttype.pyramid using the worksheet.getCharts ().add() method.
  • Get the reference information for the added Chart as a Chart object.
  • Use chart.getnSeries ().add(String, Boolean) method to add NSeries(Chart data source) to the Chart.
  • Save the Excel Workbook using the workbook.save (String, SaveFormat) method.

The following code example shows how to create a pyramid in Excel using Java.

// Instantiate a Workbook object
Workbook workbook = new Workbook();

// Obtain the reference of the first worksheet
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);

// Add some sample value to cells
Cells cells = sheet.getCells();
Cell cell = cells.get("A1");
cell.setValue(50);
cell = cells.get("A2");
cell.setValue(100);
cell = cells.get("A3");
cell.setValue(150);
cell = cells.get("B1");
cell.setValue(4);
cell = cells.get("B2");
cell.setValue(20);
cell = cells.get("B3");
cell.setValue(180);
cell = cells.get("C1");
cell.setValue(320);
cell = cells.get("C2");
cell.setValue(110);
cell = cells.get("C3");
cell.setValue(180);
cell = cells.get("D1");
cell.setValue(40);
cell = cells.get("D2");
cell.setValue(120);
cell = cells.get("D3");
cell.setValue(250);

// Access chart collection
ChartCollection charts = sheet.getCharts();

// Add a chart to the worksheet
int chartIndex = charts.add(ChartType.PYRAMID, 5, 0, 15, 5);
Chart chart = charts.get(chartIndex);

// Add NSeries (chart data source) to the chart ranging from "A1"
// cell to "B3"
SeriesCollection serieses = chart.getNSeries();
serieses.add("A1:B3", true);

// Save the Excel file
workbook.save("Pyramid-Chart.xlsx", SaveFormat.XLSX);
Copy the code

If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.