Skip to content

Instantly share code, notes, and snippets.

@srenatus
Created March 5, 2024 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srenatus/e7e01872206cee1eaf064b95bbf110ce to your computer and use it in GitHub Desktop.
Save srenatus/e7e01872206cee1eaf064b95bbf110ce to your computer and use it in GitHub Desktop.
const events = await db.sql`select
    created_at,
    last_updated_at,
  from events
`;
const stackPeriod = () => {
  const p = new Array();
  return (e) => {
    const start = e.created_at;
    const end = e.last_updated_at;
    let height = 0;
    while (
      p.some(
        (p) =>
          p.height === height &&
          ((p.start <= start && start <= p.end) ||
            (p.start <= end && end <= p.end) ||
            (start <= p.start && p.start <= end) ||
            (start <= p.end && p.end <= end)),
      )
    )
      height++;

    p.push({ start, end, height });
    return height;
  };
};
${resize((width) => Plot.plot({ width, height: 240, x: {interval: d3.utcDay, grid: true, axis: "top", round: true}, y: {axis: null}, marks: [ Plot.barX(events, { y: stackPeriod(), x1: "created_at", x2: "last_updated_at", inset: 0.5, tip: true, }), ] }))}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment