用最小,最大和离散的“步骤”创建对数刻度

发布于 2025-01-18 16:49:21 字数 877 浏览 4 评论 0 原文

我正在为地图生成颜色的秤,其中各种数字范围与颜色相对应。在下面的刻度中, 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 对数比例的正确数字?

I'm building a scale for generating colors for a map, where various ranges of numbers correspond to colors. In the scale below, the a, b, and c values (along with the min and max) form "steps" or "buckets" corresponding to colors. Anything between -1 and min will be colored "#ffeda0 in this example, and anything between min and a will be colored differently:

  return [
    "#e7e9e9",
    -1,
    "#ffeda0",
    min,
    "#fed976",
    a
    "#feb24c",
    b
    "#fd8d3c",
    c
    "#fc4e2a",
    max
    "#e31a1c"
  ];

This scale will always start an a minimum value, called min, and will end at a maximum value, max. I've currently got a simple linear scale like this:

  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"
  ];

How could one write a function that, given a minimum, maximum, and the number of steps in a scale (in this case three) output the correct numbers for a logarithmic scale?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神魇的王 2025-01-25 16:49:21

对数刻度的对数是线性的。因此:

const min = Math.min(data);
const max = Math.max(data);

const logmin = Math.log(min);
const logmax = Math.log(max);

const logrange = logmax - logmin;
const logstep = logrange / 4;

现在,线性刻度具有对

value = min + n * step;

数刻度的位置:

value = Math.exp(logmin + n * logstep);

仅当两个 min max 均为正时,这才能正常数字和对数刻度的数字和bcause,每个值都有一个恒定的正因子。您可以通过调整符号来为负面 min max 进行此工作。

您可以在其他基础上进行计算,例如10:

const logmin = Math.log10(min);
const logmax = Math.log10(max);

value = Math.pow(10, logmin + n * logstep);

另外,您可以通过采用最低商的商的 n th root,从一个步骤到下一个步骤找到共同因素。 :

const min = Math.min(data);
const max = Math.max(data);

const fact = Math.pow(max / min, 1.0 / 4.0);

然后通过重复乘法找到值:

value = prev_value * fact

value = xmin * Math.pow(fact, n);

两种方法都可能产生“不整合”的数字,即使量表应该具有良好的圆形数字,因为对数和 n -th roots可能会产生不准确的浮点数结果。如果选择圆形因子并调整最小/最大值,您可能会获得更好的结果。

A logarithmic scale is linear in its logarithms. So:

const min = Math.min(data);
const max = Math.max(data);

const logmin = Math.log(min);
const logmax = Math.log(max);

const logrange = logmax - logmin;
const logstep = logrange / 4;

Now where the linear scale has

value = min + n * step;

the logarithmic scale has:

value = Math.exp(logmin + n * logstep);

This will work only if both min and max 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 negative min and max by adjusting the signs.

You can do the calculations in other bases, for example 10:

const logmin = Math.log10(min);
const logmax = Math.log10(max);

value = Math.pow(10, logmin + n * logstep);

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:

const min = Math.min(data);
const max = Math.max(data);

const fact = Math.pow(max / min, 1.0 / 4.0);

The values are then found by repeated multiplication:

value = prev_value * fact

or

value = xmin * Math.pow(fact, n);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文