我有问题在d3中创建对数刻度.如果设置为线性,则刻度可以正常工作.
这有效:
var myLinScale = d3.scale.linear() .domain([0,100]) .range([50,1150]); console.log(myLinScale(71)); //output = 831
但是,这不起作用:
var myLogScale = d3.scale.log() .domain([0,1150]); console.log(myLogScale(71)); //output = NaN
对数刻度有什么问题?
解决方法
更改您的域名,使其不包含或交叉零:
var myLogScale = d3.scale.log() .domain([1e-6,100])//domain doesn't include zero now .range([50,1150]); console.log(myLogScale(71));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
在上面的演示中,我使用的是1e-6,即0.000001.
说明:
零的对数未定义(或未定义).例如,在基数10中,log(0)是数字x,因此提升到x的幂的10是零……当然,该数字不存在.然而,当我们从正面接近零时,极限是负无穷大.
在纯JavaScript中:
console.log("Log of 0 is: " + Math.log(0))
因此,在JavaScript中,log(0)是负无穷大,或负无穷大.
话虽如此,根据API:
a log scale must have either an exclusively-positive or exclusively-negative domain; the domain must not include or cross zero. (emphasis mine)