* 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>
13 KiB
Charts
!> Charts require an understanding of Paragraphs.
Charts are native Word charts. Word draws each one from its data, in the document's theme, just as it draws a chart made with Insert > Chart. Edit Data in Word opens the chart's data in Excel.
Like an image, a chart is a run inside a Paragraph.
Your First Chart
import * as fs from "fs";
import { Document, Packer, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "column",
title: "Sales",
categories: ["Jan", "Feb", "Mar"],
series: [
{ name: "2024", values: [10, 20, 30] },
{ name: "2025", values: [15, 25, null] },
],
}),
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});
This draws a column chart with a column for each year in each month, and a legend below it:
typeis the kind of chart. See Chart Types.categoriesare the labels along the bottom: one group of columns for each.seriesare the sets of values. Each has aname, which the legend shows, and one value for each category, in order.nullleaves a gap, so there is no 2025 column for March.
Every chart needs a type and its series, and every type but a scatter or bubble chart needs categories. A stock chart's data is its prices instead of series. All the other options are optional.
Chart Types
type is the name Word gives the chart in Insert > Chart. Each page shows a chart type's own options, such as stacking for column charts and markers for line charts.
| Type | Draws | Use it to | Page |
|---|---|---|---|
"column" |
Vertical bars | Compare values across categories | Column and Bar Charts |
"bar" |
Horizontal bars | Compare values with long category names | Column and Bar Charts |
"line" |
A line for each series | Show a trend over time | Line and Area Charts |
"area" |
A filled area for each series | Show a trend, and how the parts add up | Line and Area Charts |
"pie" |
Slices of a circle | Show the parts of one whole | Pie and Doughnut Charts |
"doughnut" |
A ring of slices for each series | Compare the parts of several wholes | Pie and Doughnut Charts |
"pieOfPie" |
A pie, and its smallest slices as a second pie | Read small parts of one whole | Pie and Doughnut Charts |
"barOfPie" |
A pie, and its smallest slices as a bar | Read small parts of one whole | Pie and Doughnut Charts |
"radar" |
A line round a spoke for each category | Compare several qualities at once | Radar Charts |
"scatter" |
Points, each with its own x and y | Show how two numbers relate | Scatter Charts |
"bubble" |
Bubbles, each with its own x, y and size | Show how three numbers relate | Bubble Charts |
"stock" |
Each day's high, low and close, and its open and volume | Show how a price moved | Stock Charts |
Columns, lines and areas can be drawn together in one chart, with a second value axis on the right. See Combo Charts.
Common Use Cases
| I want to... | Use | Page |
|---|---|---|
| Give a chart its data | categories and series |
Chart Data |
| Show dates along the bottom | Dates as the categories |
Chart Axes |
| Group quarters within years | Groups as the categories | Chart Data |
| Draw a line over columns | A series' type |
Combo Charts |
| Show numbers on another scale on the right | A series' axis: "secondary" |
Combo Charts |
| Change a series' colour | color, such as "1F4E79" or a theme colour |
Chart Colours |
| Pick out one bar in another colour | A series' colors |
Column and Bar Charts |
| Draw a dashed line, or change the markers | A series' line and markers |
Line and Area Charts |
| Add a title, or move or hide the legend | title and legend |
Chart Titles and Legends |
| Change the font or the background | font, chartArea and plotArea |
Chart Fonts and Fills |
| Name an axis, or set its range or its format | categoryAxis and valueAxis |
Chart Axes |
| Use a logarithmic scale, or thousands | logarithmicBase and displayUnits |
Chart Axes |
| Show the numbers on the chart | dataLabels |
Chart Data Labels |
| Label one point, such as the highest | A series' pointLabels |
Chart Data Labels |
| Show a trend, or a forecast | A series' trendlines |
Chart Trendlines and Error Bars |
| Show how far values might be off | A series' errorBars |
Chart Trendlines and Error Bars |
| Join a line across a missing value | emptyValues: "connect" |
Line and Area Charts |
| Leave a series out of the legend | legend: { hiddenEntries } |
Chart Titles and Legends |
| Show a table of the data under the chart | dataTable |
Chart Titles and Legends |
| Pull a slice out of a pie | A series' explosion |
Pie and Doughnut Charts |
| Make a chart bigger or smaller | transformation |
Chart Size and Position |
| Float a chart beside its text | floating |
Chart Size and Position |
| Put a chart in a table, header or footer | A Paragraph with the chart in it |
Chart Size and Position |
| Put a chart in a template | patchDocument, with the chart in a patch |
Chart Size and Position |
| Give a template's chart, made in Word, data | patchDocument, with a ChartDataPatch |
Charts in Templates |
| Describe a chart for screen readers | altText, or nothing: it is described for you |
Chart Size and Position |
| Turn a Mermaid pie or XY chart into a chart | A ChartRun with its data |
Charts from Mermaid |
| Know how a chart looks in other applications | Chart Compatibility |
Importing
Charts come with the docx package. Import them from docx/charts, and everything else, such as the document and its paragraphs, from docx:
import { Document, Packer, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
Types such as ChartRunOptions and ChartSeries come from docx/charts too.
In a page without a bundler, load the charts after docx: dist/charts.umd.cjs after dist/index.umd.cjs, or dist/charts.iife.js after dist/index.iife.js. They add a docxCharts global next to the docx one:
<script src="node_modules/docx/dist/index.umd.cjs"></script>
<script src="node_modules/docx/dist/charts.umd.cjs"></script>
<script>
const chart = new docxCharts.ChartRun({ type: "pie", categories: ["A", "B"], series: [{ name: "Share", values: [60, 40] }] });
</script>
Mistakes
new ChartRun(...) throws an error when an option is wrong, such as a series with more values than there are categories. The error says what is wrong, and it is thrown where the chart is made, not when the document is opened. Chart Data lists the mistakes.
Examples
Charts
A chart of each of the first types, with a small chart in the header, two charts in a table and one floating beside its text.
Source: https://github.com/dolanmiu/docx/blob/master/demo/charts/charts.ts
Chart Options
A combo chart with a secondary axis, dates, axes in reverse and on a logarithmic scale, labels and colours, fonts and fills, and radar and bubble charts.
Source: https://github.com/dolanmiu/docx/blob/master/demo/charts/chart-options.ts
More Charts
Stock charts, pie of pie and bar of pie charts, categories in groups, trendlines and error bars, the labels of single points, a pulled-out slice, a hidden legend entry, a data table, and a line joined across an empty value.
Source: https://github.com/dolanmiu/docx/blob/master/demo/charts/more-charts.ts
Charts in Templates
A column, a pie and a line chart put in a template saved by Word, where its placeholders were.
Source: https://github.com/dolanmiu/docx/blob/master/demo/charts/charts-in-templates.ts
Chart Data in Templates
A template's charts, a column, a doughnut and a line chart, given new data, with more series and points than they had, keeping their look.
Source: https://github.com/dolanmiu/docx/blob/master/demo/charts/chart-data-in-templates.ts