I am using a text mark with a group transform to show proportions on top of my histogram bars. It’s working well but the proportion results have too many decimal places so take up too much horizontal space (see below)
Is there an easy way to round to only 2 decimal places? My current solution is to do it via DOM manipulation which is not ideal.
Here’s the code I’m using to get the result in the screenshot
Plot.plot({
x: {
label: "Years in development"
},
y: {
label: "Innovation Count"
},
marks: [
Plot.barY(
innovations,
groupByDevelopmentTime( null, { fill: "goldenrod" })
),
Plot.ruleX(
[getAverageDevelopmentTime(innovations)],
{stroke: "red"}
),
Plot.text(
[getAverageDevelopmentTime(innovations)],
{
x: getAverageDevelopmentTime(innovations),
y: 150,
text: [`Average: ${getAverageDevelopmentTime(innovations)}`],
dx: 30
}
),
Plot.text(
innovations,
groupByDevelopmentTime ( { "text": "proportion" }, { dy: -5 })
)
]
});
const getAverageDevelopmentTime = (innovations) =>
Math.round(
innovations.reduce(
(acc, curr) => acc + curr.DEVELOPMENT_TIME, 0)
/ innovations.length
)
const groupByDevelopmentTime = (outputs, options) => {
return Plot.groupX(
{ y: "count", ...outputs },
{ x: "DEVELOPMENT_TIME", ...options }
);
}