对数尺度
¥Logarithmic scales
对数(“log”)尺度与 线性比例尺 类似,不同之处在于,在计算输出范围值之前,会对输入域值应用对数变换。到范围值 y 的映射可以表示为域值 x 的函数:y = m log(x) + b。
¥Logarithmic (“log”) scales are like linear scales except that a logarithmic transform is applied to the input domain value before the output range value is computed. The mapping to the range value y can be expressed as a function of the domain value x: y = m log(x) + b.
CAUTION
由于 log(0) = -∞,对数尺度域必须为严格正数或严格负数;域不得包含或跨越零。具有正值域的对数刻度对正值有明确定义的行为,具有负值域的对数刻度对负值有明确定义的行为。(对于负值域,输入和输出值会隐式乘以 -1。)如果将负值传递给具有正域的对数刻度,或反之,则刻度的行为未定义。
¥As log(0) = -∞, a log scale domain must be strictly-positive or strictly-negative; the domain must not include or cross zero. A log scale with a positive domain has a well-defined behavior for positive values, and a log scale with a negative domain has a well-defined behavior for negative values. (For a negative domain, input and output values are implicitly multiplied by -1.) The behavior of the scale is undefined if you pass a negative value to a log scale with a positive domain or vice versa.
scaleLog(domain, range)
示例 · 源代码 · 使用指定的 domain 和 range,禁用 base、default、interpolator 和 clamping,构造一个新的对数比例尺。
¥Examples · Source · Constructs a new log scale with the specified domain and range, the base 10, the default interpolator and clamping disabled.
const x = d3.scaleLog([1, 10], [0, 960]);
如果未指定域,则默认为 [1, 10]。如果未指定范围,则默认为 [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) {#log_base}
示例 · 源代码 · 如果指定了 base,则将此对数比例的底数设置为指定的值。
¥Examples · Source · If base is specified, sets the base for this logarithmic scale to the specified value.
const x = d3.scaleLog([1, 1024], [0, 960]).base(2);
如果未指定基数,则返回当前基数,默认为 10。请注意,由于对数变换的性质,底数不会影响比例尺的编码;它只会影响选择哪个 ticks。
¥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) {#log_ticks}
示例 · 源代码 · 与 linear.ticks 类似,但针对对数比例尺进行了定制。
¥Examples · Source · Like linear.ticks, but customized for a log scale.
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 为整数,则返回的刻度在每个底数的整数次幂内均匀分布;否则,返回每个基数幂的一个刻度。返回的刻度保证在域的范围内。如果 domain 中的数量级大于 count,则每个幂最多返回一个 tick。否则,刻度值未过滤,但请注意,你可以使用 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) {#log_tickFormat}
示例 · 源代码 · 与 linear.tickFormat 类似,但针对对数比例尺进行了定制。指定的计数通常与用于生成 刻度值 的计数具有相同的值。
¥Examples · Source · Like linear.tickFormat, but customized for a log scale. The specified count typically has the same value as the count that is used to generate the tick values.
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", …]
如果刻度过多,格式化程序可能会为某些刻度标签返回空字符串;然而,请注意,刻度仍然会显示,以准确传达对数变换。要禁用过滤,请指定计数为 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() {#log_nice}
示例 · 源代码 · 与 linear.nice 类似,但 except 将定义域扩展为 base 的整数幂。
¥Examples · Source · Like linear.nice, except extends the domain to integer powers of base.
const x = d3.scaleLog([0.201479, 0.996679], [0, 960]).nice();
x.domain(); // [0.1, 1]
如果域包含两个以上的值,则对域进行细化处理仅影响第一个和最后一个值。对比例尺进行细化只会修改当前域;它不会自动优化随后使用 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.