【发布时间】:2022-12-04 08:06:31
【问题描述】:
对于每个 Stacked Bar 元素,我计算其百分比
const valuePercent = attribute => ({payload}) => {
const keys = getKeys(chartData);
const total = keys.reduce((acc, curr) => {
return acc + payload[curr].count;
}, 0);
const ratio = total > 0 ? payload[attribute].count / total : 0;
return `${(ratio * 100).toFixed(0)}%`;
};
但是当我在样式中替换这个值时,它不起作用。可能是什么问题呢?
return keys.map((item, index) => ( <
Bar key = {
index
}
dataKey = {
`${item}.count`
}
stackId = 'a'
style = {
{
fill: '#0452D7',
fillOpacity: valuePercent(item),
}
}
/>
));
【问题讨论】:
-
valuePercent是高阶函数,您没有调用返回的函数。你需要像valuePercent(item)({ payload: 'data'})这样的东西。
标签: javascript reactjs recharts