用最小,最大和离散的“步骤”创建对数刻度
我正在为地图生成颜色的秤,其中各种数字范围与颜色相对应。在下面的刻度中, a
, b
和 c
值(以及min and Max)形式“步骤”或“ buckets”对应于颜色。 -1
和之间的任何内容
将被彩色“#ffeda0
在此示例中,以及 min
和<之间的任何内容 > a 将以不同的颜色颜色:
return [
"#e7e9e9",
-1,
"#ffeda0",
min,
"#fed976",
a
"#feb24c",
b
"#fd8d3c",
c
"#fc4e2a",
max
"#e31a1c"
];
此量表将始终启动一个最小值,称为最小值,并且最大值我当前有一个简单的线性比例。
const min = Math.min(data);
const range = Math.max(data) - min;
const step = range / 4;
return [
"#e7e9e9",
-1,
"#ffeda0",
min,
"#fed976",
min + step,
"#feb24c",
min + step * 2,
"#fd8d3c",
min + step * 3,
"#fc4e2a",
max,
"#e31a1c"
];
代码 编写一个函数,给定标度中的最小,最大和步骤数(在本例中为三个),输出了A 对数比例的正确数字?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对数刻度的对数是线性的。因此:
现在,线性刻度具有对
数刻度的位置:
仅当两个
min
和max
均为正时,这才能正常数字和对数刻度的数字和bcause,每个值都有一个恒定的正因子。您可以通过调整符号来为负面min
和max
进行此工作。您可以在其他基础上进行计算,例如10:
另外,您可以通过采用最低商的商的 n th root,从一个步骤到下一个步骤找到共同因素。 :
然后通过重复乘法找到值:
或
两种方法都可能产生“不整合”的数字,即使量表应该具有良好的圆形数字,因为对数和 n -th roots可能会产生不准确的浮点数结果。如果选择圆形因子并调整最小/最大值,您可能会获得更好的结果。
A logarithmic scale is linear in its logarithms. So:
Now where the linear scale has
the logarithmic scale has:
This will work only if both
min
andmax
are positive, because you cannot take the logarithm of zero or a negative number and bcause in logarithmic scale, each value has a constant positive factor to the previous one. You can make this work for negativemin
andmax
by adjusting the signs.You can do the calculations in other bases, for example 10:
Alternatively, you can find the common factor from one step to the next by taking the n-th root of the quotient of the min and max values:
The values are then found by repeated multiplication:
or
Both methods might yield "untidy" numbers even if the scale should have nice round numbers, because the logarithms and n-th roots could produce inaccurate floating-point results. You might get better results if you pick a round factor and adjust your min/max values.