<t-obsplot>
An Observable Plot chart that shares variables with the page.
Where <t-vega> takes a JSON spec, <t-obsplot> takes a JavaScript expression, so
the chart can compute with the variables directly. Plot loads only on pages that have a
<t-obsplot>.
A wave of
The expression reads amp, damp and freq, so dragging any of them redraws the chart.
Writing the chart
Put the expression in a <script type="text/plain"> inside the element. The browser doesn't run
or show it, and Markdown leaves it alone. Or keep it in its own file and point to it with src:
<t-obsplot src="charts/wave.js"></t-obsplot>
It's an expression, like the expr of a <t-let>, not a script: no return, no
statements. It can give back one of three things:
- Plot options, as in the example:
{ marks: [...], height: 240, ... }. They go toPlot.plotwith a few defaults filled in. - An array of marks, short for
{ marks: [...] }. - A chart you made yourself with
Plot.plot(...), which is shown as it is, without the defaults.
Inside the expression, Plot and d3 are available, along with the page's variables, your
registered functions and the usual Math, Array and friends.
The defaults, each of which the options can override:
widthis the width of the element, and the chart redraws when that changes.- The font comes from
--tangle-chart-font. See Theme.
Plot leaves 40 pixels on the left for the y-axis labels. For longer labels, such as the dollar
amounts below, set marginLeft.
Variables
Every variable the expression reads is tracked, including reads inside a channel function such
as y: (t) => amp * …. When one of them changes, the expression runs again and the chart is
redrawn.
Data works the same way: a variable holding an array of objects can go straight into a mark.
Here a <t-let> computes the 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.
Loading data and reading the pointer
Plot draws arrays and doesn't fetch, so load a file with a short script and put the rows in a
variable. Until they arrive, cars ?? [] gives the chart an empty array to draw.
With name="car", the variable car holds whatever Plot's pointer is on (the datum under a
tip, Plot.pointer or crosshair), or null. That's how the chart sets variables on the page:
Cars with more than
A chart shouldn't read the variable it sets with name. Each pointer move would redraw the
chart, and the redraw would lose the pointer.
What redraws when
Plot draws a chart in one go and can't update part of one, so <t-obsplot> redraws the whole
chart whenever a variable it read changes:
- Changes are batched. Dragging one number may change several variables. The chart redraws once.
- The new chart replaces the old one in place, in the same frame, so nothing flickers.
- A redraw resets the pointer. An open tip closes, and the
namevariable goes back tonulluntil the pointer moves again. - A width change redraws the chart at the new width.
- A theme change needs no redraw. See below.
For charts with tens of thousands of marks, redrawing on every step of a drag can feel slow.
There, <t-vega> updates only what changed.
Theme
Plot draws text, axes and gridlines in currentColor, so the chart follows the text color, light
or dark, without redrawing. On top of that:
- The font size and family come from
--tangle-chart-font, and the strength of the axis labels from--tangle-chart-text(60%). - Tips and the halos around text are drawn on
--tangle-surface, instead of Plot's white. - Single-color marks are
currentColorby default. For the accent color, writefill: "var(--tangle-accent)"orstroke: "var(--tangle-accent)". Plot accepts any CSS color, so avar()works.
Set style in the options to change any of this.
Attributes
| Attribute | Default | Description |
|---|---|---|
src | A URL to load the expression from, instead of the <script> inside. | |
name | A variable to set to the datum under Plot's pointer, or null. See Loading data and reading the pointer. |
How Plot loads
The first <t-obsplot> on a page loads two scripts from jsDelivr, in order: d3 7.9.0 and
Observable Plot 0.6.17. That's about 490 KB of minified JavaScript (less over the network,
because it's compressed). Pages without a <t-obsplot> load none of it.
- If the page already loaded them (
window.d3,window.Plot), that copy is used. - To load them from somewhere else, for example your own server, call
configure({ d3Url, plotUrl })in the module that importstangle.js, or setwindow.TangleConfigbefore it loads. See Configuration. - The expression is compiled with
new Function, like every tangle.js expression, so a Content-Security-Policy needs'unsafe-eval'.
Until Plot has loaded, the element shows "Loading chart…" (.tangle-obsplot.is-loading). A
failure to load Plot, or an error in the expression, shows a message in its place
(.tangle-obsplot-error). The chart comes back on the next change that doesn't fail.