Skip to content

刻度

¥Ticks

从连续间隔中生成代表值。

¥Generate representative values from a continuous interval.

ticks(start, stop, count)

示例 · 源代码 · 返回一个数组,其中包含起始值和终止值(含)之间约 count + 1 个均匀分布、四舍五入的值。每个值都是 10 的幂乘以 1、2 或 5。

¥Examples · Source · Returns an array of approximately count + 1 uniformly-spaced, nicely-rounded values between start and stop (inclusive). Each value is a power of ten multiplied by 1, 2 or 5.

js
d3.ticks(1, 9, 5) // [2, 4, 6, 8]
js
d3.ticks(1, 9, 20) // [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9]

刻度是包含性的,这意味着它们可以包含指定的起始值和终止值,当(且仅当)这些值是精确的、四舍五入的值,与推断出的 step 一致时。更正式地说,每个返回的刻度 t 都满足 start ≤ t 且 t ≤ stop。

¥Ticks are inclusive in the sense that they may include the specified start and stop values if (and only if) they are exact, nicely-rounded values consistent with the inferred step. More formally, each returned tick t satisfies startt and tstop.

tickIncrement(start, stop, count)

源代码 · 与 d3.tickStep 类似,但要求起始值始终小于或等于终止值,并且如果给定起始值、终止值和计数的刻度步长小于 1,则返回负的刻度步长。

¥Source · Like d3.tickStep, except requires that start is always less than or equal to stop, and if the tick step for the given start, stop and count would be less than one, returns the negative inverse tick step instead.

js
d3.tickIncrement(1, 9, 5) // 2
js
d3.tickIncrement(1, 9, 20) // -2, meaning a tick step 0.5

此方法始终保证返回整数,并由 d3.ticks 使用,以确保返回的 tick 值尽可能精确地以 IEEE 754 浮点数表示。

¥This method is always guaranteed to return an integer, and is used by d3.ticks to guarantee that the returned tick values are represented as precisely as possible in IEEE 754 floating point.

tickStep(start, stop, count)

源代码 · 如果将相同的参数传递给 d3.ticks,则返回相邻刻度值之间的差值:一个四舍五入的值,它是 10 的幂乘以 1、2 或 5。

¥Source · Returns the difference between adjacent tick values if the same arguments were passed to d3.ticks: a nicely-rounded value that is a power of ten multiplied by 1, 2 or 5.

js
d3.tickStep(1, 9, 5) // 2

如果 stop 小于 start,则可能返回负的 tick step,以指示 tick 的递减顺序。

¥If stop is less than start, may return a negative tick step to indicate descending ticks.

js
d3.tickStep(9, 1, 5) // -2

请注意,由于 IEEE 754 浮点精度有限,返回值可能不是精确的小数;使用 d3-format 格式化数字以供用户使用。

¥Note that due to the limited precision of IEEE 754 floating point, the returned value may not be exact decimals; use d3-format to format numbers for human consumption.

nice(start, stop, count)

源代码 · 返回一个覆盖给定区间 [start, stop] 的新区间 [niceStart, niceStop],其中 niceStart 和 niceStop 保证与相应的 刻度步长 对齐。

¥Source · Returns a new interval [niceStart, niceStop] covering the given interval [start, stop] and where niceStart and niceStop are guaranteed to align with the corresponding tick step.

js
d3.nice(1, 9, 5) // [0, 10]

d3.tickIncrement 类似,这要求起始值小于或等于终止值。

¥Like d3.tickIncrement, this requires that start is less than or equal to stop.

range(start, stop, step)

示例 · 源代码 · 返回一个包含等差数列的数组,类似于 Python 内置的 range。此方法通常用于迭代一系列均匀分布的数值,例如数组的索引或线性刻度的刻度。(另请参阅 d3.ticks 以获得精确的数值。)

¥Examples · Source · Returns an array containing an arithmetic progression, similar to the Python built-in range. This method is often used to iterate over a sequence of uniformly-spaced numeric values, such as the indexes of an array or the ticks of a linear scale. (See also d3.ticks for nicely-rounded values.)

js
d3.range(6) // [0, 1, 2, 3, 4, 5]

如果省略 step,则默认为 1。如果省略 start,则默认为 0。停止值不包含任何值;它不包含在结果中。如果 step 为正数,则最后一个元素是 start + i * step 中小于 stop 的最大值;如果 step 为负数,则最后一个元素是 start + i * step 中大于 stop 的最小值。

¥If step is omitted, it defaults to 1. If start is omitted, it defaults to 0. The stop value is exclusive; it is not included in the result. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop.

js
d3.range(5, -1, -1) // [5, 4, 3, 2, 1, 0]

如果返回的数组包含无限数量的值,则返回一个空范围。

¥If the returned array would contain an infinite number of values, an empty range is returned.

js
d3.range(Infinity) // []

参数不必为整数;但是,如果是这样,结果会更可预测。返回数组中的值定义为 start + i * step,其中 i 是一个从 0 到 1 的整数,减去返回数组中元素的总数。

¥The arguments are not required to be integers; however, the results are more predictable if they are. The values in the returned array are defined as start + i * step, where i is an integer from zero to one minus the total number of elements in the returned array.

js
d3.range(0, 1, 0.2) // [0, 0.2, 0.4, 0.6000000000000001, 0.8]

此行为源于 IEEE 754 双精度浮点数,其定义为 0.2 * 3 = 0.6000000000000001。Use d3-format to format numbers for human consumption with appropriate rounding;另请参阅 d3-scale 中的 linear.tickFormat。同样,如果返回的数组应该具有特定长度,请考虑在整数范围内使用 array.map

¥This behavior is due to IEEE 754 double-precision floating point, which defines 0.2 * 3 = 0.6000000000000001. Use d3-format to format numbers for human consumption with appropriate rounding; see also linear.tickFormat in d3-scale. Likewise, if the returned array should have a specific length, consider using array.map on an integer range.

js
d3.range(0, 1, 1 / 49) // 👎 returns 50 elements!
js
d3.range(49).map((d) => d / 49) // 👍 returns 49 elements