Skip to content

Can I create filtered percentage stacked bars using Plot? #2198

Answered by Fil
mkfreeman asked this question in Q&A
Discussion options

You must be logged in to vote

You need a custom offset function that takes as basis the maximum value from the whole series and filters out the meanies:

Plot.plot({
  marks: [
    Plot.barY(tidy, {
      offset: function (facets, Y1, Y2, Z) {
        for (const series of facets) {
          for (const I of series) {
            const basis = d3.max(I, (i) => Y2[i]);
            for (const i of I)
              if (Z[i] === "<10" || Z[i] === "10-19") {
                Y1[i] = Y2[i] = NaN;
              } else {
                Y1[i] /= basis;
                Y2[i] /= basis;
              }
          }
        }
      },
      x: "state",
      y: "population",
      fill: "age",
      order: (d) => d.age === "<10" || d.a…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mkfreeman
Comment options

Answer selected by mkfreeman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants