* chore: organise the demos into a folder for each topic The demos were one flat list of 124 numbered files, which made it hard to find the one you wanted. Each is now in a folder for its topic, such as demo/tables/basic-table.ts, with a name that says what it shows instead of a number. npm run demo lists every demo and narrows the list as you type. It also takes a demo's path or name, a topic, or search words. The demos' images moved to demo/assets/images, as images is now a topic. The docs' links, the Demos workflow, the shape render script and its reference images use the new paths and names. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ * fix: list only a topic's own demos, and drop stale output file names npm run demo text also offered the text-boxes demos and others with "text" in their name, so a topic now searches for "text/". Three demos logged their old numbered file name after writing My Document.docx. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- 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