Skip to content

Driven by a Signal ​

A scatterplot built once, whose data reads a signal: every 1.5s the signal takes a new count of points, and only what reads it runs again, the data, its scales and the marks, then the chart is drawn again, its points entering, moving and exiting with transitions, and its axes following.

js
const random = d3.randomLcg(0.42);
const points = Array.from({length: 40}, (_, i) => ({id: i, x: random() * 100, y: random() * 100}));
const n = cm.signal(10);

const renderer = cm.svg({width: 640, height: 400});
const root = cm.flow({transition: {duration: 600}}).renderer(renderer);

root
  .data(() => points.slice(0, n.value))
  .transform(cm.encode, {x: "x", y: "y", key: "id"})
  .transform(cm.plane)
  .add(cm.circle, {r: 5, fill: "steelblue", enter: {r: 0}, exit: {r: 0}});

root.render();
document.body.append(renderer.node());

// Sets a new count every 1.5s.
setInterval(() => (n.value = 5 + Math.floor(random() * 35)), 1500);