-
I created a simple horizontal bar chart, that uses a custom domain (The total is supposed to be the total of respondents for a multiple answer survey, but this total is not accessible via the dataset). The result should be expressed in percentages, not in total answers. I came up with this version (which I like), but I need to create my own array of ticks. The version without calculating the tick array is below and showing why this isn't working (it returns odd percentage values, as the tick values are only expressed in percentages) The question: is there a better way to convert total values to percentages while using a custom x-domain? I was playing around with https://observablehq.com/@spandl/percentages-with-custom-domain Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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",
}),
],
}) |
Beta Was this translation helpful? Give feedback.
You can normalize the x-values by computing a proportion in a function: