Vega-Lite API
The Vega-Lite API (version 6.0.0) provides a JavaScript fluent API for programmatically constructing Vega-Lite JSON specifications. It serves as a high-level grammar for visual analysis, enabling developers to define interactive visualizations in a more structured and code-centric manner than direct JSON manipulation. The package tracks the major version of Vega-Lite, ensuring compatibility with the underlying visualization grammar; v6.x of this API is compatible with Vega-Lite v6.x. It differentiates itself by offering a declarative chaining syntax that simplifies complex specification creation, particularly beneficial in environments like Observable notebooks or browser-based applications where dynamic chart generation is common. The API is actively maintained with releases tied to Vega-Lite updates, providing a stable and evolving tool for data visualization, primarily aimed at simplifying the creation of complex charts.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` to import `vega-lite-api` in a Node.js or bundled environment after version 6.0.0, which is ESM-only.fixChange your import statement to `import { vl } from 'vega-lite-api';`. Ensure your `package.json` specifies `"type": "module"` or use `.mjs` file extensions for Node.js. -
TypeError: vl.register is not a function
cause The `vega-lite-api` script might be loaded but the `vl.register` method is called without `vega` and `vegaLite` being available globally or passed correctly in a module context.fixIn a browser, ensure `<script src="https://cdn.jsdelivr.net/npm/vega"></script>` and `<script src="https://cdn.jsdelivr.net/npm/vega-lite"></script>` are loaded before `<script src="https://cdn.jsdelivr.net/npm/vega-lite-api"></script>`, and `vl.register(vega, vegaLite, options)` is called. In a module environment, ensure `vega` and `vegaLite` are correctly imported and passed to `vl.register`. -
TypeError: Cannot read properties of undefined (reading 'empty')
cause Using the deprecated `selection.empty()` method with a string argument, or applying it incorrectly, especially after the v5.0.0 breaking changes.fixReview your selection definitions. The `empty` method now takes a boolean (`true`/`false`) and should be applied to a predicate reference (e.g., `vl.selection().empty(false)`), not directly to the selection definition object. Refer to Vega-Lite v5+ documentation on parameters and selections.
Warnings
- breaking Version 6.0.0 of `vega-lite-api` switched to being an ESM-only package. This means it can no longer be `require()`-d in CommonJS environments (e.g., Node.js scripts without `--experimental-modules` or older bundler configurations).
- breaking Version 5.0.0 significantly revised the `selection` abstraction into a more general `parameters` abstraction. Specifically, the `selection.empty` method now expects a boolean argument instead of a string, and should be applied to a predicate reference to a selection, not directly to a selection definition.
- gotcha When using `vega-lite-api` in a browser environment (e.g., via CDN), you must explicitly register the `vl` object with the `vega` and `vegaLite` global objects using `vl.register(vega, vegaLite, options)` before creating any charts.
- gotcha The major version of `vega-lite-api` tracks the major version of `vega-lite`. Therefore, upgrading `vega-lite-api` to a new major version often implies that you should also upgrade `vega-lite` to the corresponding major version to maintain compatibility.
Install
-
npm install vega-lite-api -
yarn add vega-lite-api -
pnpm add vega-lite-api
Imports
- vl
const vl = require('vega-lite-api');import { vl } from 'vega-lite-api'; - vl (global)
/* (after loading CDN scripts) */ vl.markBar();
Quickstart
import { vl } from 'vega-lite-api';
import vega from 'vega';
import vegaLite from 'vega-lite';
// In a browser environment, you would typically load vega, vega-lite, and vega-tooltip via CDN.
// For Node.js or bundlers, you'd import them.
// For this example, we mock the registration that happens in a browser.
const options = {
config: {
// vega-lite default configuration
},
init: (view) => {
// Example: enable horizontal scrolling for large plots
// if (view.container()) view.container().style["overflow-x"] = "auto";
},
view: {
// view constructor options
loader: vega.loader({
baseURL: "https://cdn.jsdelivr.net/npm/vega-datasets@1/"
}),
renderer: "canvas"
}
};
// Simulate browser registration for demonstration
const mockGlobalVega = vega;
const mockGlobalVegaLite = vegaLite;
if (typeof globalThis !== 'undefined') {
globalThis.vega = mockGlobalVega;
globalThis.vegaLite = mockGlobalVegaLite;
}
vl.register(mockGlobalVega, mockGlobalVegaLite, options);
const chartSpec = vl.markBar({ tooltip: true })
.data([
{ a: "A", b: 28 },
{ a: "B", b: 55 },
{ a: "C", b: 43 },
{ a: "D", b: 91 },
{ a: "E", b: 81 }
])
.encode(
vl.x().fieldQ("b"),
vl.y().fieldN("a"),
vl.tooltip([vl.fieldQ("b"), vl.fieldN("a")])
)
.toJSON();
// In a real browser scenario, you would call .render().then(chart => document.body.appendChild(chart));
// For a Node.js context, you would typically work with the JSON specification.
console.log(JSON.stringify(chartSpec, null, 2));