Skip to content

对数尺度

🌐 Logarithmic scales

对数(“log”)刻度类似于线性刻度,只不过在计算输出范围值之前,对输入域值应用了对数变换。映射到范围值 y 可以表示为域值 x 的函数:y = m log(x) + b

小心

由于 log(0) = -∞,对数刻度的取值范围必须严格为正或严格为负;取值范围不能包含或经过零。正取值范围的对数刻度对于正值具有明确的行为,而负取值范围的对数刻度对于负值具有明确的行为。(对于负取值范围,输入和输出值会隐式地乘以 -1。)如果你向正取值范围的对数刻度传递负值,或向负取值范围的对数刻度传递正值,其行为是未定义的。

scaleLog(domain, range)

示例 · 来源 · 构建一个新的对数刻度,具有指定的 domainrangebase 为 10,默认 插值器 并且 clamping 被禁用。

js
const x = d3.scaleLog([1, 10], [0, 960]);

如果未指定 domain,则默认为 [1, 10]。如果未指定 range,则默认为 [0, 1]。

🌐 If domain is not specified, it defaults to [1, 10]. If range is not specified, it defaults to [0, 1].

log.base(base)

示例 · 来源 · 如果指定了base,则将此对数刻度的底数设置为指定的值。

js
const x = d3.scaleLog([1, 1024], [0, 960]).base(2);

如果未指定 base,则返回当前基数,默认值为 10。请注意,由于对数变换的性质,基数不会影响刻度的编码;它只会影响选择哪些 刻度

🌐 If base is not specified, returns the current base, which defaults to 10. Note that due to the nature of a logarithmic transform, the base does not affect the encoding of the scale; it only affects which ticks are chosen.

log.ticks(count)

示例 · 来源 · 类似 linear.ticks,但为对数刻度自定义。

js
const x = d3.scaleLog([1, 100], [0, 960]);
const T = x.ticks(); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

如果base是整数,则返回的刻度在base的每个整数次幂内均匀分布;否则,每个次幂仅返回一个刻度。返回的刻度保证在域的范围内。如果domain的数量级超过count,则每个次幂最多返回一个刻度。否则,刻度值不加以过滤,但请注意,你可以使用log.tickFormat来过滤刻度标签的显示。如果未指定count,则默认值为10。

🌐 If the base is an integer, the returned ticks are uniformly spaced within each integer power of base; otherwise, one tick per power of base is returned. The returned ticks are guaranteed to be within the extent of the domain. If the orders of magnitude in the domain is greater than count, then at most one tick per power is returned. Otherwise, the tick values are unfiltered, but note that you can use log.tickFormat to filter the display of tick labels. If count is not specified, it defaults to 10.

log.tickFormat(count, specifier)

示例 · 来源 · 类似 linear.tickFormat,但针对对数刻度进行了自定义。指定的 count 通常与用于生成 刻度值 的计数相同。

js
const x = d3.scaleLog([1, 100], [0, 960]);
const T = x.ticks(); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, …]
const f = x.tickFormat();
T.map(f); // ["1", "2", "3", "4", "5", "", "", "", "", "10", …]

如果刻度太多,格式化程序可能会返回某些刻度标签的空字符串;但是,请注意,刻度仍然会显示以准确传达对数变换。要禁用过滤,请指定 count 为 Infinity。

🌐 If there are too many ticks, the formatter may return the empty string for some of the tick labels; however, note that the ticks are still shown to convey the logarithmic transform accurately. To disable filtering, specify a count of Infinity.

在指定计数时,你还可以提供格式 指定符 或格式函数。例如,要获得一个将显示 20 个货币刻度的刻度格式化器,可以使用 log.tickFormat(20, "$,f")。如果指定符没有定义精度,则精度将由刻度自动设置,从而返回适当的格式。这提供了一种便捷的方式来指定精度由刻度自动设置的格式。

🌐 When specifying a count, you may also provide a format specifier or format function. For example, to get a tick formatter that will display 20 ticks of a currency, say log.tickFormat(20, "$,f"). If the specifier does not have a defined precision, the precision will be set automatically by the scale, returning the appropriate format. This provides a convenient way of specifying a format whose precision will be automatically set by the scale.

log.nice()

示例 · 来源 · 类似 linear.nice,但将定义域扩展到 base 的整数次幂。

js
const x = d3.scaleLog([0.201479, 0.996679], [0, 960]).nice();
x.domain(); // [0.1, 1]

如果域有两个以上的值,调整域的边界(nicing)仅影响第一个和值最后一个值。调整比例的边界仅修改当前域;它不会自动调整随后使用 log.domain 设置的域。如果需要,设置新域后必须重新调整比例的边界。

🌐 If the domain has more than two values, nicing the domain only affects the first and last value. Nicing a scale only modifies the current domain; it does not automatically nice domains that are subsequently set using log.domain. You must re-nice the scale after setting the new domain, if desired.