Skip to content

序数尺度

🌐 Ordinal scales

连续刻度不同,序数刻度具有离散的定义域和值域。例如,序数刻度可以将一组命名类别映射到一组颜色,或确定柱状图中柱子的水平位置。

🌐 Unlike continuous scales, ordinal scales have a discrete domain and range. For example, an ordinal scale might map a set of named categories to a set of colors, or determine the horizontal positions of columns in a column chart.

scaleOrdinal(domain, range)

示例 · 来源 · 构建一个具有指定值域的新序数比例尺。

js
const color = d3.scaleOrdinal(["a", "b", "c"], ["red", "green", "blue"]);

如果未指定 domain,则默认为空数组。如果未指定 range,则默认为空数组;序数比例尺在定义非空范围之前总是返回未定义。

🌐 If domain is not specified, it defaults to the empty array. If range is not specified, it defaults to the empty array; an ordinal scale always returns undefined until a non-empty range is defined.

序数()

🌐 ordinal(value)

示例 · 来源 · 给定输入中的,返回输出值域中对应的值。

js
color("a") // "red"

如果给定的 value 不在刻度的 domain 中,则返回 unknown value;或者,如果 unknown value 是 implicit(默认),那么 value 会被隐式地添加到域中,并且将范围中下一个可用的值分配给 value,使得对于相同的输入 value,刻度的这次及后续调用返回相同的输出值。

🌐 If the given value is not in the scale’s domain, returns the unknown value; or, if the unknown value is implicit (the default), then the value is implicitly added to the domain and the next-available value in the range is assigned to value, such that this and subsequent invocations of the scale given the same input value return the same output value.

ordinal.domain(domain)

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

js
const color = d3.scaleOrdinal(["red", "green", "blue"]).domain(["a", "b", "c"]);
color("a"); // "red"
color("b"); // "green"
color("c"); // "blue"

domain 中的第一个元素将映射到范围中的第一个元素,第二个域值映射到第二个范围值,依此类推。域值在内部存储在一个从原始值到索引的 InternMap 中;然后使用生成的索引从范围中检索值。因此,序数比例的值必须可以强制转换为原始值,并且原始域值唯一标识相应的范围值。

🌐 The first element in domain will be mapped to the first element in the range, the second domain value to the second range value, and so on. Domain values are stored internally in an InternMap from primitive value to index; the resulting index is then used to retrieve a value from the range. Thus, an ordinal scale’s values must be coercible to a primitive value, and the primitive domain value uniquely identifies the corresponding range value.

js
color.domain() // ["a", "b", "c"]

如果未指定 domain,此方法将返回当前域。

🌐 If domain is not specified, this method returns the current domain.

如果未知值隐式的(默认),则在序数刻度上设置域是可选的。在这种情况下,域将通过从范围中为传递给刻度的每个唯一值分配一个新值,从使用中隐式推断出来。

🌐 Setting the domain on an ordinal scale is optional if the unknown value is implicit (the default). In this case, the domain will be inferred implicitly from usage by assigning each unique value passed to the scale a new value from the range.

js
const color = d3.scaleOrdinal(["red", "green", "blue"]);
color("b"); // "red"
color("a"); // "green"
color("c"); // "blue"
color.domain(); // inferred ["b", "a", "c"]

小心

建议使用显式域以实现确定性行为;从使用情况推断域取决于顺序。

ordinal.range(range)

示例 · 来源 · 如果指定了range,将有序刻度的范围设置为指定的值数组。

js
const color = d3.scaleOrdinal().range(["red", "green", "blue"]);

域中的第一个元素将映射到range中的第一个元素,第二个域值映射到第二个区间值,依此类推。如果range中的元素少于域中的元素,比例尺将从range的开始处重新使用值。如果未指定range,此方法将返回当前的range。

🌐 The first element in the domain will be mapped to the first element in range, the second domain value to the second range value, and so on. If there are fewer elements in the range than in the domain, the scale will reuse values from the start of the range. If range is not specified, this method returns the current range.

ordinal.unknown(value)

示例 · 来源 · 如果指定了 value,则设置该比例对未知输入值的输出值并返回此比例。

js
const color = d3.scaleOrdinal(["a", "b", "c"], d3.schemeTableau10).unknown(null);
color("a"); // "#4e79a7"
color("b"); // "#f28e2c"
color("c"); // "#e15759"
color("d"); // null

如果未指定 value,则返回当前未知值,默认值为 implicit。隐式值启用隐式域构建;参见 ordinal.domain

🌐 If value is not specified, returns the current unknown value, which defaults to implicit. The implicit value enables implicit domain construction; see ordinal.domain.

ordinal.copy()

示例 · 来源 · 返回此序数刻度的精确副本。

js
const c1 = d3.scaleOrdinal(["a", "b", "c"], d3.schemeTableau10);
const c2 = c1.copy();

更改此比例不会影响返回的比例,反之亦然。

🌐 Changes to this scale will not affect the returned scale, and vice versa.

scaleImplicit

示例 · 来源 · ordinal.unknown 的一个特殊值,支持隐式域构建:未知值会被隐式添加到域中。

js
const color = d3.scaleOrdinal(["a", "b", "c"], d3.schemeTableau10);
color.unknown(); // d3.scaleImplicit

小心

建议使用显式域以实现确定性行为;从使用情况推断域取决于顺序。