<t-vega>

A Vega-Lite chart that shares variables with the page. Use it for lines, bars, scatter plots, tooltips, stacking, legends from data and selections. Vega loads only on pages that have a <t-vega>.

Cars with more than 150 horsepower are highlighted. Hover over a point to see the car.

The spec declares a parameter named hp, and the page has a variable named hp, so they're linked: dragging the number sets the parameter, and Vega redraws.

Writing the spec

Put the spec, as JSON, in a <script type="application/json"> inside the element. The browser doesn't run or show it, and Markdown leaves it alone. Or keep the spec in its own file and point to it with src:

<t-vega src="charts/cars.vl.json"></t-vega>

Anything Vega-Lite can draw works. A few defaults are filled in for you:

Variables and parameters

A spec and the page share a name in two ways.

Parameters. A top-level entry in params with the name of a variable is linked to that variable, in both directions:

Drag across the chart to select a range of horsepower. Selected: .

Data. A named data source, "data": { "name": "rows" }, is linked to the variable rows. The variable should hold an array of objects. Whenever it changes, Vega gets the new rows:

Save 200 a month at 6 a year. After 30 years, of your money is interest.

Set a new array to update the chart. A <t-let> does this for you, because it computes a new array each time. From JavaScript, call scope.set("rows", [...]): changing the old array in place isn't noticed.

What redraws when

Vega only recomputes the parts of a chart that depend on what changed, and <t-vega> hands it changes in the smallest form it can:

Theme

Unless the spec says otherwise, the chart takes its look from the CSS around it:

When the theme changes (a dark mode toggle, or the system setting), the charts re-render with the new colors. A config in the spec wins over all of this, so you can still set a color scheme for data colors, as in "config": { "range": { "category": { "scheme": "tableau10" } } }.

Attributes

AttributeDefaultDescription
srcA URL to load the spec from, instead of the <script> inside.
renderersvgsvg, or canvas, which is faster for many thousands of points.

How Vega loads

The first <t-vega> on a page loads three scripts from jsDelivr, in order: Vega 6.4.0, Vega-Lite 6.4.3 and vega-embed 7.3.0. That's about 830 KB of minified JavaScript (less over the network, because it's compressed), which is why it isn't part of tangle.js. Pages without a <t-vega> load none of it.

Until Vega has loaded, the element shows "Loading chart…" (.tangle-vega.is-loading). A failure to load Vega or to parse the spec shows a message in its place (.tangle-vega-error).