1
0
mirror of https://github.com/d3/d3.git synced 2025-12-08 19:46:24 +00:00
d3/docs/d3-scale/diverging.md
luizbarboza 3f4a2d7cfe
always have exactly two/three elements (#3782)
* always has exactly three elements

* always have exactly two/three elements
2023-10-15 03:17:55 -04:00

3.7 KiB
Raw Permalink Blame History

Diverging scales

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)

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

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

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

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

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

const identity = d3.scaleDiverging();

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.

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

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

A diverging scales domain must be numeric and must contain exactly three values.

diverging.interpolator(interpolator)

If interpolator is specified, sets the scales interpolator to the specified function.

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

If interpolator is not specified, returns the scales current interpolator.

color.interpolator() // d3.interpolateRdBu

diverging.range(range)

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

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

The above is equivalent to:

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

diverging.rangeRound(range)

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.