Skip to content

点尺度

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

示例 · 来源 · 构建一个具有指定范围的新点刻度,不使用填充,不进行舍入,并居中对齐。如果未指定,则默认为空域。如果未指定范围,则默认为单位范围 [0, 1]。

point(value)

示例 · 来源 · 给定输入中的一个,返回从输出范围派生的对应点。

js
const x = d3.scalePoint(["a", "b", "c"], [0, 960]);
x("a"); // 0
x("b"); // 480
x("c"); // 960
x("d"); // undefined

如果给定的不在量表的定义域内,则返回未定义。

🌐 If the given value is not in the scale’s domain, returns undefined.

point.domain(domain)

abcdef

示例 · 来源 · 如果指定了 domain,则将域设置为指定的值数组。

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

示例 · 来源 · 如果指定了 range,将刻度的范围设置为指定的两个元素的数字数组,并返回该刻度。

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

示例 · 来源 · 将量表的范围设置为指定的两个元素的数字数组,同时启用四舍五入;返回此量表。

js
const x = d3.scalePoint().rangeRound([0, 960]);

这是一个便捷的方法,相当于:

🌐 This is a convenience method equivalent to:

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

abcdefghij

示例 · 来源 · 如果指定了round,则相应地启用或禁用四舍五入。

js
const x = d3.scalePoint(["a", "b", "c"], [0, 960]).round(false);

如果未指定 round,则返回是否启用四舍五入。

🌐 If round is not specified, returns whether rounding is enabled.

js
x.round() // false

如果启用了四舍五入,每个点的位置将是整数。四舍五入有时有助于避免抗锯齿伪影,但也可以考虑 shape-rendering 的“crispEdges”样式。请注意,如果域的宽度不是范围基数的倍数,即使没有填充,也可能会有多余未用的空间!使用 point.align 来指定多余空间的分布方式。

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

abcdefghij

示例 · 来源 · 如果指定了padding,则将外部填充设置为指定的数字,通常范围在[0, 1]之间。

js
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.

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

abcdefghij

示例 · 来源 · 如果指定了 align,则将对齐设置为指定的值,该值必须在 [0, 1] 范围内。

js
const x = d3.scalePoint(["a", "b", "c"], [0, 960]).align(0.5);

如果未指定 align,则返回当前对齐方式,默认值为 0.5。

🌐 If align is not specified, returns the current alignment which defaults to 0.5.

js
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.step()

abcdefghij

示例 · 来源 · 返回相邻点之间的距离。

point.copy()

示例 · 来源 · 返回此刻度的精确副本。对该刻度的更改不会影响返回的刻度,反之亦然。