* 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>
9.8 KiB
Charts from Mermaid
Mermaid draws charts from text, such as a pie chart or an XY chart of bars and lines. This page shows the ChartRun for each, so a chart written in Mermaid can be a native Word chart, which Word draws and Edit Data opens.
docx doesn't read Mermaid's text. Give its data to a ChartRun, as below. Charts shows how to import ChartRun and add a chart to a document.
Pie Charts
This Mermaid pie chart:
pie title Pets adopted by volunteers
"Dogs" : 386
"Cats" : 85
"Rats" : 15
is this chart:
import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "pie",
title: "Pets adopted by volunteers",
categories: ["Dogs", "Cats", "Rats"],
series: [{ name: "Pets", values: [386, 85, 15] }],
dataLabels: { percentage: true },
legend: { position: "right" },
}),
],
}),
],
},
],
});
| Mermaid | ChartRun |
|---|---|
pie |
type: "pie" |
title Pets adopted by volunteers |
title: "Pets adopted by volunteers" |
"Dogs" : 386, a line for each slice |
"Dogs" in categories, and 386 in the series' values, in the same order |
| The slices' percentages | dataLabels: { percentage: true } |
showData |
dataLabels: { percentage: true, value: true }, which puts the values on the slices instead |
| The legend on the right | legend: { position: "right" }. Word puts it below by default |
donutHole: 0.4 |
type: "doughnut", with holeSize: 40, from 10 to 90 |
legendPosition: "top" |
legend: { position: "top" } |
highlightSlice: "Dogs" |
No equivalent: Mermaid draws the slice a little larger, and the others fainter. The series' explosion can single it out another way, by pulling it out: explosion: [20] |
A Word chart's series has a name, which Mermaid's pie has none of, so give it one. See Pie and Doughnut Charts.
From Mermaid's Text
When the Mermaid text is what you have, a few lines of your own code can turn a pie chart into a ChartRun:
import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
// A Mermaid pie chart as a ChartRun: its title, and a slice for each "label" : value line
const pieFromMermaid = (mermaid: string): ChartRun => {
const lines = mermaid.split("\n").map((line) => line.trim());
const title = lines.map((line) => /^(?:pie\s+(?:showData\s+)?)?title\s+(.+)$/.exec(line)?.[1]).find((found) => found !== undefined);
const showData = lines.some((line) => /^pie\s+showData\b/.test(line));
const slices = lines.flatMap((line) => {
const match = /^"(.*)"\s*:\s*([\d.]+)$/.exec(line);
return match ? [{ label: match[1], value: Number(match[2]) }] : [];
});
return new ChartRun({
type: "pie",
title,
categories: slices.map(({ label }) => label),
series: [{ name: title ?? "Share", values: slices.map(({ value }) => value) }],
dataLabels: { percentage: true, value: showData },
legend: { position: "right" },
});
};
const mermaid = `pie title Pets adopted by volunteers
"Dogs" : 386
"Cats" : 85
"Rats" : 15`;
const doc = new Document({
sections: [
{
children: [new Paragraph({ children: [pieFromMermaid(mermaid)] })],
},
],
});
It reads Mermaid's pie syntax only. It leaves out the Mermaid config above the chart, such as its colours and donutHole.
XY Charts
Mermaid's XY chart (xychart, or xychart-beta in older versions of Mermaid) draws bars and lines against categories. This one:
xychart
title "Sales Revenue"
x-axis [jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]
y-axis "Revenue (in $)" 4000 --> 11000
bar [5000, 6000, 7500, 8200, 9500, 10500, 11000, 10200, 9200, 8500, 7000, 6000]
line [5000, 6000, 7500, 8200, 9500, 10500, 11000, 10200, 9200, 8500, 7000, 6000]
is this column chart, with its line drawn as a line, as a combo chart:
import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
const revenue = [5000, 6000, 7500, 8200, 9500, 10500, 11000, 10200, 9200, 8500, 7000, 6000];
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "column",
title: "Sales Revenue",
categories: ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
series: [
{ name: "Revenue", values: revenue },
{ name: "Trend", values: revenue, type: "line" },
],
valueAxis: { title: "Revenue (in $)", minimum: 4000, maximum: 11000 },
legend: false,
}),
],
}),
],
},
],
});
| Mermaid | ChartRun |
|---|---|
xychart |
type: "column", or type: "line" when every series is a line |
xychart horizontal |
type: "bar". A bar chart's series are all bars, so a line can't be drawn on it |
title "Sales Revenue" |
title: "Sales Revenue" |
x-axis [jan, feb, mar] |
categories: ["jan", "feb", "mar"] |
x-axis "Month" [jan, feb, mar] |
The categories, and categoryAxis: { title: "Month" } |
x-axis 1 --> 5 |
Numbers as the categories, from 1 to 5, spread evenly, one for each value: [1, 2, 3, 4, 5] for five values |
y-axis "Revenue (in $)" 4000 --> 11000 |
valueAxis: { title: "Revenue (in $)", minimum: 4000, maximum: 11000 } |
y-axis "Revenue (in $)" |
valueAxis: { title: "Revenue (in $)" }, and Word picks the range |
bar [5000, 6000] |
A series: { name, values: [5000, 6000] } |
line [5000, 6000] |
A series drawn as a line: { name, values: [5000, 6000], type: "line" } |
bar "North" [5000, 6000], a named series |
{ name: "North", values: [5000, 6000] }, which the legend shows |
| No named series | legend: false |
| Named and unnamed series | Give every series a name, and hide the unnamed ones' from the legend with legend: { hiddenEntries: [...] } |
A Word chart's series each have a name, for the legend and for Edit Data, so give Mermaid's unnamed ones names too. See Column and Bar Charts and Line and Area Charts.
Other Mermaid Diagrams
Mermaid's radar chart is a radar chart here too. Its quadrant, Sankey and Gantt charts have no Word chart to match. Its flowcharts and other diagrams can be drawn as shapes, which lay themselves out as flows and trees: see Shapes.