* 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>
8.3 KiB
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.
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 |
categoriesare 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.- Each series has a
name, which the legend shows, andvalues: one value for each category, in the same order. nullis 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.
Dates
When a column, bar, line or area chart's categories are all Dates, the category axis spaces them by date, as Excel does, and labels them as dates:
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 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:
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
nameand itscategories, 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.
Points
A scatter chart has no categories. Each of its series has points, and each point has its own x and y:
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'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:
Xabove 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, itsxand then itsylengths. See 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
colorsthan there are categories; - a scatter or bubble series has no points, a point's
xoryisn't a finite number, or a bubble'ssizeis negative; - a series has an option the way it is drawn doesn't have, such as
markerson columns, or atypeon a bar chart; - every series is on the secondary axis;
- a data label
positionisn't one the chart has, or a label withtextshows something else too; - a series has more
pointLabels,explosions 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
hiddenEntrieshas text that isn't one of the legend's entries; - a pie of pie's
splitpicks categories the chart doesn't have; - an option is outside its range, such as
gapWidth: 600, or a value axis'minimumisn't less than itsmaximum; - 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.