D3.js can turn a scientific model into a visual explanation that learners can manipulate directly. In Quarto, D3 works particularly well when several parts of a figure should respond to the same input, or when the marks in a figure need custom appearance, movement, or interaction.

The example below is intentionally modest. It uses radioactive decay as plausible course content, but its main purpose is to show a reusable authoring pattern:

A coordinated interactive figure

Move the slider through five half-lives. The grid shows the state of 100 individual nuclei, while the chart shows the same realization as an aggregate count. Select Generate another sample to see a different stochastic realization without changing the theoretical curve.

The fading circles and the step-shaped line are not independent illustrations. They are two views of the same array of 100 objects, so selecting a new time or sample keeps every part of the explanation consistent.

The reusable pattern

Quarto provides the reactive layer through Observable JS. The input values time and sample become ordinary variables, and cells that use them are automatically recalculated. D3 provides the lower-level control over the SVG.

First, load D3 and create a control:

```{ojs}
d3 = require("d3@7")

viewof time = Inputs.range([0, 5], {
  value: 0,
  step: 0.1,
  label: "Time (half-lives)"
})
```

Each nucleus is represented by one object. Its position is fixed, while its randomly generated decayTime determines its state at any selected time:

```{ojs}
nuclei = {
  const random = seededRandom(2027);
  return d3.range(100).map(id => ({
    id,
    row: Math.floor(id / 10),
    column: id % 10,
    decayTime: -Math.log2(1 - random())
  }));
}
```

D3’s data join creates one SVG circle per object. The id key keeps the link between an object and its circle explicit:

const circles = svg.append("g")
  .selectAll("circle")
  .data(nuclei, d => d.id)
  .join("circle")
  .attr("cx", d => grid.x + d.column * grid.spacing)
  .attr("cy", d => grid.y + d.row * grid.spacing)
  .attr("r", 9);

When time changes, the SVG is retained and its existing circles are updated. Interrupting an unfinished transition makes rapid slider movement behave cleanly:

circles
  .interrupt()
  .transition()
  .duration(220)
  .attr("r", d => d.decayTime > time ? 9 : 5)
  .attr("opacity", d => d.decayTime > time ? 1 : 0.28);

This separation is useful in larger examples: create the SVG structure once, then write a small update step that maps the current reactive state onto it.

Why use D3 here?

For a conventional scatter plot or bar chart, a higher-level tool such as Observable Plot will usually require less code. D3 becomes attractive when the teaching idea calls for custom visual marks, transitions, direct manipulation, or several views that must update together.

The same pattern could be adapted without changing its overall architecture:

  • nuclei could become cells surviving a treatment;
  • discrete states could show molecules binding or changing conformation;
  • a grid and summary could reveal repeated samples from a distribution;
  • individuals could move between states in a population model; or
  • marks could represent items moving through the steps of an algorithm.

The strongest use of interactivity is not simply to make a figure move. It is to let learners change something meaningful and immediately see the consequences in representations that support the intended explanation.