阈值尺度
🌐 Threshold scales
阈值刻度类似于 量化刻度,不同之处在于它们允许你将域的任意子集映射到范围内的离散值。输入域仍然是连续的,并根据一组阈值被划分为多个片段。参见 这个分级地图 作为示例。
🌐 Threshold scales are similar to quantize scales, except they allow you to map arbitrary subsets of the domain to discrete values in the range. The input domain is still continuous, and divided into slices based on a set of threshold values. See this choropleth for an example.
scaleThreshold(domain, range)
示例 · 来源 · 使用指定的 domain 和 range 构建一个新的阈值比例。
const color = d3.scaleThreshold([0, 1], ["red", "white", "blue"]);如果未指定domain,则默认为[0.5]。
🌐 If domain is not specified, it defaults to [0.5].
const color = d3.scaleThreshold(["red", "blue"]);
color(0); // "red"
color(1); // "blue"如果未指定 range,则默认为 [0, 1]。
🌐 If range is not specified, it defaults to [0, 1].
阈值(值)
🌐 threshold(value)
示例 · 来源 · 给定输入域中的一个值,返回输出范围中的对应值。例如:
const color = d3.scaleThreshold([0, 1], ["red", "white", "green"]);
color(-1); // "red"
color(0); // "white"
color(0.5); // "white"
color(1); // "green"
color(1000); // "green"threshold.invertExtent(value)
来源 · 返回范畴中对应值的值的范围[x0, x1],表示从值域到定义域的逆映射。
const color = d3.scaleThreshold([0, 1], ["red", "white", "green"]);
color.invertExtent("red"); // [undefined, 0]
color.invertExtent("white"); // [0, 1]
color.invertExtent("green"); // [1, undefined]这种方法对于交互非常有用,例如确定鼠标下像素位置对应的域值。低于最低阈值的范围是不确定的(无限的),高于最高阈值的范围也是如此。
🌐 This method is useful for interaction, say to determine the value in the domain that corresponds to the pixel location under the mouse. The extent below the lowest threshold is undefined (unbounded), as is the extent above the highest threshold.
threshold.domain(domain)
示例 · 来源 · 如果指定了domain,则将刻度的域设置为指定的值数组。
const color = d3.scaleThreshold(["red", "white", "green"]).domain([0, 1]);这些值必须按升序排列,否则比例尺的行为未定义。这些值通常是数字,但任何自然有序的值(例如字符串)也可以;阈值比例尺可以用来编码任何有序类型。如果比例尺范围中的值的数量为 n + 1,则比例尺域中的值的数量必须为 n。如果域中的元素少于 n 个,则范围中多余的值将被忽略。如果域中的元素多于 n 个,则比例尺对于某些输入可能返回未定义值。
🌐 The values must be in ascending order or the behavior of the scale is undefined. The values are typically numbers, but any naturally ordered values (such as strings) will work; a threshold scale can be used to encode any type that is ordered. If the number of values in the scale’s range is n + 1, the number of values in the scale’s domain must be n. If there are fewer than n elements in the domain, the additional values in the range are ignored. If there are more than n elements in the domain, the scale may return undefined for some inputs.
如果未指定 domain,则返回刻度当前的域。
🌐 If domain is not specified, returns the scale’s current domain.
color.domain() // [0, 1]threshold.range(range)
示例 · 来源 · 如果指定了range,则将刻度的范围设置为指定的值数组。
const color = d3.scaleThreshold().range(["red", "white", "green"]);如果量表域中的值的数量是 n,则量表值域中的值的数量必须是 n + 1。如果值域中的元素少于 n + 1,量表对某些输入可能返回未定义。如果值域中的元素多于 n + 1,多余的值将被忽略。给定数组中的元素不必是数字;任何值或类型都可以使用。
🌐 If the number of values in the scale’s domain is n, the number of values in the scale’s range must be n + 1. If there are fewer than n + 1 elements in the range, the scale may return undefined for some inputs. If there are more than n + 1 elements in the range, the additional values are ignored. 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.
color.range() // ["red", "white", "green"]threshold.copy()
const c1 = d3.scaleThreshold(d3.schemeBlues[5]);
const c2 = c1.copy();更改此比例不会影响返回的比例,反之亦然。
🌐 Changes to this scale will not affect the returned scale, and vice versa.