Skip to content

Calendar of Pies ​

The calendar of June 2022, whose cells are pies of three activities. Each pie is a chart nested in its cell, in polar coordinates, and a full turn: its angle is independent of the other cells. After G2.

js
// Three activities per day of June 2022, with the random values made repeatable.
const days = ["Sun.", "Mon.", "Tues.", "Wed.", "Thur.", "Fri.", "Sat."];
const random = d3.randomUniform.source(d3.randomLcg(0.42))();
const week = (date) => Math.ceil((date.getDate() + new Date(2022, 5, 1).getDay()) / 7);
const calendar = Array.from({length: 30}, (_, i) => {
  const date = new Date(2022, 5, i + 1);
  return ["Eat", "Play", "Sleep"].map((activity) => ({
    activity,
    value: random(),
    week: `${week(date)}`,
    day: days[date.getDay()],
  }));
}).flat();

// The cells of the calendar that have days: one row per (day, week), with its rows of data.
const cells = d3
  .groups(
    calendar,
    (d) => d.day,
    (d) => d.week,
  )
  .flatMap(([day, weeks]) => weeks.map(([week, rows]) => ({day, week, rows})));

const renderer = cm.svg({height: 500});
const root = cm.flow().renderer(renderer);

root
  .data(cells)
  .transform(cm.encode, {x: "day", y: "week"})
  .transform(cm.spreadX, {domain: days})
  .transform(cm.spreadY)
  .transform(cm.plane, {width: "auto"})
  .append(cm.g)
  .data((cell) => cell.rows)
  .transform(cm.encode, {x: "value", fill: "activity"})
  .transform(cm.stackX)
  .transform(cm.polar, {resolve: {x: "independent"}})
  .add(cm.arc, {stroke: "white"});

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