Working with Charts

Working with Charts

Aspose.Cells FOSS for Java preserves embedded charts when loading and saving XLSX workbooks. You can inspect existing charts, query their type and position, and add new standard-type charts programmatically. ChartEx types such as Waterfall, Treemap, Sunburst, Histogram, Box and Whisker, Funnel, and Map are preserved on round-trip but cannot be created through the API.


Overview

Charts in a worksheet are managed through the ChartCollection accessed via Worksheet.getCharts(). Each Chart instance exposes its name, chart type, and its two-cell anchor position. A new chart is inserted with ChartCollection.add(type, dataRange, upperLeftRow, upperLeftColumn, lowerRightRow, lowerRightColumn), which returns the index of the newly created chart.

The ChartType enum identifies the chart variant (bar, line, pie, etc.). Helper methods ChartCollection.parseChartType(string) and ChartCollection.chartTypeToString(ChartType) convert between the enum and its string representation for serialization use cases.


Key Classes

ClassDescription
ChartRepresents an embedded chart in a worksheet. Read access to name, type, and cell-anchor position.
ChartCollectionCollection of all charts on a worksheet. Supports count, index access, add, and chart-type conversion helpers.
ChartTypeEnum identifying the chart variant (bar, column, line, pie, scatter, etc.).
ChartModelInternal serialization model for a chart. Exposes raw XML fields used during load/save.

Accessing Charts on a Worksheet

Retrieve the ChartCollection from any Worksheet object via Worksheet.getCharts(). Use ChartCollection.getCount() to determine how many charts are present. Individual charts are accessed by zero-based index through ChartCollection.get(index).

Once you hold a Chart reference you can read:

  • Chart.getName() — the chart’s internal name string.
  • Chart.getChartType() — the ChartType enum value.
  • Chart.getUpperLeftRow() / Chart.getUpperLeftColumn() — the top-left anchor cell.
  • Chart.getLowerRightRow() / Chart.getLowerRightColumn() — the bottom-right anchor cell.
  • Chart.getExtentCx() / Chart.getExtentCy() — the chart extents in EMUs.

Adding a New Chart

Use ChartCollection.add(type, dataRange, upperLeftRow, upperLeftColumn, lowerRightRow, lowerRightColumn) to insert a new chart. The method returns the integer index of the chart within the collection. Provide a ChartType value for the type argument and a cell range string (for example A1:B5) for dataRange.

Note: ChartEx-family types (Waterfall, Treemap, Sunburst, Histogram, Box and Whisker, Funnel, Map) are preserved when loading XLSX files but cannot be created programmatically via this API.


API Reference Summary

Chart

MethodReturn typeDescription
getName()StringInternal name of the chart.
getChartType()ChartTypeThe chart variant enum value.
getUpperLeftRow()intRow index of the top-left anchor.
getUpperLeftColumn()intColumn index of the top-left anchor.
getLowerRightRow()intRow index of the bottom-right anchor.
getLowerRightColumn()intColumn index of the bottom-right anchor.
getExtentCx()longChart width in EMUs.
getExtentCy()longChart height in EMUs.

ChartCollection

MethodReturn typeDescription
getCount()intNumber of charts on the worksheet.
get(index)ChartReturns the chart at the given zero-based index.
add(type, dataRange, ...)intAdds a new chart; returns its index.
parseChartType(string)ChartTypeConverts a string name to a ChartType enum value.
chartTypeToString(type)StringConverts a ChartType enum value to its string name.

Limitations

  • ChartEx types (Waterfall, Treemap, Sunburst, Histogram, Box and Whisker, Funnel, Map) are round-tripped verbatim but cannot be created via the API.
  • Chart data series bindings are not modifiable in this release; the chart renders based on the original data range supplied at creation time.

See Also