* 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>
7.1 KiB
Combo Charts
A combo chart draws some series one way and some another, such as sales as columns and a target as a line. A series can also be drawn against a second value axis, on the right, when its numbers are on another scale, such as a percentage beside a number of units. Word calls these charts "Combo" charts.
Charts shows how to import ChartRun and add a chart to a document.
Columns and a Line
Give a series a type to draw it as "column", "line" or "area", whatever the chart's own type:
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 and target",
categories: ["Jan", "Feb", "Mar", "Apr"],
series: [
{ name: "Sales", values: [120, 135, 150, 170] },
{ name: "Target", values: [130, 140, 150, 160], type: "line" },
],
}),
],
}),
],
},
],
});
- Series without a
typeare drawn as the chart'stype. - Areas are drawn behind columns, and lines in front of both, whatever the order of the series. Series on the secondary axis are drawn in front of those on the primary axis.
- Column, line and area charts can have series of each type. A bar chart's series are all bars, as its bars run the other way.
The options of each way of drawing a series work as they do on its own chart. A line series takes markers, smooth and line, as on a line chart, and a column series takes colors, as on a column chart. The chart's gapWidth and overlap are for its columns when its type is "column", and its markers and smooth are for its lines when its type is "line".
A Secondary Axis
axis: "secondary" draws a series against a second value axis, on the right side of the chart:
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 and growth",
categories: ["Jan", "Feb", "Mar", "Apr"],
series: [
{ name: "Sales", values: [120, 135, 150, 170] },
{ name: "Growth", values: [4, 12.5, 11.1, 13.3], type: "line", axis: "secondary" },
],
valueAxis: { title: "Units" },
secondaryValueAxis: { title: "Growth (%)" },
}),
],
}),
],
},
],
});
secondaryValueAxistakes the same options asvalueAxis, such as a title, a range and a number format. See Chart Axes.- The secondary axis has no gridlines by default, so they don't cross the primary axis' gridlines.
- At least one series has to be on the primary axis.
- A bar chart's secondary axis runs along the top.
- The secondary axis stays on the other side from the primary one when the categories are in reverse order.
A series drawn as columns on the secondary axis is drawn in front of those on the primary axis, in the same place, so the columns cover each other. Draw series on the secondary axis as lines, as in the example above.
Labels
A chart's dataLabels are on every series, but their position is for the series drawn as the chart's type, as each way of drawing a series has positions of its own. The others keep Office's positions, unless their own dataLabels give one:
import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "column",
categories: ["Jan", "Feb", "Mar", "Apr"],
dataLabels: { value: true, position: "insideEnd" },
series: [
{ name: "Sales", values: [120, 135, 150, 170] },
{
name: "Target",
values: [130, 140, 150, 160],
type: "line",
dataLabels: { value: true, position: "above" },
},
],
}),
],
}),
],
},
],
});
See Where the Labels Go.
Stacking
stacking stacks the series drawn as the chart's type. The others aren't stacked, so a line can show the total of stacked columns:
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"],
series: [
{ name: "North", values: [40, 45, 50] },
{ name: "South", values: [30, 35, 30] },
{ name: "Total", values: [70, 80, 80], type: "line" },
],
stacking: "stacked",
}),
],
}),
],
},
],
});
Options
| Property | Type | Notes | Description |
|---|---|---|---|
series[].type |
"column" | "line" | "area" |
Optional | How the series is drawn. Default is the chart's type. Not on bar charts |
series[].axis |
"primary" | "secondary" |
Optional | The value axis the series is drawn against. Default "primary" |
secondaryValueAxis |
ChartValueAxis |
Optional | The secondary value axis, on the right. See Chart Axes |
stacking |
"none" | "stacked" | "percent" |
Optional | Stacks the series drawn as the chart's type. Default "none" |