<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
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:
- If the spec has no
width, the chart fills the width of the page ("width": "container"). - The colors and fonts come from your CSS. See Theme.
- Vega's action menu (export, view source) is off.
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:
- When the variable changes (the reader drags it, or an expression it depends on changes), the parameter gets the new value and Vega redraws. Vega's dataflow only recomputes what uses it.
- When the parameter changes inside Vega, for example through a slider made with
bind, the variable gets the new value, and every output and formula that uses it updates. - A parameter with
select(a selection) only goes from Vega to the page. Read it in expressions to show what the reader selected:
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
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:
- A parameter is set on its own. Dragging
hpin the first example re-evaluates only the opacity of each point; the data isn't fetched or parsed again, and only the points that cross the threshold change on screen. - A data change is diffed. The new array is compared with the old one row by row. Rows at the same position are updated field by field, and only extra rows are added or missing ones removed. In the savings example, dragging the rate updates the "Interest" bars in place; the "Paid in" bars and the rest of the chart aren't rebuilt.
- Changes are batched. Dragging one number may change several variables. They're applied together, and the chart redraws once.
- A theme change re-renders the whole chart, since every color changes.
Theme
Unless the spec says otherwise, the chart takes its look from the CSS around it:
- text, axes and gridlines from the text color (
currentColor), - the color of single-color marks from
--tangle-accent, - label size and font from
--tangle-chart-font, and the strength of labels and gridlines from--tangle-chart-text(60%) and--tangle-chart-grid(12%).
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
| Attribute | Default | Description |
|---|---|---|
src | A URL to load the spec from, instead of the <script> inside. | |
renderer | svg | svg, 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.
- If the page already loaded Vega (
window.vega,window.vegaLite,window.vegaEmbed), that copy is used. - To load it from somewhere else, for example your own server, call
configure({ vegaUrl, vegaLiteUrl, vegaEmbedUrl })in the module that importstangle.js, or setwindow.TangleConfigbefore it loads. See Configuration. - Vega compiles its expressions with
new Function, just like tangle.js, so a Content-Security-Policy needs'unsafe-eval'for both.
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).