分位数尺度
🌐 Quantile scales
分位数比例尺将采样的输入域映射到离散范围。该域被认为是连续的,因此比例尺将接受任何合理的输入值;然而,域被指定为一组离散的样本值。输出范围中的值的数量(基数)决定将从域中计算出的分位数量。为了计算分位数,先对域进行排序,并将其视为离散值的总体;参见分位数。有关示例,请参见此分位数分级图。
🌐 Quantile scales map a sampled input domain to a discrete range. The domain is considered continuous and thus the scale will accept any reasonable input value; however, the domain is specified as a discrete set of sample values. The number of values in (the cardinality of) the output range determines the number of quantiles that will be computed from the domain. To compute the quantiles, the domain is sorted, and treated as a population of discrete values; see quantile. See this quantile choropleth for an example.
scaleQuantile(domain, range)
const color = d3.scaleQuantile(penguins.map((d) => d.body_mass_g), d3.schemeBlues[5]);如果未指定 domain 或 range,每个都默认为空数组。在指定了 domain 和 range 之前,分位数比例是无效的。
🌐 If either domain or range is not specified, each defaults to the empty array. The quantile scale is invalid until both a domain and range are specified.
分位数(值)
🌐 quantile(value)
示例 · 来源 · 给定输入域中的值,返回输出值域中的对应值。
color(3000); // "#eff3ff"
color(4000); // "#6baed6"
color(5000); // "#08519c"quantile.invertExtent(value)
示例 · 来源 · 返回 域 中 [x0, x1] 对应 值 的值域:分位数 的逆运算。
color.invertExtent("#eff3ff"); // [2700, 3475]
color.invertExtent("#6baed6"); // [3800, 4300]
color.invertExtent("#08519c"); // [4950, 6300]此方法对于交互很有用,例如,确定域中与鼠标下方像素位置对应的值。
🌐 This method is useful for interaction, say to determine the value in the domain that corresponds to the pixel location under the mouse.
quantile.domain(domain)
示例 · 来源 · 如果指定了domain,则将分位数刻度的域设置为指定的一组离散数值,并返回该刻度。
const color = d3.scaleQuantile(d3.schemeBlues[5]);
color.domain(penguins.map((d) => d.body_mass_g));数组不能为空,且必须至少包含一个数值;NaN、null 和 undefined 值会被忽略,不计入样本总体。如果给定数组中的元素不是数字,它们将被强制转换为数字。输入数组的副本会被排序并在内部存储。
🌐 The array must not be empty, and must contain at least one numeric value; NaN, null and undefined values are ignored and not considered part of the sample population. If the elements in the given array are not numbers, they will be coerced to numbers. A copy of the input array is sorted and stored internally.
如果未指定 domain,则返回比例的当前域(观察到的值的集合)。
🌐 If domain is not specified, returns the scale’s current domain (the set of observed values).
color.domain() // [2700, 2850, 2850, 2900, 2900, 2900, 2900, …]quantile.range(range)
示例 · 来源 · 如果指定了 range,则设置范围内的离散值。
const color = d3.scaleQuantile();
color.range(d3.schemeBlues[5]);数组不能为空,并且可以包含任何类型的值。range 数组中的值的数量(基数或长度)决定了计算的分位数的数量。例如,要计算四分位数,range 必须是一个包含四个元素的数组,例如 [0, 1, 2, 3]。
🌐 The array must not be empty, and may contain any type of value. The number of values in (the cardinality, or length, of) the range array determines the number of quantiles that are computed. For example, to compute quartiles, range must be an array of four elements such as [0, 1, 2, 3].
如果未指定 range,则返回当前范围。
🌐 If range is not specified, returns the current range.
color.range() // ["#eff3ff", "#bdd7e7", "#6baed6", "#3182bd", "#08519c"]quantile.quantiles()
color.quantiles() // [3475, 3800, 4300, 4950]如果 range 包含 n 个离散值,则返回的数组将包含 n - 1 个阈值。小于第一个阈值的值被认为属于第一个分位数;大于或等于第一个阈值但小于第二个阈值的值属于第二个分位数,依此类推。在内部,阈值数组与 bisect 一起使用,以找到与给定输入值关联的输出分位数。
🌐 If the range contains n discrete values, the returned array will contain n - 1 thresholds. Values less than the first threshold are considered in the first quantile; values greater than or equal to the first threshold but less than the second threshold are in the second quantile, and so on. Internally, the thresholds array is used with bisect to find the output quantile associated with the given input value.
quantile.copy()
const c1 = d3.scaleQuantile(d3.schemeBlues[5]);
const c2 = c1.copy();更改此比例不会影响返回的比例,反之亦然。
🌐 Changes to this scale will not affect the returned scale, and vice versa.