Color mode

Chart.js

Render ```chart code blocks as responsive Chart.js charts.

日本語

Overview

chartjs() rewrites each ```chart fenced code block into a <figure> that carries the Chart.js configuration as JSON. The charts themselves are drawn in the browser by initChartJs, which dynamically imports chart.js/auto; the build only emits the markup. It runs with order: -10.

Usage

ts
import { defineConfig } from "@riebeckite/core";
import { chartjs } from "@riebeckite/plugin-chartjs";
 
export default defineConfig({
  // ...
  plugins: [
    chartjs({
      responsive: true,
      caption: true,
    }),
  ],
});

Writing a chart

The body of a chart block is a JSON object. Use either a complete Chart.js configuration:

markdown
```chart
{
  "type": "bar",
  "data": {
    "labels": ["Mon", "Tue", "Wed"],
    "datasets": [{ "label": "Visits", "data": [12, 19, 8] }]
  },
  "options": { "plugins": { "legend": { "display": false } } }
}
```

or the shorthand form, where labels and datasets sit at the top level and are normalized into data:

markdown
```chart
{
  "type": "line",
  "labels": ["Mon", "Tue", "Wed"],
  "datasets": [{ "label": "Visits", "data": [12, 19, 8] }]
}
```

Any other top-level keys (options, plugins, …) are preserved on the normalized configuration.

Captions

A caption is taken from the code block title:

markdown
```chart title="Weekly visits"
{ "type": "bar", "labels": ["Mon"], "datasets": [{ "data": [1] }] }
```

or from a "caption" key in the JSON. The key is removed before the configuration is serialized, so Chart.js never sees it:

markdown
```chart
{
  "type": "bar",
  "caption": "Weekly visits",
  "labels": ["Mon"],
  "datasets": [{ "data": [1] }]
}
```

Output

html
<figure class="rb-chartjs" data-chartjs-marker="RIEBECKITE_EXTERNAL_CHARTJS_MARKER">
  <canvas
    class="rb-chartjs__canvas"
    data-chartjs-config="{&quot;type&quot;:&quot;bar&quot;,...}"
    role="img"
    aria-label="Weekly visits"
  ></canvas>
  <figcaption class="rb-chartjs__caption">Weekly visits</figcaption>
</figure>
  • .rb-chartjs — figure wrapper (responsive, themed with --rb-color-*)
  • .rb-chartjs__canvas — the <canvas> Chart.js draws into; its data-chartjs-config attribute holds the escaped JSON configuration
  • .rb-chartjs__caption — the <figcaption> shown when a caption is present

Options

Option Type Default Description
responsive boolean true Default for options.responsive on charts that do not set it
caption boolean true Render the caption as a <figcaption>
className string "rb-chartjs" Base class for the figure

Diagnostics

A block whose body is not valid JSON, is not an object, is missing type, or lacks data/labels/datasets is left untouched and reported as a diagnostic with source: "@riebeckite/plugin-chartjs" and ruleId: "invalid-config".

Client rendering

Chart.js is not bundled at build time. The site's client bundle must call initChartJs, which chartjs() wires up through createClientEntry. It looks for canvas[data-chartjs-config], parses each payload, imports chart.js/auto, and constructs a chart per canvas. A parse error or a Chart.js exception skips that canvas only.

Limitations

  • The E2E build only asserts the emitted markup; charts need a real browser to appear.
  • The chart configuration is inlined as an HTML attribute, so keep datasets reasonably small.
  • Only the chart info string is recognized; other languages are untouched.

See also

History

