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
| Class | Description |
|---|---|
Chart | Represents an embedded chart in a worksheet. Read access to name, type, and cell-anchor position. |
ChartCollection | Collection of all charts on a worksheet. Supports count, index access, add, and chart-type conversion helpers. |
ChartType | Enum identifying the chart variant (bar, column, line, pie, scatter, etc.). |
ChartModel | Internal 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()— theChartTypeenum 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
| Method | Return type | Description |
|---|---|---|
getName() | String | Internal name of the chart. |
getChartType() | ChartType | The chart variant enum value. |
getUpperLeftRow() | int | Row index of the top-left anchor. |
getUpperLeftColumn() | int | Column index of the top-left anchor. |
getLowerRightRow() | int | Row index of the bottom-right anchor. |
getLowerRightColumn() | int | Column index of the bottom-right anchor. |
getExtentCx() | long | Chart width in EMUs. |
getExtentCy() | long | Chart height in EMUs. |
ChartCollection
| Method | Return type | Description |
|---|---|---|
getCount() | int | Number of charts on the worksheet. |
get(index) | Chart | Returns the chart at the given zero-based index. |
add(type, dataRange, ...) | int | Adds a new chart; returns its index. |
parseChartType(string) | ChartType | Converts a string name to a ChartType enum value. |
chartTypeToString(type) | String | Converts 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.