Files
docx/docs/usage/chart-bubble.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

141 lines
9.1 KiB
Markdown

# Bubble Charts
A bubble chart is a scatter chart whose points are bubbles of different sizes, so it shows three numbers for each point: where it is across, where it is up, and how big it is.
[Charts](usage/charts.md) shows how to import `ChartRun` and add a chart to a document.
## A Bubble Chart
```ts live
import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "bubble",
title: "Markets",
series: [
{
name: "Europe",
points: [
{ x: 12, y: 4.5, size: 740 },
{ x: 18, y: 3.1, size: 330 },
],
},
{ name: "Asia", points: [{ x: 24, y: 6.8, size: 1400 }] },
],
xAxis: { title: "Market share (%)" },
yAxis: { title: "Growth (%)" },
}),
],
}),
],
},
],
});
```
- Like a [scatter chart](usage/chart-scatter.md), a bubble chart has no categories. Each series has `points`, and each point has an `x`, a `y` and a `size`.
- Sizes are compared with each other: the biggest bubble is drawn at Office's largest size, and the others in proportion. They can't be negative.
- Bubbles are drawn in their series' colour, a quarter see-through, so bubbles behind them show.
## Bubble Sizes
- `bubbleScale` makes every bubble bigger or smaller, as a percentage of Office's size, from `0` to `300`. The default is `100`.
- `sizeRepresents` says whether a point's `size` is the bubble's area (`"area"`, the default) or its width (`"width"`). By area, a bubble of size 4 looks twice as wide as one of size 1; by width, four times as wide.
```ts live
import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "bubble",
series: [
{
name: "Europe",
points: [
{ x: 12, y: 4.5, size: 740 },
{ x: 18, y: 3.1, size: 330 },
],
},
{ name: "Asia", points: [{ x: 24, y: 6.8, size: 1400 }] },
],
bubbleScale: 60,
sizeRepresents: "width",
}),
],
}),
],
},
],
});
```
## Labels
A bubble's label can show its size, as well as what any label shows:
```ts live
import { Document, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartRun({
type: "bubble",
series: [
{
name: "Europe",
points: [
{ x: 12, y: 4.5, size: 740 },
{ x: 18, y: 3.1, size: 330 },
],
},
{ name: "Asia", points: [{ x: 24, y: 6.8, size: 1400 }] },
],
dataLabels: { bubbleSize: true },
}),
],
}),
],
},
],
});
```
See [Chart Data Labels](usage/chart-data-labels.md).
## Options
| Property | Type | Notes | Description |
| ---------------- | ------------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `type` | `"bubble"` | Required | |
| `series` | `BubbleChartSeries[]` | Required | `{ name, points, color?, dataLabels?, pointLabels?, trendlines?, xErrorBars?, yErrorBars? }`, where a point is `{ x, y, size }`. See [Chart Data Labels](usage/chart-data-labels.md#labels-of-single-points) and [Chart Trendlines and Error Bars](usage/chart-trendlines-and-error-bars.md) |
| `bubbleScale` | `number` | Optional | The bubbles' size, as a percentage of Office's, from 0 to 300. Default `100` |
| `sizeRepresents` | `"area"` \| `"width"` | Optional | Whether a size is a bubble's area or width. Default `"area"` |
| `xAxis` | `ChartValueAxis` | Optional | The horizontal axis. See [Chart Axes](usage/chart-axes.md) |
| `yAxis` | `ChartValueAxis` | Optional | The vertical axis. See [Chart Axes](usage/chart-axes.md) |
| `dataLabels` | `BubbleChartDataLabels` | Optional | `{ value?, category?, seriesName?, bubbleSize?, ... }`. See [Chart Data Labels](usage/chart-data-labels.md) |
| `title` | `string` \| `ChartTitle` | Optional | See [Chart Titles and Legends](usage/chart-titles-and-legends.md) |
| `legend` | `false` \| `ChartLegend` | Optional | See [Chart Titles and Legends](usage/chart-titles-and-legends.md) |
| `transformation` | `{ width, height }` | Optional | See [Chart Size and Position](usage/chart-size-and-position.md) |
| `floating` | `IFloating` | Optional | See [Chart Size and Position](usage/chart-size-and-position.md) |
| `altText` | `DocPropertiesOptions` | Optional | See [Chart Size and Position](usage/chart-size-and-position.md) |
[Chart Fonts and Fills](usage/chart-fonts-and-fills.md) shows the options every chart has for its text, background and border.