Files
docx/docs/usage/chart-column-and-bar.md
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

13 KiB

Column and Bar Charts

A column chart draws a vertical bar for each value, and a bar chart a horizontal one. Use them to compare values across categories. A bar chart suits long category names, which have room to the left of the bars.

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

A Column 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 by quarter",
                            categories: ["Q1", "Q2", "Q3", "Q4"],
                            series: [
                                { name: "2024", values: [120, 135, 150, 170] },
                                { name: "2025", values: [140, 150, 165, null] },
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

Each category has a group of columns, one for each series, side by side. The first series is on the left of each group.

A Bar Chart

A bar chart takes the same options, with type: "bar":

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

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ChartRun({
                            type: "bar",
                            categories: ["Design", "Build", "Test"],
                            series: [{ name: "Hours", values: [30, 80, 40] }],
                        }),
                    ],
                }),
            ],
        },
    ],
});

The categories run up the left side, so the first category is at the bottom, as in Word. The values run along the bottom.

Stacking

stacking puts each group's bars on top of each other, as Word's "Stacked" and "100% Stacked" charts do:

stacking Draws
"none" (default) The bars side by side
"stacked" The bars on top of each other, so each stack is as long as the category's total
"percent" The bars on top of each other, as percentages of the category's total, so each stack is 100%
import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ChartRun({
                            type: "bar",
                            title: "Hours by team",
                            categories: ["Design", "Build", "Test"],
                            series: [
                                { name: "Planned", values: [30, 80, 40] },
                                { name: "Extra", values: [5, 20, 15] },
                            ],
                            stacking: "stacked",
                        }),
                    ],
                }),
            ],
        },
    ],
});

On a 100% stacked chart, the value axis is labelled as percentages. See Chart Axes.

Spacing

gapWidth is the space between groups of bars, and overlap is how much the bars in a group overlap. Both are percentages of a bar's width.

  • gapWidth goes from 0 (no space between groups) to 500 (five bars' width). A smaller gap makes the bars wider.
  • overlap goes from -100 (a bar's width apart) to 100 (each bar on top of the one before). 0 puts them side by side, touching.
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", "Q4"],
                            series: [
                                { name: "2024", values: [120, 135, 150, 170] },
                                { name: "2025", values: [140, 150, 165, 180] },
                            ],
                            gapWidth: 50,
                            overlap: 0,
                        }),
                    ],
                }),
            ],
        },
    ],
});

The defaults are Office's:

Column chart Bar chart Stacked
gapWidth 219 182 150
overlap -27 0 100, and can't change

Bar Colours

Each series' bars take its colour. colors gives single bars colours of their own, in the order of the categories, such as to pick out the best result:

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

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ChartRun({
                            type: "column",
                            title: "Tickets closed",
                            categories: ["Ana", "Ben", "Cai", "Dee"],
                            series: [{ name: "Tickets", values: [42, 57, 38, 71], colors: [undefined, undefined, undefined, "70AD47"] }],
                        }),
                    ],
                }),
            ],
        },
    ],
});

A bar without a colour, or with undefined, keeps the series' colour. Chart Colours explains the colours a chart can take.

Lines on a Column Chart

A column chart's series can be drawn as lines or areas too, such as a target line over the columns, and against a second value axis. See Combo Charts.

Options

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

Property Type Notes Description
type "column" | "bar" Required Vertical or horizontal bars
categories (string | number | Date)[] | ChartCategoryGroup[] Required The categories, in order, or in groups. See Chart Data
series ChartSeries[] Required { name, values, color?, ... }, with a value or null for each category. See Series
stacking "none" | "stacked" | "percent" Optional See Stacking. Default "none"
gapWidth number Optional The space between groups of bars, from 0 to 500. See Spacing
overlap number Optional How much the bars in a group overlap, from -100 to 100. See Spacing
title string | ChartTitle Optional See Chart Titles and Legends
legend false | ChartLegend Optional See Chart Titles and Legends
categoryAxis ChartAxis Optional The axis of categories. See Chart Axes
valueAxis ChartValueAxis Optional The axis of values. See Chart Axes
secondaryValueAxis ChartValueAxis Optional A second axis of values. See Combo Charts
dataLabels ChartDataLabels Optional See Chart Data Labels
emptyValues "gap" | "zero" | "connect" Optional How null is drawn. Default "gap". See Empty Values
dataTable boolean | ChartDataTable Optional A table of the data. See Chart Titles and Legends
font ChartFont Optional See Chart Fonts and Fills
chartArea ChartAreaStyle Optional See Chart Fonts and Fills
plotArea ChartAreaStyle Optional See Chart Fonts and Fills
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
decorative boolean Optional See Chart Size and Position

Series

Property Type Notes Description
name string Required The series' name, shown in the legend
values (number | null)[] Required A value for each category, or null for a gap
color string | ThemeColor Optional See Chart Colours
colors (string | ThemeColor | undefined)[] Optional A colour for each bar. See Bar Colours
dataLabels false | ChartDataLabels Optional The series' own labels. See Chart Data Labels
type "column" | "line" | "area" Optional Column charts: draws the series another way. See Combo Charts
axis "primary" | "secondary" Optional See Combo Charts
pointLabels (false | ChartPointLabel | undefined)[] Optional Labels of single bars. See Chart Data Labels
trendlines ChartTrendline[] Optional Not for stacked series. See Chart Trendlines and Error Bars
errorBars ChartErrorBars Optional See Chart Trendlines and Error Bars