点尺度
¥Point scales
点比例尺是 带尺度 的一种变体,其带宽固定为零。点比例尺通常用于具有序数或分类维度的散点图。
¥Point scales are a variant of band scales with the bandwidth fixed to zero. Point scales are typically used for scatterplots with an ordinal or categorical dimension.
scalePoint(domain, range)
示例 · 源代码 · 使用指定的 domain 和 range,不使用 padding、rounding,并以 alignment 为中心,构造一个新的点比例尺。如果未指定域,则默认为空域。如果未指定范围,则默认为单位范围 [0, 1]。
¥Examples · Source · Constructs a new point scale with the specified domain and range, no padding, no rounding and center alignment. If domain is not specified, it defaults to the empty domain. If range is not specified, it defaults to the unit range [0, 1].
point(value) {#_point}
示例 · 源代码 · 给定输入 domain 中的值,返回输出 range 中的对应点。
¥Examples · Source · Given a value in the input domain, returns the corresponding point derived from the output range.
const x = d3.scalePoint(["a", "b", "c"], [0, 960]);
x("a"); // 0
x("b"); // 480
x("c"); // 960
x("d"); // undefined
如果给定值不在比例尺的域中,则返回 undefined。
¥If the given value is not in the scale’s domain, returns undefined.
point.domain(domain) {#point_domain}
示例 · 源代码 · 如果指定了域,则将域设置为指定的值数组。
¥Examples · Source · If domain is specified, sets the domain to the specified array of values.
const x = d3.scalePoint([0, 960]).domain(["a", "b", "c", "d", "e", "f"]);
域中的第一个元素将映射到第一个点,第二个域值映射到第二个点,依此类推。域值内部存储在 InternMap 中,从原始值到索引;然后使用生成的索引确定点。因此,点比例尺的值必须可强制转换为原始值,并且原始域值唯一地标识相应的点。如果未指定域,则此方法返回当前域。
¥The first element in domain will be mapped to the first point, the second domain value to the second point, and so on. Domain values are stored internally in an InternMap from primitive value to index; the resulting index is then used to determine the point. Thus, a point scale’s values must be coercible to a primitive value, and the primitive domain value uniquely identifies the corresponding point. If domain is not specified, this method returns the current domain.
point.range(range) {#point_range}
示例 · 源代码 · 如果指定了 range,则将比例尺的范围设置为指定的二元素数字数组并返回此比例尺。
¥Examples · Source · If range is specified, sets the scale’s range to the specified two-element array of numbers and returns this scale.
const x = d3.scalePoint().range([0, 960]);
如果给定数组中的元素不是数字,则它们将被强制转换为数字。如果未指定 range,则返回比例尺的当前范围,默认为 [0, 1]。
¥If the elements in the given array are not numbers, they will be coerced to numbers. If range is not specified, returns the scale’s current range, which defaults to [0, 1].
point.rangeRound(range) {#point_rangeRound}
示例 · 源代码 · 将比例尺的 range 设置为指定的二元素数字数组,同时启用 rounding;返回此比例尺。
¥Examples · Source · Sets the scale’s range to the specified two-element array of numbers while also enabling rounding; returns this scale.
const x = d3.scalePoint().rangeRound([0, 960]);
这是一个便捷的方法,相当于:
¥This is a convenience method equivalent to:
point.range(range).round(true)
舍入有时有助于避免抗锯齿伪影,但也要考虑 shape-rendering 的“crispEdges”样式。
¥Rounding is sometimes useful for avoiding antialiasing artifacts, though also consider the shape-rendering “crispEdges” styles.
point.round(round) {#point_round}
示例 · 源代码 · 如果指定了 round,则相应地启用或禁用舍入。
¥Examples · Source · If round is specified, enables or disables rounding accordingly.
const x = d3.scalePoint(["a", "b", "c"], [0, 960]).round(false);
如果未指定 round,则返回舍入是否启用。
¥If round is not specified, returns whether rounding is enabled.
x.round() // false
如果启用了舍入,则每个点的位置将为整数。舍入有时有助于避免抗锯齿伪影,但也要考虑 shape-rendering 的“crispEdges”样式。请注意,如果域的宽度不是范围基数的倍数,即使没有填充,也可能存在剩余的未使用空间!Use point.align to specify how the leftover space is distributed.
¥If rounding is enabled, the position of each point will be integers. Rounding is sometimes useful for avoiding antialiasing artifacts, though also consider the shape-rendering “crispEdges” styles. Note that if the width of the domain is not a multiple of the cardinality of the range, there may be leftover unused space, even without padding! Use point.align to specify how the leftover space is distributed.
point.padding(padding) {#point_padding}
示例 · 源代码 · 如果指定了 padding 参数,则将外部填充设置为指定的数字(该数字通常在 [0, 1] 范围内)。
¥Examples · Source · If padding is specified, sets the outer padding to the specified number which is typically in the range [0, 1].
const x = d3.scalePoint(["a", "b", "c"], [0, 960]).padding(0.1);
如果未指定 padding,则返回当前外边距,默认为 0。
¥If padding is not specified, returns the current outer padding which defaults to 0.
x.padding() // 0.1
外部填充指定在第一个点之前和最后一个点之后保留的空白空间量,以 step 的倍数表示。等同于 band.paddingOuter。
¥The outer padding specifies the amount of blank space, in terms of multiples of the step, to reserve before the first point and after the last point. Equivalent to band.paddingOuter.
point.align(align) {#point_align}
示例 · 源代码 · 如果指定了 align,则将对齐方式设置为指定的值,该值必须在 [0, 1] 范围内。
¥Examples · Source · If align is specified, sets the alignment to the specified value which must be in the range [0, 1].
const x = d3.scalePoint(["a", "b", "c"], [0, 960]).align(0.5);
如果未指定对齐方式,则返回当前对齐方式,默认为 0.5。
¥If align is not specified, returns the current alignment which defaults to 0.5.
x.align() // 0.5
对齐指定了范围内剩余未使用空间的分布方式。值 0.5 表示剩余空间应均匀分布在第一个点之前和最后一个点之后;即,点应位于范围内的中心。可以使用 0 或 1 值将点移到一侧,例如,将它们放置在与轴相邻的位置。
¥The alignment specifies how any leftover unused space in the range is distributed. A value of 0.5 indicates that the leftover space should be equally distributed before the first point and after the last point; i.e., the points should be centered within the range. A value of 0 or 1 may be used to shift the points to one side, say to position them adjacent to an axis.
point.bandwidth() {#point_bandwidth}
¥Examples · Source · Returns zero.
point.step() {#point_step}
¥Examples · Source · Returns the distance between adjacent points.
point.copy() {#point_copy}
示例 · 源代码 · 返回此比例尺的精确副本。更改此比例不会影响返回的比例,反之亦然。
¥Examples · Source · Returns an exact copy of this scale. Changes to this scale will not affect the returned scale, and vice versa.