1 changesCollapseExpand
1 + <!-- Generated from packages/plugins/chartjs/README.md. Do not edit this page directly; edit the package README and run `pnpm docs:sync`. -->
2 +
3 + # Chart.js
4 +
5 + Render ` ```chart ` code blocks as responsive [Chart.js](https://www.chartjs.org/)
6 + charts.
7 +
8 + [日本語](./chartjs.md)
9 +
10 + ## Overview
11 +
12 + `chartjs()` rewrites each ` ```chart ` fenced code block into a `<figure>`
13 + that carries the Chart.js configuration as JSON. The charts themselves are
14 + drawn in the browser by `initChartJs`, which dynamically imports
15 + `chart.js/auto`; the build only emits the markup. It runs with `order: -10`.
16 +
17 + ## Usage
18 +
19 + ```ts
20 + import { defineConfig } from "@riebeckite/core";
21 + import { chartjs } from "@riebeckite/plugin-chartjs";
22 +
23 + export default defineConfig({
24 + // ...
25 + plugins: [
26 + chartjs({
27 + responsive: true,
28 + caption: true,
29 + }),
30 + ],
31 + });
32 + ```
33 +
34 + ## Writing a chart
35 +
36 + The body of a `chart` block is a JSON object. Use either a complete Chart.js
37 + configuration:
38 +
39 + ````markdown
40 + ```chart
41 + {
42 + "type": "bar",
43 + "data": {
44 + "labels": ["Mon", "Tue", "Wed"],
45 + "datasets": [{ "label": "Visits", "data": [12, 19, 8] }]
46 + },
47 + "options": { "plugins": { "legend": { "display": false } } }
48 + }
49 + ```
50 + ````
51 +
52 + or the shorthand form, where `labels` and `datasets` sit at the top level and
53 + are normalized into `data`:
54 +
55 + ````markdown
56 + ```chart
57 + {
58 + "type": "line",
59 + "labels": ["Mon", "Tue", "Wed"],
60 + "datasets": [{ "label": "Visits", "data": [12, 19, 8] }]
61 + }
62 + ```
63 + ````
64 +
65 + Any other top-level keys (`options`, `plugins`, …) are preserved on the
66 + normalized configuration.
67 +
68 + ### Captions
69 +
70 + A caption is taken from the code block `title`:
71 +
72 + ````markdown
73 + ```chart title="Weekly visits"
74 + { "type": "bar", "labels": ["Mon"], "datasets": [{ "data": [1] }] }
75 + ```
76 + ````
77 +
78 + or from a `"caption"` key in the JSON. The key is removed before the
79 + configuration is serialized, so Chart.js never sees it:
80 +
81 + ````markdown
82 + ```chart
83 + {
84 + "type": "bar",
85 + "caption": "Weekly visits",
86 + "labels": ["Mon"],
87 + "datasets": [{ "data": [1] }]
88 + }
89 + ```
90 + ````
91 +
92 + ## Output
93 +
94 + ````html
95 + <figure class="rb-chartjs" data-chartjs-marker="RIEBECKITE_EXTERNAL_CHARTJS_MARKER">
96 + <canvas
97 + class="rb-chartjs__canvas"
98 + data-chartjs-config="{&quot;type&quot;:&quot;bar&quot;,...}"
99 + role="img"
100 + aria-label="Weekly visits"
101 + ></canvas>
102 + <figcaption class="rb-chartjs__caption">Weekly visits</figcaption>
103 + </figure>
104 + ````
105 +
106 + - `.rb-chartjs` — figure wrapper (responsive, themed with `--rb-color-*`)
107 + - `.rb-chartjs__canvas` — the `<canvas>` Chart.js draws into; its
108 + `data-chartjs-config` attribute holds the escaped JSON configuration
109 + - `.rb-chartjs__caption` — the `<figcaption>` shown when a caption is present
110 +
111 + ## Options
112 +
113 + | Option | Type | Default | Description |
114 + | ------ | ---- | ------- | ----------- |
115 + | `responsive` | `boolean` | `true` | Default for `options.responsive` on charts that do not set it |
116 + | `caption` | `boolean` | `true` | Render the caption as a `<figcaption>` |
117 + | `className` | `string` | `"rb-chartjs"` | Base class for the figure |
118 +
119 + ## Diagnostics
120 +
121 + A block whose body is not valid JSON, is not an object, is missing `type`, or
122 + lacks `data`/`labels`/`datasets` is left untouched and reported as a
123 + diagnostic with `source: "@riebeckite/plugin-chartjs"` and
124 + `ruleId: "invalid-config"`.
125 +
126 + ## Client rendering
127 +
128 + Chart.js is **not** bundled at build time. The site's client bundle must call
129 + `initChartJs`, which `chartjs()` wires up through `createClientEntry`. It looks
130 + for `canvas[data-chartjs-config]`, parses each payload, imports
131 + `chart.js/auto`, and constructs a chart per canvas. A parse error or a Chart.js
132 + exception skips that canvas only.
133 +
134 + ## Limitations
135 +
136 + - The E2E build only asserts the emitted markup; charts need a real browser to
137 + appear.
138 + - The chart configuration is inlined as an HTML attribute, so keep datasets
139 + reasonably small.
140 + - Only the `chart` info string is recognized; other languages are untouched.
141 +
142 + ## See also
143 +
144 + - [Plugin guide](../reference/plugin-api.en.md)
145 +