Skip to content

Connected Scatterplot ​

Miles driven against the price of gas, one point a year, in order. A branch splits the flow in two that share one plane: the line and points, and every fifth year labeled on the side its data gives, over a white halo. After Observable Plot, itself after Hannah Fairfield.

js
const driving = await d3.csv("/data/driving.csv", d3.autoType);
const fifth = driving.filter((d) => d.year % 5 === 0);

const renderer = cm.svg({width: 640, height: 480});
const root = cm.flow().renderer(renderer);

const [line, labels] = root
  .branch(
    (flow) => flow.data(driving).transform(cm.encode, {x: "miles", y: "gas"}),
    (flow) => flow.data(fifth).transform(cm.encode, {x: "miles", y: "gas", side: "side"}),
  )
  .transform(cm.plane, {
    x: {label: "Miles driven (per person-year)", grid: true},
    y: {label: "Cost of gasoline ($ per gallon)", grid: true},
  });
line
  .add(cm.line, {stroke: "currentColor", strokeWidth: 2})
  .add(cm.circle, {r: 3, fill: "white", stroke: "currentColor", strokeWidth: 1.5});
labels.add(cm.label, {text: "year", position: "side", padding: 6, halo: true, haloWidth: 4});

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