plot icon indicating copy to clipboard operation
plot copied to clipboard

Add a "gap" option in the stack transform?

Open Fil opened this issue 3 years ago • 0 comments

Inspiration from https://twitter.com/GRSturge/status/1499000981870288914 : the ribbons are separated by gaps of constant height.

I can reproduce without touching Plot's source code with this plugin:

function gapStackY({ y, gap = 0, ...options }) {
  let y1, y2;
  const ygap = (d, i, e) => {
    const v = y(d, i, e);
    return v > 0 ? v + 3 * gap : 0;
  };
  ({ y1, y2, ...options } = Plot.stackY({ y: ygap, ...options }));
  const { transform: t1 } = y1;
  const { transform: t2 } = y2;
  y1.transform = () => {
    const y1 = t1();
    const y2 = t2();
    return y1.map((d, i) => d + (y2[i] > y1[i]) * gap);
  };
  y2.transform = () => {
    const y1 = t1();
    const y2 = t2();
    return y2.map((d, i) => d - (y2[i] > y1[i]) * gap);
  };
  return { y1, y2, ...options };
}

It's working fine for this use case, even if some options are not fully working (like, y MUST be a function), and I haven't tested with negative values of y. I will research these if you agree it makes sense as an official option.

example of using this plugin

Fil avatar Mar 11 '22 10:03 Fil