Skip to content

Percentage ticks with custom domain #2208

Answered by mbostock
spandl asked this question in Q&A
Discussion options

You must be logged in to vote

You can normalize the x-values by computing a proportion in a function:

Plot.plot({
    marginLeft: 300,
    x: {
     percent: true
    },
    marks: [
      Plot.ruleX([0]),
      Plot.barX(data, {
        x: 1,
        y: "Response_in_Text",
        fill: "slategrey",
        opacity: 0.25,
      }),
      Plot.barX(data, {
        x: (d) => d.count / 203,
        y: "Response_in_Text",
        sort: { y: "x", reverse: true },
      }),
      Plot.text(data, {
        x: 1,
        y: "Response_in_Text",
        text: (d) => `${((d.count / 203) * 100).toFixed(0)}%`,
        dx: -5,
        textAnchor: "end",
        fill: "black",
      }),
    ],
  })

Replies: 1 comment 1 reply

Comment options

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

Answer selected by spandl
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