PivotTable.createChart Method
Creates a linked worksheet chart from visible pivot detail groups, excluding subtotals and grand totals.
Package: @txtextcontrol/tx-sheet-control.
Syntax
createChart(options: Omit<ChartObjectOptions, 'dataRange' | 'series' | 'seriesOrientation'> = {}): Chart
Parameters
options: Optional chart type, title, legend, colors, style, size and anchor. Chart data and orientation belong to the PivotTable; dataRange, series and seriesOrientation cannot be supplied. Scatter, bubble and stock chart types are unsupported.
Return Value
A worksheet Chart that remains connected when the pivot's fields, measures, filters, sorting, expanded groups or destination change. Source edits appear when the pivot refreshes. A temporarily empty report leaves a blank connected chart.
Exceptions
- Unsupported chart families or invalid chart settings reject without creating a chart.
- Custom data or orientation settings reject; change the pivot's
rows,columnsorvaluesinstead. - Stale pivot handles reject after removal or document replacement.
Example
In a browser, set workbook = editor.workbook; in Node.js, use the workbook of your server control. The example creates its own source and report.
import { Workbook as ExampleWorkbook } from '@txtextcontrol/tx-sheet-control';
const workbook = new ExampleWorkbook();
const source = workbook.sheets.at(0);
source.range('A1:D5').setValues([
['Region', 'Category', 'Revenue', 'Cost'],
['Europe', 'Software', 1250, 500],
['Europe', 'Services', 700, 350],
['Americas', 'Software', 1800, 900],
['Americas', 'Services', 950, 400]
]);
const report = workbook.sheets.add('Sales Summary');
const pivot = report.pivotTables.add({
name: 'RegionalSales',
source: { sheetId: source.id, range: 'A1:D5' },
destination: { sheetId: report.id, cell: 'B3' },
rows: [{ field: 'Region' }],
columns: [{ field: 'Category' }],
values: [{ field: 'Revenue', caption: 'Sales', numberFormatCode: '$#,##0.00' }],
layout: 'tabular', style: { rowBanding: true }
});
const chart = pivot.createChart({ type: 'column-clustered', title: 'Regional Revenue' });
pivot.setFilters([{ field: 'Region', type: 'include', items: ['Europe'] }]);
chart.update({ type: 'line', legend: 'right' });
Remarks
Appearance updates through Chart.update preserve the binding. Direct chart dataRange, series or orientation changes are rejected; change the PivotTable axes and measures to change chart data. Creation and pivot-driven chart updates share workbook undo history. XLSX includes the native chart-to-pivot association. Removing the pivot retains the chart as a static snapshot.
See Pivot Tables for refresh, grouping, persistence and validation limits.