Skip to content

序数尺度

¥Ordinal scales

Unlike 连续缩放, ordinal scales have a discrete domain and range.例如,序数尺度可能会将一组命名类别映射到一组颜色,或确定柱形图中各列的水平位置。

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

示例 · 源代码 · 使用指定的 domainrange,构造一个新的序数比例尺。

¥Examples · Source · Constructs a new ordinal scale with the specified domain and range.

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

如果未指定域,则默认为空数组。如果未指定范围,则默认为空数组;序数比例始终返回未定义,直到定义非空范围。

¥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) {#_ordinal}

示例 · 源代码 · 给定输入 domain 中的值,返回输出 range 中的对应值。

¥Examples · Source · Given a value in the input domain, returns the corresponding value in the output range.

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

如果给定值不在比例尺的 domain 中,则返回 未知值;或者,如果未知值是 implicit(默认值),则该值将隐式添加到域中,并将范围中的下一个可用值赋给值,这样,给定相同输入值的本次调用和后续调用比例尺将返回相同的输出值。

¥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) {#ordinal_domain}

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

¥Examples · Source · If domain is specified, sets the domain to the specified array of values.

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

域中的第一个元素将映射到范围中的第一个元素,第二个域值映射到第二个范围值,依此类推。域值内部存储在 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"]

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

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

如果 未知值implicit(默认值),则按序数缩放域是可选的。在这种情况下,将通过为每个传递给比例尺的唯一值从范围中分配一个新值,从使用情况中隐式推断出域。

¥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"]

CAUTION

建议使用明确的域来实现确定性行为;根据使用情况推断域取决于顺序。

¥An explicit domain is recommended for deterministic behavior; inferring the domain from usage is dependent on ordering.

ordinal.range(range) {#ordinal_range}

示例 · 源代码 · 如果指定了 range,则将序数比例尺的范围设置为指定的值数组。

¥Examples · Source · If range is specified, sets the range of the ordinal scale to the specified array of values.

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

域中的第一个元素将映射到范围中的第一个元素,第二个域值映射到第二个范围值,依此类推。如果范围中的元素少于定义域中的元素,比例尺将重用范围起始处的值。如果未指定 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) {#ordinal_unknown}

示例 · 源代码 · 如果指定了值,则设置未知输入值的比例尺的输出值并返回该比例尺。

¥Examples · Source · If value is specified, sets the output value of the scale for unknown input values and returns this scale.

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() {#ordinal_copy}

示例 · 源代码 · 返回此序数比例尺的精确副本。

¥Examples · Source · Returns an exact copy of this ordinal scale.

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 的一个特殊值,用于隐式构造域:未知值将隐式添加到域中。

¥Examples · Source · A special value for ordinal.unknown that enables implicit domain construction: unknown values are implicitly added to the domain.

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

CAUTION

建议使用明确的域来实现确定性行为;根据使用情况推断域取决于顺序。

¥An explicit domain is recommended for deterministic behavior; inferring the domain from usage is dependent on ordering.