* 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>
11 KiB
Charts in Templates
!> Charts in templates require an understanding of the Patcher and Chart Data.
patchDocument fills in a template's charts two ways:
- New data for the template's charts. A chart made and styled in Word gets new data, and keeps its look. This page shows how.
- New charts. A
ChartRungoes where a placeholder is in the template's text, as an image does. See Chart Size and Position.
New Data for a Template's Charts
- In Word, insert the chart and style it as the document needs: its type, title, colours, fonts, labels, axes and legend. Its data can be anything.
- Right-click the chart, choose Edit Alt Text, and type a placeholder as its description, such as
{{sales}}. - Patch the template with a
ChartDataPatchfor the placeholder's key:
import * as fs from "fs";
import { Document, Packer, Paragraph, patchDocument } from "docx";
import { ChartDataPatch, ChartRun } from "docx/charts";
// The template, made here as it would be in Word: a styled chart, with {{sales}} as its alt text's description
const template = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "column",
title: "Sales by quarter",
categories: ["Q1", "Q2"],
series: [{ name: "Last year", values: [100, 120], color: "1F4E79" }],
dataLabels: { value: true },
altText: { name: "Sales", description: "{{sales}}" },
}),
],
}),
],
},
],
});
const doc = await patchDocument({
outputType: "nodebuffer",
data: await Packer.toBuffer(template),
patches: {
sales: new ChartDataPatch({
categories: ["Q1", "Q2", "Q3", "Q4"],
series: [
{ name: "2024", values: [120, 135, 150, 170] },
{ name: "2025", values: [140, 160, 155, 190] },
],
}),
},
});
fs.writeFileSync("My Document.docx", doc);
Text patches, new charts and ChartDataPatches go in the same patches. A ChartDataPatch only looks in charts' alt text, and a text patch only in text, so {{sales}} typed in the document's text stays as it is.
Every chart whose alt text holds the placeholder gets the data, wherever it is: in the document, a table, a header, a footer or a footnote. patchDetector lists the placeholders in charts' alt text along with those in text.
The Data
The data is given as it is to ChartRun, without the chart's type, which is the template's:
import * as fs from "fs";
import { Document, Packer, Paragraph, patchDocument } from "docx";
import { ChartDataPatch, ChartRun } from "docx/charts";
// The template, made here as it would be in Word: a line, a bubble and a stock chart, each with a placeholder as its alt text
const template = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "line",
categories: ["A", "B"],
series: [{ name: "Series 1", values: [1, 2] }],
altText: { name: "Visitors", description: "{{visitors}}" },
}),
],
}),
new Paragraph({
children: [
new ChartRun({
type: "bubble",
series: [{ name: "Series 1", points: [{ x: 1, y: 1, size: 1 }] }],
altText: { name: "Stores", description: "{{stores}}" },
}),
],
}),
new Paragraph({
children: [
new ChartRun({
type: "stock",
categories: ["A", "B"],
high: [2, 3],
low: [1, 2],
close: [1.5, 2.5],
altText: { name: "Prices", description: "{{prices}}" },
}),
],
}),
],
},
],
});
const doc = await patchDocument({
outputType: "nodebuffer",
data: await Packer.toBuffer(template),
patches: {
// A column, bar, line, area, pie, doughnut or radar chart
visitors: new ChartDataPatch({
categories: ["Jan", "Feb", "Mar"],
series: [{ name: "Visitors", values: [320, 410, null] }],
}),
// A scatter chart, or with each point's size, a bubble chart
stores: new ChartDataPatch({
series: [
{
name: "Stores",
points: [
{ x: 12, y: 340, size: 4 },
{ x: 30, y: 510, size: 9 },
],
},
],
}),
// A stock chart: its prices, and its opening prices and volumes if it has them
prices: new ChartDataPatch({
categories: [new Date("2025-01-06"), new Date("2025-01-07")],
high: [103.1, 104.8],
low: [100.4, 101.9],
close: [102.4, 104.2],
}),
},
});
fs.writeFileSync("My Document.docx", doc);
| Option | Type | Notes |
|---|---|---|
| categories | (string | number | Date)[] | ChartCategoryGroup[] |
For a chart with categories. Dates, and groups, are for column, bar, line and area charts |
| series | { name, values }[] with categories, or { name, points }[] |
Each series' name, and a value for each category (null for a gap), or its points. A bubble chart's points each need a size, and a scatter chart's leave it out |
| high, low, close, open, volume, names | (number | null)[], StockChartSeriesNames |
For a stock chart, in place of series, as for a stock chart. It takes open and volume if the template's chart has them, and only then |
| description | string |
The chart's alt text description. Default is a description of the new data, such as "Column chart, Sales. 2024: Q1 120, Q2 135." An empty one removes it |
The data is checked when the patch is made, as a ChartRun's is, and then against the template's chart: a pie chart has one series, pie and doughnut charts have no negative values, a scatter or bubble chart's series have points, and a stock chart has opening prices and volumes if the template's has them.
What Changes, and What Stays
The chart keeps its look. Its type, title, colours, fonts, labels, axes, legend and size are the template's. Its data changes like this:
- The series. Each series' name and data are replaced in the order the series are plotted, which is their order in Word's Select Data.
- More series. New series copy the look of the template's last series, such as its line, markers and labels, in their own colour: the next of the theme's accent colours, as
ChartRuncolours them. In a combo chart, they are drawn the way the last series is. - Fewer series. The template's other series are removed, with any axes only they used, such as a secondary axis.
- More points. When each point has its own colour, as the slices of a pie do, new points take the next of the theme's accent colours. Otherwise they take their series' colour.
- Fewer points. The colours and labels of points past the new data are removed, and so are a pie of pie's split slices past it.
- Slices pulled out. New slices are pulled out as far as the template's, if every slice of the template is pulled out as far. A slice pulled out on its own is that slice's.
- Trendlines, error bars and data tables stay, and are drawn for the new data. A trendline that can't be fitted to the new values, such as a moving average over more points than there are, isn't drawn, as when a chart's data is edited in Word. New series don't copy the template's last series' trendlines or error bars.
- Number formats. Values keep the chart's number format, such as
0%. So do categories, when they and the template's are both dates or both numbers. - Dates. Dates on a date axis are spaced by days, months or years, as the new dates need. Categories that aren't dates make it a text axis.
- The workbook. The chart gets a new embedded workbook holding the new data, so Word's Edit Data opens it. The template's workbook is removed, so its data doesn't stay in the document.
- The alt text. The description is replaced with a description of the new data, or the patch's
description. A decorative chart gets none. The placeholder is taken out of the alt text's title.
What Can't Be Patched
patchDocument throws an error that names the chart's placeholder and says what to do, for a chart it can't patch:
- Drawings that aren't charts, such as pictures and shapes. In a group of drawings, put the placeholder in the alt text of the chart itself, unless it is the group's only chart.
- Office 2016's chart types: waterfall, histogram, box and whisker, treemap, sunburst, funnel and map charts.
- Surface and pivot charts.
- A stock chart combined with a chart other than its volumes' columns.
- Charts with something else in their workbook's cells: data labels from cells (Word's "Value From Cells"), series hidden with Word's chart filters, error bars with custom values from cells, and titles or labels linked to cells. The new data replaces the workbook, so these would lose their cells.
- Data that isn't the chart's kind, such as points for a column chart or two series for a pie chart.
- Two charts' placeholders in one chart's alt text.
The template's charts without a placeholder are left as they are, whatever they are.
Other Drawings
ChartDataPatch is a DrawingPatch from docx: a patch for a drawing whose alt text holds the placeholder, which changes the drawing and the parts of the document it refers to. patchDocument applies drawing patches before text patches, so they only see the template's own drawings.
Example
Source: https://github.com/dolanmiu/docx/blob/master/demo/charts/chart-data-in-templates.ts