Color mode

Vega-Lite

Renders ```vega-lite code blocks as Vega-Lite charts. Charts are drawn in the browser, and the Vega runtime is imported dynamically only when it is needed.

Japanese

Configure

ts
import { defineConfig } from "@riebeckite/core";
import { vegaLite } from "@riebeckite/plugin-vega-lite";
 
export default defineConfig({
  // ...
  plugins: [
    vegaLite({
      caption: true,
      theme: "light",
      renderer: "canvas",
    }),
  ],
});

The plugin runs with order: -10.

Syntax

Put a Vega-Lite specification as JSON in the body of the code block. Both vega-lite and vega are accepted.

markdown
```vega-lite
{
  "title": "Revenue",
  "data": {
    "values": [
      { "category": "A", "value": 28 },
      { "category": "B", "value": 55 }
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": { "field": "category", "type": "nominal" },
    "y": { "field": "value", "type": "quantitative" }
  }
}
```

The caption comes from the code-block title, or from the specification's title.

How it renders

A ```vega-lite block becomes a figure.rb-vega-lite:

  • figure.rb-vega-lite: carries data-vega-lite="pending" and data-vega-lite-spec (the escaped JSON)
  • div.rb-vega-lite__canvas: the element the chart is rendered into (role="img")
  • figcaption.rb-vega-lite__caption: the caption (enabled by default)
  • details.rb-vega-lite__fallback: the original spec, folded away

initVegaLite finds [data-vega-lite], JSON.parses data-vega-lite-spec, then dynamically imports vega, vega-lite, and vega-embed and renders with vega-embed. On success the figure becomes data-vega-lite="rendered".

If loading vega-embed, parsing the spec, or embedding fails, the initializer does not throw: it opens that figure's details and marks it data-vega-lite="error".

A block whose body is not valid JSON is left as a normal code block, and a diagnostic with source: "@riebeckite/plugin-vega-lite" is emitted.

Options

Option Default Description
caption true Show the title as a caption
actions null Show the vega-embed actions menu (true / false / null)
theme "light" Colour scheme ("light", "dark", "none")
renderer "canvas" Vega renderer ("canvas" or "svg")
className "rb-vega-lite" Base class applied to the figure

When actions is null, the vega-embed default (actions shown) applies. With theme set to "light" or "none", Vega's default light styling is used; only "dark" applies the built-in dark theme.

Client rendering

The client initializer is static and receives no plugin options. actions, theme, and renderer are embedded into the figure's data-vega-lite-* attributes, and initVegaLite reads them from there.

The Vega runtime is loaded with import(), so the page still renders when JavaScript is disabled and the spec stays readable in the fallback details.

Output hooks

  • figure[data-vega-lite]: state (pending / rendered / error)
  • figure[data-vega-lite-spec]: the escaped spec JSON
  • figure[data-vega-lite-theme], figure[data-vega-lite-renderer], figure[data-vega-lite-actions]
  • [data-vega-lite-canvas]: the render target
  • details.rb-vega-lite__fallback: the original spec

Main exports

  • vegaLite(options?): create the plugin (vegaLitePlugin is an alias)
  • initVegaLite: initialize client-side rendering
  • Types: VegaLiteOptions, VegaLiteSpec, VegaLiteTheme, VegaLiteRenderer

Limitations

  • Rendering is client-only. Nothing is rendered at build time, so charts are not visible without JavaScript (the spec remains in the fallback details)
  • Each chart loads vega, vega-lite, and vega-embed, so pages with many charts pay for the transfer and render cost repeatedly
  • Not every Vega-Lite feature is validated; complex specs surface as browser errors and reveal the fallback
  • vega, vega-lite, and vega-embed are BSD-3-Clause licensed

See also

History

1 changesCollapseExpand
1 + <!-- Generated from packages/plugins/vega-lite/README.md. Do not edit this page directly; edit the package README and run `pnpm docs:sync`. -->
2 +
3 + # Vega-Lite
4 +
5 + Renders ` ```vega-lite ` code blocks as Vega-Lite charts. Charts are drawn in the
6 + browser, and the Vega runtime is imported dynamically only when it is needed.
7 +
8 + [Japanese](./vega-lite.md)
9 +
10 + ## Configure
11 +
12 + ```ts
13 + import { defineConfig } from "@riebeckite/core";
14 + import { vegaLite } from "@riebeckite/plugin-vega-lite";
15 +
16 + export default defineConfig({
17 + // ...
18 + plugins: [
19 + vegaLite({
20 + caption: true,
21 + theme: "light",
22 + renderer: "canvas",
23 + }),
24 + ],
25 + });
26 + ```
27 +
28 + The plugin runs with `order: -10`.
29 +
30 + ## Syntax
31 +
32 + Put a Vega-Lite specification as JSON in the body of the code block. Both
33 + `vega-lite` and `vega` are accepted.
34 +
35 + ````markdown
36 + ```vega-lite
37 + {
38 + "title": "Revenue",
39 + "data": {
40 + "values": [
41 + { "category": "A", "value": 28 },
42 + { "category": "B", "value": 55 }
43 + ]
44 + },
45 + "mark": "bar",
46 + "encoding": {
47 + "x": { "field": "category", "type": "nominal" },
48 + "y": { "field": "value", "type": "quantitative" }
49 + }
50 + }
51 + ```
52 + ````
53 +
54 + The caption comes from the code-block `title`, or from the specification's
55 + `title`.
56 +
57 + ## How it renders
58 +
59 + A ` ```vega-lite ` block becomes a `figure.rb-vega-lite`:
60 +
61 + - `figure.rb-vega-lite`: carries `data-vega-lite="pending"` and
62 + `data-vega-lite-spec` (the escaped JSON)
63 + - `div.rb-vega-lite__canvas`: the element the chart is rendered into
64 + (`role="img"`)
65 + - `figcaption.rb-vega-lite__caption`: the caption (enabled by default)
66 + - `details.rb-vega-lite__fallback`: the original spec, folded away
67 +
68 + `initVegaLite` finds `[data-vega-lite]`, `JSON.parse`s `data-vega-lite-spec`,
69 + then dynamically imports `vega`, `vega-lite`, and `vega-embed` and renders with
70 + `vega-embed`. On success the figure becomes `data-vega-lite="rendered"`.
71 +
72 + If loading `vega-embed`, parsing the spec, or embedding fails, the initializer
73 + does not throw: it opens that figure's `details` and marks it
74 + `data-vega-lite="error"`.
75 +
76 + A block whose body is not valid JSON is left as a normal code block, and a
77 + diagnostic with `source: "@riebeckite/plugin-vega-lite"` is emitted.
78 +
79 + ## Options
80 +
81 + | Option | Default | Description |
82 + | --- | --- | --- |
83 + | `caption` | `true` | Show the `title` as a caption |
84 + | `actions` | `null` | Show the `vega-embed` actions menu (`true` / `false` / `null`) |
85 + | `theme` | `"light"` | Colour scheme (`"light"`, `"dark"`, `"none"`) |
86 + | `renderer` | `"canvas"` | Vega renderer (`"canvas"` or `"svg"`) |
87 + | `className` | `"rb-vega-lite"` | Base class applied to the figure |
88 +
89 + When `actions` is `null`, the `vega-embed` default (actions shown) applies. With
90 + `theme` set to `"light"` or `"none"`, Vega's default light styling is used; only
91 + `"dark"` applies the built-in `dark` theme.
92 +
93 + ## Client rendering
94 +
95 + The client initializer is static and receives no plugin options. `actions`,
96 + `theme`, and `renderer` are embedded into the figure's `data-vega-lite-*`
97 + attributes, and `initVegaLite` reads them from there.
98 +
99 + The Vega runtime is loaded with `import()`, so the page still renders when
100 + JavaScript is disabled and the spec stays readable in the fallback `details`.
101 +
102 + ## Output hooks
103 +
104 + - `figure[data-vega-lite]`: state (`pending` / `rendered` / `error`)
105 + - `figure[data-vega-lite-spec]`: the escaped spec JSON
106 + - `figure[data-vega-lite-theme]`, `figure[data-vega-lite-renderer]`,
107 + `figure[data-vega-lite-actions]`
108 + - `[data-vega-lite-canvas]`: the render target
109 + - `details.rb-vega-lite__fallback`: the original spec
110 +
111 + ## Main exports
112 +
113 + - `vegaLite(options?)`: create the plugin (`vegaLitePlugin` is an alias)
114 + - `initVegaLite`: initialize client-side rendering
115 + - Types: `VegaLiteOptions`, `VegaLiteSpec`, `VegaLiteTheme`,
116 + `VegaLiteRenderer`
117 +
118 + ## Limitations
119 +
120 + - Rendering is client-only. Nothing is rendered at build time, so charts are not
121 + visible without JavaScript (the spec remains in the fallback `details`)
122 + - Each chart loads `vega`, `vega-lite`, and `vega-embed`, so pages with many
123 + charts pay for the transfer and render cost repeatedly
124 + - Not every Vega-Lite feature is validated; complex specs surface as browser
125 + errors and reveal the fallback
126 + - `vega`, `vega-lite`, and `vega-embed` are BSD-3-Clause licensed
127 +
128 + ## See also
129 +
130 + - [Plugin system](../reference/plugin-api.en.md)
131 +