DsExcel Java enables users to add charts in spreadsheets for improved data analysis and enhanced data visualization.
Users can create and delete chart using the methods of the IShapes interface and the IChart interface
You can create chart in a worksheet by using the addChart method of the IShapes interface. Using this method, you can add a chart at a particular position by providing postion coordinates of the target range. The method has another overload that lets you add a chart directly to the target range. You can use add method of the ISeriesCollection class which lets you reflect data over the chart.
To create a chart, refer to the following example code.
Java |
Copy Code |
---|---|
// Add Chart IShape shape = worksheet.getShapes().addChart(ChartType.ColumnClustered, 300, 10, 300, 300); worksheet.getRange("A1:D6").setValue( new Object[][] { { null, "S1", "S2", "S3" }, { "Item1", 10, 25, 25 }, { "Item2", -51, -36, 27 }, { "Item3", 52, -85, -30 }, { "Item4", 22, 65, 65 }, { "Item5", 23, 69, 69 } }); //Add Chart at a specified position IShape shape = worksheet.getShapes.addChart(ChartType.ColumnClustered, 300, 100, 300, 300); // Create Chart shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true); //Add Chart at a particular range // IShape shapeRange = worksheet.getShapes.addChart(ChartType.ColumnClustered, worksheet.getRange["A8:E20"]); //shapeRange.getChart().getSeriesCollection().add(worksheet.Range["A1:D6"], RowCol.Columns, true, true); |
You can delete an existing chart by using the delete method of the IChart interface.
To delete a chart from your worksheet, refer to the following example code.
Java |
Copy Code |
---|---|
// Delete Chart
shape.getChart().delete(); |