Skip to content

量化尺度

¥Quantize scales

量化比例与 线性比例尺 类似,只是前者使用离散范围而非连续范围。根据输出范围中的值数量(即基数),将连续输入域划分为均匀的段。每个范围值 y 可以表示为定义域值 x 的量化线性函数:y = m round(x) + b。请参阅 量化的等值线图,了解示例。

¥Quantize scales are similar to linear scales, except they use a discrete rather than continuous range. The continuous input domain is divided into uniform segments based on the number of values in (i.e., the cardinality of) the output range. Each range value y can be expressed as a quantized linear function of the domain value x: y = m round(x) + b. See the quantized choropleth for an example.

scaleQuantize(domain, range)

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

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

js
const color = d3.scaleQuantize([0, 100], d3.schemeBlues[9]);

如果未指定域或范围,则每个值默认为 [0, 1]。

¥If either domain or range is not specified, each defaults to [0, 1].

js
const color = d3.scaleQuantize(d3.schemeBlues[9]);

quantize(value) {#_quantize}

示例 · 源代码 · 给定输入 domain 中的值,返回输出 range 中的对应值。例如,要应用颜色编码:

¥Examples · Source · Given a value in the input domain, returns the corresponding value in the output range. For example, to apply a color encoding:

js
const color = d3.scaleQuantize([0, 1], ["brown", "steelblue"]);
color(0.49); // "brown"
color(0.51); // "steelblue"

或者将区域划分为三个大小相等、范围值不同的部分,以计算合适的笔触宽度:

¥Or dividing the domain into three equally-sized parts with different range values to compute an appropriate stroke width:

js
const width = d3.scaleQuantize([10, 100], [1, 2, 4]);
width(20); // 1
width(50); // 2
width(80); // 4

quantize.invertExtent(value) {#quantize_invertExtent}

示例 · 源代码 · 返回 domain [x0, x1] 中值与 range 中对应值的范围:quantize 的逆。此方法对于交互很有用,例如,确定域中与鼠标下方像素位置对应的值。

¥Examples · Source · Returns the extent of values in the domain [x0, x1] for the corresponding value in the range: the inverse of quantize. This method is useful for interaction, say to determine the value in the domain that corresponds to the pixel location under the mouse.

js
const width = d3.scaleQuantize([10, 100], [1, 2, 4]);
width.invertExtent(2); // [40, 70]

quantize.domain(domain) {#quantize_domain}

示例 · 源代码 · 如果指定了 domain,则将比例尺的 domain 设置为指定的二元数值数组。

¥Examples · Source · If domain is specified, sets the scale’s domain to the specified two-element array of numbers.

js
const color = d3.scaleQuantize(d3.schemeBlues[9]);
color.domain([0, 100]);

如果给定数组中的元素不是数字,则它们将被强制转换为数字。数字必须按升序排列,否则比例尺的行为将不确定。

¥If the elements in the given array are not numbers, they will be coerced to numbers. The numbers must be in ascending order or the behavior of the scale is undefined.

如果未指定域,则返回比例尺的当前域。

¥If domain is not specified, returns the scale’s current domain.

js
color.domain() // [0, 100]

quantize.range(range) {#quantize_range}

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

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

js
const color = d3.scaleQuantize();
color.range(d3.schemeBlues[5]);

数组可以包含任意数量的离散值。给定数组中的元素不必是数字;任何值或类型都可以。

¥The array may contain any number of discrete values. The elements in the given array need not be numbers; any value or type will work.

如果未指定 range,则返回比例尺的当前范围。

¥If range is not specified, returns the scale’s current range.

js
color.range() // ["#eff3ff", "#bdd7e7", "#6baed6", "#3182bd", "#08519c"]

quantize.thresholds() {#quantize_thresholds}

示例 · 源代码 · 返回 domain 中计算出的阈值数组。

¥Examples · Source · Returns the array of computed thresholds within the domain.

js
color.thresholds() // [0.2, 0.4, 0.6, 0.8]

返回的阈值数量比 range 的长度少 1:小于第一个阈值的值将被分配为范围中的第一个元素,而大于或等于最后一个阈值的值将被分配为范围中的最后一个元素。

¥The number of returned thresholds is one less than the length of the range: values less than the first threshold are assigned the first element in the range, whereas values greater than or equal to the last threshold are assigned the last element in the range.

quantize.copy() {#quantize_copy}

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

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

js
const c1 = d3.scaleQuantize(d3.schemeBlues[5]);
const c2 = c1.copy();

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

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