简体   繁体   English

对数刻度在 d3.js 中的工作原理

[英]How log scale works in d3.js

I am trying to understand why the equation doesn't give the desired result.我试图理解为什么方程没有给出预期的结果。

I am unable to decode how this returns 90.96?我无法解码这如何返回 90.96? Even with using the equation log y = m log x + log k I can't get the same result.即使使用等式log y = m log x + log k我也无法得到相同的结果。 using ∆log y divided by ∆log x to get m .使用 Δlog y 除以 Δlog x 得到m

const logScale = d3.scaleLog()
                   .domain([1, 1000])
                   .range([10, 100]);

console.log(logScale(500)); // returns 90.96

Log scales have the form y = m log(x) + b.对数刻度的形式为 y = m log(x) + b。 By default it's a base 10 log.默认情况下,它是以 10 为底的日志。 With your scale用你的秤

const logScale = d3.scaleLog()
  .domain([1, 1000])
  .range([10, 100]);

m = 30 and b = 10. So you can think of it as m = 30 和 b = 10。所以你可以把它想象成

function logScale(x) {
  return 30 * Math.log10(x) + 10;
}

Edit: explaining how I got the b = 10 and m = 30编辑:解释我如何得到 b = 10 和 m = 30

From the domain and range, we know that logScale(1) === 10 .从域和范围,我们知道logScale(1) === 10 We also know that Math.log10(1) === 0 .我们也知道Math.log10(1) === 0 Therefore, when x is 1 and y is 10, the equation y = m * log(x) + b becomes 10 = m * 0 + b, so we know that b is 10. Now we can plug another (x, y) pair in to get m.因此,当 x 为 1 且 y 为 10 时,方程 y = m * log(x) + b 变为 10 = m * 0 + b,所以我们知道 b 为 10。现在我们可以插入另一个 (x, y)配对以获取 m。 For example, we can choose the max values from the domain and range: 100 = m * log(1000) + 10. Thus, m = 90 / log(1000), which is 30.例如,我们可以从域和范围中选择最大值:100 = m * log(1000) + 10。因此,m = 90 / log(1000),即 30。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM