How to access an aggregated value in Plot?

Let’s say I want to show the cumsum value in the title attribute in a plot that uses Plot.map, like this one:

Is there something I can put here?

Plot.lineY(
  values,
  Plot.map(
    { y: "cumsum" },
    { 
        y: values,
        title: d => `The cumsum value is ${d.????}`
    },
  )
).plot();

sure! With two consecutive “maps”.

Plot.dot(
  values,
  Plot.map(
    { title: (cs) => Array.from(cs, (s) => `The cumsum value is ${s}`) },
    Plot.map(
      { title: "cumsum", y: "cumsum" },
      {
        x: d3.range(values.length),
        y: values,
        title: values
      }
    )
  )
).plot()
1 Like

Thanks!

I guess an alternative is to do the sum myself (or use d3.cumsum):

Plot.dot(
  values,
  Plot.map(
    {
      y: "cumsum",
      title: (v) => {
        let cumsum = 0;
        return v.map((d) => `The cumsum value is ${(cumsum += d)}`);
      },
    },
    {
      x: d3.range(values.length),
      y: values,
      title: values,
    }
  )
).plot();

1 Like