gpt4 book ai didi

javascript - 很难理解 d3.range.js 和 d3.scale.linear

转载 作者:行者123 更新时间:2023-12-03 12:01:08 25 4
gpt4 key购买 nike

我知道如何使用range.jsd3.scale.linear ,还知道域和值域。

但是当我阅读range.js和linear.js中的代码时,我很困惑,为什么它看起来如此复杂和神秘:比如 Linear.js 中的代码:

step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)),
err = m / span * step;
// Filter ticks to get closer to the desired count.
if (err <= .15) step *= 10;
else if (err <= .35) step *= 5;
else if (err <= .75) step *= 2;

一般情况下,我使用“ax + b = y”来实现该功能,例如:

domain(inputMin, inputMax); 
stick = 4;

a = (inputMax-inputMin)/stick;
for(i = 0; i<a; i++)
{
arr[] = i*a+inputMin;
}

那么为什么 d3 使用 Math.log (Math.pow...) 来获取“步骤”?

这段代码的含义和作用是什么?

感谢您提供的任何帮助。

最佳答案

上周我必须为这个算法编写单元测试。这是我的解释:

// Approximate the step to the closest power of ten by transforming to the
// commom log scale, truncating the mantissa, and transforming back.
step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)),

// Calculate how closely the step covers the span of the domain.
err = (m * step) / span;

// Given the coverage of the current step against the domain span, multiply
// (if necessary) by a constant to get closer to the desired tick count.
if (err <= 0.15) step *= 10;
else if (err <= 0.35) step *= 5;
else if (err <= 0.75) step *= 2;

关于javascript - 很难理解 d3.range.js 和 d3.scale.linear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25409462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com