阈值尺度
¥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 构造一个新的阈值比例尺。
¥Examples · Source · Constructs a new threshold scale with the specified domain and range.
const color = d3.scaleThreshold([0, 1], ["red", "white", "blue"]);
如果未指定域,则默认为 [0.5]。
¥If domain is not specified, it defaults to [0.5].
const color = d3.scaleThreshold(["red", "blue"]);
color(0); // "red"
color(1); // "blue"
如果未指定范围,则默认为 [0, 1]。
¥If range is not specified, it defaults to [0, 1].
threshold(value) {#_threshold}
示例 · 源代码 · 给定输入 domain 中的值,返回输出 range 中的对应值。例如:
¥Examples · Source · Given a value in the input domain, returns the corresponding value in the output range. For example:
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) {#threshold_invertExtent}
源代码 · 返回 domain [x0, x1] 中值与 range 中值的范围,表示从范围到域的逆映射。
¥Source · Returns the extent of values in the domain [x0, x1] for the corresponding value in the range, representing the inverse mapping from range to domain.
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) {#threshold_domain}
示例 · 源代码 · 如果指定了 domain,则将比例尺的 domain 设置为指定的值数组。
¥Examples · Source · If domain is specified, sets the scale’s domain to the specified array of values.
const color = d3.scaleThreshold(["red", "white", "green"]).domain([0, 1]);
这些值必须按升序排列,否则缩放行为未定义。这些值通常是数字,但任何自然排序的值(例如字符串)都可以;阈值比例可用于编码任何有序类型。如果比例尺的范围中的值的数量为 n + 1,则比例尺的域中的值的数量必须为 n。如果定义域中的元素少于 n 个,则范围中的其他值将被忽略。如果定义域中的元素多于 n 个,比例尺可能会对某些输入返回 undefined。
¥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.
如果未指定域,则返回比例尺的当前域。
¥If domain is not specified, returns the scale’s current domain.
color.domain() // [0, 1]
threshold.range(range) {#threshold_range}
示例 · 源代码 · 如果指定了 range,则将比例尺的范围设置为指定的值数组。
¥Examples · Source · If range is specified, sets the scale’s range to the specified array of values.
const color = d3.scaleThreshold().range(["red", "white", "green"]);
如果比例尺的域中的值的数量为 n,则比例尺的范围中的值的数量必须为 n + 1。如果范围中的元素少于 n + 1 个,比例尺可能会对某些输入返回 undefined。如果范围中的元素多于 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() {#threshold_copy}
¥Examples · Source · Returns an exact copy of this scale.
const c1 = d3.scaleThreshold(d3.schemeBlues[5]);
const c2 = c1.copy();
更改此比例不会影响返回的比例,反之亦然。
¥Changes to this scale will not affect the returned scale, and vice versa.