Files
docx/docs/usage/chart-scatter.md
T
DolanandClaude Opus 5.5 c169dd1a83 feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded (#3556)
* 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>
2026-09-28 13:00:29 +01:00

9.5 KiB

Scatter Charts

A scatter chart plots points, each with its own x and y. Use it to show how two numbers relate, such as people's heights and weights.

Charts shows how to import ChartRun and add a chart to a document.

A Scatter Chart

import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ChartRun({
                            type: "scatter",
                            title: "Height and weight",
                            series: [
                                {
                                    name: "Group A",
                                    points: [
                                        { x: 150, y: 50 },
                                        { x: 165, y: 61 },
                                        { x: 180, y: 75 },
                                    ],
                                },
                                {
                                    name: "Group B",
                                    points: [
                                        { x: 155, y: 58 },
                                        { x: 170, y: 72 },
                                    ],
                                },
                            ],
                            xAxis: { title: "Height (cm)" },
                            yAxis: { title: "Weight (kg)" },
                        }),
                    ],
                }),
            ],
        },
    ],
});

A scatter chart has no categories. Instead, each series has its own points, and each point is an { x, y } pair. Series can have different numbers of points.

Markers and Lines

A scatter chart draws a circle at each point, and doesn't join them:

  • markers: false leaves out the circles.
  • lines joins each series' points, in order: "none" (the default), "straight" or "smooth".

Markers can have a shape and size, as on a line chart, and a series' line a colour, width and dash pattern. A series' own markers replace the chart's. See Markers and Lines.

import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ChartRun({
                            type: "scatter",
                            series: [
                                {
                                    name: "Group A",
                                    points: [
                                        { x: 150, y: 50 },
                                        { x: 165, y: 61 },
                                    ],
                                    markers: { shape: "triangle", size: 7 },
                                },
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

A smooth line without markers draws a curve, such as a graph of a formula:

import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ChartRun({
                            type: "scatter",
                            series: [{ name: "y = x²", points: [-2, -1, 0, 1, 2].map((x) => ({ x, y: x * x })) }],
                            markers: false,
                            lines: "smooth",
                        }),
                    ],
                }),
            ],
        },
    ],
});

Axes

A scatter chart has two value axes: xAxis along the bottom and yAxis up the left side. Each takes a title, a range and a number format, as a value axis does. See Chart Axes.

Options

These are all the options of a scatter chart. Each links to the page that explains it.

Property Type Notes Description
type "scatter" Required
series ScatterChartSeries[] Required { name, points, color?, markers?, line?, dataLabels?, pointLabels?, trendlines?, xErrorBars?, yErrorBars? }, where a point is { x, y }. See Chart Data Labels and Chart Trendlines and Error Bars
markers boolean | ChartMarker Optional A marker at each point. Default true
lines "none" | "straight" | "smooth" Optional How the points are joined. Default "none"
xAxis ChartValueAxis Optional The horizontal axis. See Chart Axes
yAxis ChartValueAxis Optional The vertical axis. See Chart Axes
dataLabels ChartDataLabels Optional See Chart Data Labels
title string | ChartTitle Optional See Chart Titles and Legends
legend false | ChartLegend Optional See Chart Titles and Legends
transformation { width, height } Optional See Chart Size and Position
floating IFloating Optional See Chart Size and Position
altText DocPropertiesOptions Optional See Chart Size and Position

A series' line is only for a chart whose lines join the points. A bubble chart is a scatter chart whose points have sizes. Chart Fonts and Fills shows the options every chart has for its text, background and border.