【发布时间】:2021-08-13 11:39:06
【问题描述】:
在 y 轴上多次显示相同的刻度值(在本例中为 1M)。我认为问题不在于我如何使用yAxisTickFormat 格式化刻度。我做错了什么?
export const yAxisTickFormat = (d) => {
//Logic to reduce big numbers
const limits = [1000000, 10000, 1000];
const shorteners = ['M', 'K', 'K'];
for (let i in limits) {
if (d > limits[i]) {
return (d / limits[i]).toFixed() + shorteners[i];
}
}
return d;
};
const maxValue = storeData.dataset.reduce((maxAll, item) => {
return item.values.reduce((maxItem, val) =>
Math.max(maxItem, val.value), maxAll);
}, 0);
const yScale = d3.scaleLinear().range([-120, height - 200]);
yScale.domain([maxValue, 0]);
const yAxis = d3
.axisLeft()
.scale(yScale)
.tickFormat((d) => (d === 0 ? null : '£') + yAxisTickFormat(d))
.ticks(5)
.tickSize(-width, barMargin, 0)
.tickPadding(10);
【问题讨论】:
标签: javascript d3.js data-visualization bar-chart axis