Files
docx/docs/usage/chart-data.md
T
DolanandClaude Opus 5.5 c169dd1a83 feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded (#3556)
* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded

The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now
an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with
docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types,
the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and
the pages side by side, or one of them. The demos a page includes are live too.

Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its
imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain.

Running them showed docs that were wrong, fixed here:
- Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page
  marked as landscape
- Comments: `comments.children` takes plain objects, not `new Comment(...)`
- Custom properties are text only
- APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...),
  createParagraph().heading1(), a border's `value`, TextRun's `.break()`
- The templates and patcher examples read files that aren't in the repository. They now patch the templates in
  demo/assets, or make their own

`npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they
describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document

"ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on
the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example
now awaits patchDocument and writes the document it makes, as the example after it does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 13:00:29 +01:00

192 lines
8.3 KiB
Markdown

# Chart Data
A chart's data is its categories and its series, or for a scatter or bubble chart, its points. This page shows how to give it, and how it is saved in the document.
## Categories and Series
Column, bar, line, area, pie, doughnut and radar charts take their data as a table: a row for each category, and a column for each series.
```ts live
import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "column",
categories: ["Q1", "Q2", "Q3", "Q4"],
series: [
{ name: "2024", values: [120, 135, 150, 170] },
{ name: "2025", values: [140, 150, 165, null] },
],
}),
],
}),
],
},
],
});
```
is this table:
| | 2024 | 2025 |
| ------ | ---- | ---- |
| **Q1** | 120 | 140 |
| **Q2** | 135 | 150 |
| **Q3** | 150 | 165 |
| **Q4** | 170 | |
- `categories` are the labels along the category axis, the slices of a pie, or the spokes of a radar chart. They are text, or numbers such as years. On a column, bar, line or area chart they can be dates too: see [Dates](#dates).
- Each series has a `name`, which the legend shows, and `values`: one value for each category, in the same order.
- `null` is an empty cell, which leaves a gap: no bar, a break in a line, or no slice.
- A series can have fewer values than there are categories. The categories after its last value are gaps.
Each series can also have a `color`. See [Chart Colours](usage/chart-colors.md).
## Dates
When a column, bar, line or area chart's categories are all `Date`s, the category axis spaces them by date, as Excel does, and labels them as dates:
```ts live
import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "line",
categories: [new Date("2025-01-01"), new Date("2025-02-01"), new Date("2025-05-01")],
series: [{ name: "Sign-ups", values: [320, 410, 520] }],
}),
],
}),
],
},
],
});
```
The categories have to be all dates or none. Dates are read in UTC. See [Dates](usage/chart-axes.md#dates) on the axes page.
## Categories in Groups
A column, bar, line or area chart's categories can be in groups, such as quarters in years. The axis labels the categories, with each group's name below its categories:
```ts live
import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "column",
title: "Sales by quarter",
categories: [
{ name: "2024", categories: ["Q1", "Q2", "Q3", "Q4"] },
{ name: "2025", categories: ["Q1", "Q2"] },
],
series: [{ name: "Sales", values: [120, 135, 150, 170, 160, 180] }],
}),
],
}),
],
},
],
});
```
- Each group has a `name` and its `categories`, text or numbers. The series have a value for each category in every group, in order: here, six.
- A group can hold groups instead, such as months in quarters in years, for another row of labels. Every group at the same depth holds groups as deep, so each category has as many groups.
- A group has at least one category. Categories in groups can't be dates.
Pages shows only the groups' names. See [Chart Compatibility](usage/chart-compatibility.md).
## Points
A [scatter chart](usage/chart-scatter.md) has no categories. Each of its series has `points`, and each point has its own `x` and `y`:
```ts live
import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "scatter",
series: [
{
name: "Group A",
points: [
{ x: 150, y: 50 },
{ x: 165, y: 61 },
],
},
],
}),
],
}),
],
},
],
});
```
A [bubble chart](usage/chart-bubble.md)'s points have a `size` too: `{ x: 150, y: 50, size: 12 }`.
## Editing the Data in Word
Each chart's data is saved twice: in the chart, which Word draws it from, and in an Excel workbook inside the document. **Edit Data** in Word opens the workbook in Excel, and Word redraws the chart when the data changes.
The workbook has one sheet, laid out as Word lays out a new chart's data:
- For a chart with categories, the sheet is the table above: the series' names in row 1 from column B, the categories in column A from row 2, and each series' values below its name.
- Categories in groups take a column each for the groups and the categories, from column A, the outermost groups first. A group's name is in the row of its first category. The series start in the column after them.
- A stock chart's series are in Word's order: the volumes, the opening, high and low prices, and the closing prices, those given.
- For a scatter chart, each series has two columns: `X` above its x values, and its name above its y values. A bubble chart's series have a third, `Size`, above their sizes.
- Custom error bars' lengths are in columns after the series: each series' `(+)` and `(-)` lengths, or for a scatter or bubble chart, its `x` and then its `y` lengths. See [Custom Error Bars](usage/chart-trendlines-and-error-bars.md#custom-error-bars).
- Dates are numbers in the sheet, in a date format, as Excel keeps them.
## Mistakes
`new ChartRun(...)` throws an error when an option is wrong, so the mistake shows up where the chart is made. It throws when:
- a chart has no series, or no categories;
- a category isn't text or a finite number, or only some of the categories are dates;
- only some of the categories are groups, a group has no categories, or groups at the same depth aren't as deep as each other;
- a value isn't a finite number or `null`;
- a series has more values than there are categories;
- a pie chart has more than one series;
- a pie or doughnut chart has a negative value;
- a series has more `colors` than there are categories;
- a scatter or bubble series has no points, a point's `x` or `y` isn't a finite number, or a bubble's `size` is negative;
- a series has an option the way it is drawn doesn't have, such as `markers` on columns, or a `type` on a bar chart;
- every series is on the secondary axis;
- a data label `position` isn't one the chart has, or a label with `text` shows something else too;
- a series has more `pointLabels`, `explosion`s or custom error bar lengths than it has points;
- a trendline has an option its type doesn't have, or can't be fitted to the series' values, such as an exponential trendline to a value of 0;
- a stacked series has a trendline;
- a stock chart's high is below its low, its open or close isn't between them, or it has no high, low or close prices;
- a legend's `hiddenEntries` has text that isn't one of the legend's entries;
- a pie of pie's `split` picks categories the chart doesn't have;
- an option is outside its range, such as `gapWidth: 600`, or a value axis' `minimum` isn't less than its `maximum`;
- the chart's width or height isn't a positive number.
The error says what is wrong, such as `Series "2025" has 5 values, but there are 4 categories`.