Skip to content

Three Axes ​

Seattle's monthly means of the high temperature, the precipitation and the wind, each a line with a scale of its own name, branched from the weather and grouped by month together. The temperature's axis is on the left; the precipitation's and the wind's are on the right, stacked outward in order, each the color of its line. After G2.

js
const seattle = await d3.csv("/data/seattle-weather.csv", d3.autoType);
const month = (d) => new Date(Date.UTC(2000, d.date.getUTCMonth()));

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

root
  .data(seattle)
  .branch(
    (flow) =>
      flow.transform(cm.encode, {
        x: month,
        y: {value: "temp_max", scale: "temperature"},
        stroke: () => "Temperature",
      }),
    (flow) =>
      flow.transform(cm.encode, {
        x: month,
        y: {value: "precipitation", scale: "precipitation"},
        stroke: () => "Precipitation",
      }),
    (flow) => flow.transform(cm.encode, {x: month, y: {value: "wind", scale: "wind"}, stroke: () => "Wind"}),
  )
  .transform(cm.groupX, {y: "mean"})
  .transform(cm.plane, {
    x: {label: null, tickFormat: d3.utcFormat("%b")},
    temperature: {label: "Temperature (°C)", color: "tomato"},
    precipitation: {label: "Precipitation (in)", axis: "right", color: "steelblue"},
    wind: {label: "Wind (m/s)", axis: "right", color: "seagreen"},
    stroke: {
      legend: "right",
      domain: ["Temperature", "Precipitation", "Wind"],
      range: ["tomato", "steelblue", "seagreen"],
    },
  })
  .add(cm.lineY, {strokeWidth: 2});

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