Skip to content

蜱虫

🌐 Ticks

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

🌐 Generate representative values from a continuous interval.

ticks(start, stop, count)

示例 · 来源 · 返回一个数组,包含大约 count + 1 个在 startstop(包含)之间均匀间隔、且四舍五入良好的值。每个值都是 10 的幂乘以 1、2 或 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]

刻度是包容性的,这意味着它们可能会包括指定的起始和值停止,前提是(并且仅当)它们是与推断的步长一致的精确、整齐的值。更正式地说,每个返回的刻度t都满足startttstop

🌐 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,但要求 start 始终小于或等于 stop,并且如果给定的 startstopcount 的刻度步长小于一,则返回负倒数刻度步长。

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

这种方法总是保证返回一个整数,并且被 d3.ticks 使用,以确保返回的刻度值在 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)

Source · 如果将相同的参数传递给 d3.ticks,则返回相邻刻度值之间的差值:一个经过良好四舍五入的值,是十的幂乘以 1、2 或 5。

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

如果 stop 小于 start,可能会返回负的刻度步长以表示递减的刻度。

🌐 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)

来源 · 返回一个新的区间 [niceStart, niceStop],覆盖给定区间 [start, stop],并且 niceStartniceStop 保证与对应的 刻度步长 对齐。

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

d3.tickIncrement 一样,这要求 start 小于或等于 stop

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

range(start, stop, step)

示例 · 来源 · 返回一个包含算术级数的数组,类似于 Python 内置的 range。此方法通常用于迭代一系列均匀间隔的数值,例如数组的索引或线性尺度的刻度。(另请参见 d3.ticks 以获取取整的数值。)

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

如果省略了,则默认为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 是从零到一的整数减去返回数组中元素总数。

🌐 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。使用 d3-format 对数字进行格式化,以适当的舍入供人类阅读;另请参见 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