<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 2, with an amplitude of 1.5, damped by 0.8 per second.

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:

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:

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 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.

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 150 horsepower are highlighted. You're pointing at .

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:

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:

Set style in the options to change any of this.

Attributes

AttributeDefaultDescription
srcA URL to load the expression from, instead of the <script> inside.
nameA 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.

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.