我解决了这个方法,它由shouldLogScale var 控制:
nv.addGraph(() => {
this.chart = nv.models.multiBarChart()
...
.y(d => {
const y = parseFloat(d.y);
return this.shouldLogScale ? Math.log10(y <= 0 ? 1 : y) : y;
});
if(this.shouldLogScale) {
this.logScale();
} else {
this.chart.yAxis.tickFormat(d3.format(`,.${this.precision}f`));
}
...
});
初始化图表时:我修改y 值,强制y (chart.forceY) 开始和结束值(将它们映射到它们的log10 range),类似地我设置chart.yAxis.tickValues。最后,我将刻度 (chart.yAxis.tickValues) 映射到它们的原始值。只要确保遵守domain的日志功能即可。
logScale方法:
private logScale() {
this.chart.forceY([1, 180]
.map(v => Math.log10(v)));
this.chart.yAxis.tickValues([1, 3, 5, 10, 20, 50, 100, 180]
.map(v => Math.log10(v)));
this.chart.yAxis.tickFormat(d => Math.pow(10, d).toFixed(this.precision));
}
也许我可以使用 y 域和范围值的映射来避免在 logScale 方法中使用 log 和 pow 函数。