observablehq / plot

A concise API for exploratory data visualization implementing a layered grammar of graphics
https://observablehq.com/plot/
ISC License
4.36k stars 176 forks source link

flamegraph transform? #2010

Open Fil opened 7 months ago

Fil commented 7 months ago

flamegraph

Here's a starting point; I'll make a notebook later. We have to change this from an ad hoc function to a proper transform (facet-aware etc).

{
  const positions = new Map();

  return (d) => {
    const p = (!positions.has(d.YEAR) && positions.set(d.YEAR, []), positions.get(d.YEAR));
    const start = days(d["AFSDATE"]);
    const end = days(d["AFEDATE"]);
    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;
  };
}