Skip to content

发散尺度

¥Diverging scales

发散比例与 线性比例尺 类似,因为它们将连续的数字输入域映射到连续的输出范围。Unlike linear scales, the input domain and output range of a diverging scale always have exactly three elements, and the output range is typically specified as an interpolator rather than an array of values.发散尺度通常用于颜色编码;另见 d3-scale-chromatic。这些比例尺不公开 invertinterpolate 方法。发散缩放比例也有 logpowsymlog 变体。

¥Diverging scales are similar to linear scales in that they map a continuous, numeric input domain to a continuous output range. Unlike linear scales, the input domain and output range of a diverging scale always have exactly three elements, and the output range is typically specified as an interpolator rather than an array of values. Diverging scales are typically used for a color encoding; see also d3-scale-chromatic. These scales do not expose invert and interpolate methods. There are also log, pow, and symlog variants of diverging scales.

scaleDiverging(domain, interpolator)

示例 · 源代码 · 使用指定的 domaininterpolator 函数或数组构造一个新的发散比例尺。

¥Examples · Source · Constructs a new diverging scale with the specified domain and interpolator function or array.

js
const color = d3.scaleDiverging([-1, 0, 1], d3.interpolateRdBu);

如果未指定域,则默认为 [0, 0.5, 1]。

¥If domain is not specified, it defaults to [0, 0.5, 1].

js
const color = d3.scaleDiverging(d3.interpolateRdBu);

如果未指定 interpolator,则默认为恒等函数。

¥If interpolator is not specified, it defaults to the identity function.

js
const identity = d3.scaleDiverging();

应用比例时,将使用通常在 [0, 1] 范围内的值调用插值器,其中 0 表示极负值,0.5 表示中性值,1 表示极正值。

¥When the scale is applied, the interpolator will be invoked with a value typically in the range [0, 1], where 0 represents the extreme negative value, 0.5 represents the neutral value, and 1 represents the extreme positive value.

如果 interpolate 是一个数组,则它表示比例尺的三元素输出范围,并使用 d3.interpolated3.piecewise 转换为插值函数。

¥If interpolator is an array, it represents the scale’s three-element output range and is converted to an interpolator function using d3.interpolate and d3.piecewise.

js
const color = d3.scaleDiverging(["blue", "white", "red"]);

发散尺度的域必须是数字,并且必须恰好包含三个值。

¥A diverging scale’s domain must be numeric and must contain exactly three values.

diverging.interpolator(interpolator) {#diverging_interpolator}

如果指定了 interpolator,则将比例尺的插值器设置为指定的函数。

¥If interpolator is specified, sets the scale’s interpolator to the specified function.

js
const color = d3.scaleDiverging().interpolator(d3.interpolateRdBu);

如果未指定 interpolator,则返回比例尺的当前插值器。

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

js
color.interpolator() // d3.interpolateRdBu

diverging.range(range) {#diverging_range}

请参阅 linear.range。如果指定了 range,则给定的三元素数组将使用 piecewise 转换为插值器函数。

¥See linear.range. If range is specified, the given three-element array is converted to an interpolator function using piecewise.

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

以上内容等同于:

¥The above is equivalent to:

js
const color = d3.scaleDiverging(d3.piecewise(["blue", "white", "red"]));

diverging.rangeRound(range) {#diverging_rangeRound}

请参阅 linear.range。如果指定了 range,则隐式使用 interpolateRound 作为插值器。

¥See linear.range. If range is specified, implicitly uses interpolateRound as the interpolator.

scaleDivergingLog(domain, range)

返回一个具有对数变换的新发散缩放比例,类似于 对数比例尺

¥Returns a new diverging scale with a logarithmic transform, analogous to a log scale.

scaleDivergingPow(domain, range)

返回一个具有指数变换的新发散缩放比例,类似于 功率标度

¥Returns a new diverging scale with an exponential transform, analogous to a power scale.

scaleDivergingSqrt(domain, range)

返回一个具有平方根变换的新发散缩放比例,类似于 平方比例尺

¥Returns a new diverging scale with a square-root transform, analogous to a sqrt scale.

scaleDivergingSymlog(domain, range)

返回一个具有对称对数变换的新发散缩放比例,类似于 符号对数尺度

¥Returns a new diverging scale with a symmetric logarithmic transform, analogous to a symlog scale.