Skip to content

Histogram ​

The weights of Olympic athletes, binned on x with cm.binX, counted per sex and stacked, with a rule at 0 drawn by cm.annotate. Like Plot's rect, the bars are inset by 0.5px with cm.insetX. After Observable Plot.

js
const athletes = await d3.csv("/data/athletes.csv", d3.autoType);

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

root
  .data(athletes)
  .transform(cm.encode, {x: "weight", fill: "sex"})
  .transform(cm.binX, {y: "count"})
  .transform(cm.stackY)
  .transform(cm.plane, {y: {grid: true}})
  .add(cm.rect, cm.insetX(0.5))
  .add(cm.annotate(cm.link), {y: 0});

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