Skip to content

Linear scales

Linear scales map a continuous, quantitative input domain to a continuous output range using a linear transformation (translate and scale). If the range is also numeric, the mapping may be inverted. Linear scales are a good default choice for continuous quantitative data because they preserve proportional differences. Each range value y can be expressed as a function of the domain value x: y = mx + b.

scaleLinear(domain, range)

Examples · Source · Constructs a new linear scale with the specified domain and range, the default interpolator, and clamping disabled.

js
d3.scaleLinear([0, 100], ["red", "blue"])

If a single argument is specified, it is interpreted as the range. If either domain or range are not specified, each defaults to [0, 1].

js
d3.scaleLinear(["red", "blue"]) // default domain of [0, 1]

linear(value)

Examples · Source · Given a value from the domain, returns the corresponding value from the range. For example, to apply a position encoding:

js
const x = d3.scaleLinear([10, 130], [0, 960]);
x(20); // 80
x(50); // 320

To apply a color encoding:

js
const color = d3.scaleLinear([10, 100], ["brown", "steelblue"]);
color(20); // "rgb(154, 52, 57)"
color(50); // "rgb(123, 81, 103)"

If the given value is outside the domain, and clamping is not enabled, the mapping will be extrapolated such that the returned value is outside the range.

linear.invert(value)

Examples · Source · Given a value from the range, returns the corresponding value from the domain. Inversion is useful for interaction, say to determine the data value corresponding to the position of the mouse. For example, to invert a position encoding:

js
const x = d3.scaleLinear([10, 130], [0, 960]);
x.invert(80); // 20
x.invert(320); // 50

If the given value is outside the range, and clamping is not enabled, the mapping may be extrapolated such that the returned value is outside the domain. This method is only supported if the range is numeric. If the range is not numeric, returns NaN.

For a valid value y in the range, linear(linear.invert(y)) approximately equals y; similarly, for a valid value x in the domain, linear.invert(linear(x)) approximately equals x. The scale and its inverse may not be exact due to the limitations of floating point precision.

linear.domain(domain)

Examples · Source · If domain is specified, sets the scale’s domain to the specified array of numbers and returns this scale.

js
const x = d3.scaleLinear().domain([10, 130]);

The array must contain two or more elements. If the elements in the given array are not numbers, they will be coerced to numbers.

Although continuous scales typically have two values each in their domain and range, specifying more than two values produces a piecewise scale. For example, to create a diverging color scale that interpolates between white and red for negative values, and white and green for positive values, say:

js
const color = d3.scaleLinear([-1, 0, 1], ["red", "white", "green"]);
color(-0.5); // "rgb(255, 128, 128)"
color(+0.5); // "rgb(128, 192, 128)"

Internally, a piecewise scale performs a binary search for the range interpolator corresponding to the given domain value. Thus, the domain must be in ascending or descending order. If the domain and range have different lengths N and M, only the first min(N,M) elements in each are observed.

If domain is not specified, returns a copy of the scale’s current domain.

js
color.domain() // [-1, 0, 1]

linear.range(range)

Examples · Source · If range is specified, sets the scale’s range to the specified array of values and returns this scale.

js
const x = d3.scaleLinear().range([0, 960]);

The array must contain two or more elements. Unlike the domain, elements in the given array need not be numbers; any value that is supported by the underlying interpolator will work, though note that numeric ranges are required for invert.

If range is not specified, returns a copy of the scale’s current range.

js
x.range() // [0, 960]

See linear.interpolate for more examples.

linear.rangeRound(range)

Examples · Source · Sets the scale’s range to the specified array of values while also setting the scale’s interpolator to interpolateRound; returns this scale.

js
const x = d3.scaleLinear().rangeRound([0, 960]);

This is a convenience method equivalent to:

js
linear.range(range).interpolate(d3.interpolateRound)

The rounding interpolator is sometimes useful for avoiding antialiasing artifacts, though also consider the shape-rendering “crispEdges” styles. Note that this interpolator can only be used with numeric ranges.

linear.clamp(clamp)

Examples · Source · If clamp is specified, enables or disables clamping accordingly; returns this scale.

js
const x = d3.scaleLinear([0, 960]).clamp(true);

If clamping is disabled and the scale is passed a value outside the domain, the scale may return a value outside the range through extrapolation. If clamping is enabled, the return value of the scale is always within the scale’s range. Clamping similarly applies to linear.invert. For example:

js
const x = d3.scaleLinear([10, 130], [0, 960]); // clamping disabled by default
x(-10); // -160, outside range
x.invert(-160); // -10, outside domain
x.clamp(true); // enable clamping
x(-10); // 0, clamped to range
x.invert(-160); // 10, clamped to domain

If clamp is not specified, returns whether or not the scale currently clamps values to within the range.

js
x.clamp() // true, perhaps

linear.unknown(value)

Examples · Source · If value is specified, sets the output value of the scale for undefined or NaN input values and returns this scale. This is useful for specifying how missing or invalid data is displayed.

js
const color = d3.scaleLinear([0, 100], ["red", "blue"]).unknown("#ccc");
color(NaN); // "#ccc"

If value is not specified, returns the current unknown value, which defaults to undefined.

js
color.unknown() // "#ccc"

linear.interpolate(interpolate)

Examples · Source · If interpolate is specified, sets the scale’s range interpolator factory.

js
const color = d3.scaleLinear(["red", "blue"]).interpolate(d3.interpolateHcl);

The scale’s interpolator factory is used to create interpolators for each adjacent pair of values from the range; these interpolators then map a normalized domain parameter t in [0, 1] to the corresponding value in the range. If factory is not specified, returns the scale’s current interpolator factory, which defaults to d3.interpolate. See d3-interpolate for more interpolators.

For example, consider a diverging color scale with three colors in the range:

js
const color = d3.scaleLinear([-100, 0, +100], ["red", "white", "green"]);

Two interpolators are created internally by the scale, equivalent to:

js
const i0 = d3.interpolate("red", "white");
const i1 = d3.interpolate("white", "green");

A common reason to specify a custom interpolator is to change the color space of interpolation. For example, to use HCL:

js
const color = d3.scaleLinear()
    .domain([10, 100])
    .range(["brown", "steelblue"])
    .interpolate(d3.interpolateHcl);

Or for Cubehelix with a custom gamma:

js
const color = d3.scaleLinear()
    .domain([10, 100])
    .range(["brown", "steelblue"])
    .interpolate(d3.interpolateCubehelix.gamma(3));

CAUTION

The default interpolator may reuse return values. For example, if the range values are objects, then the value interpolator always returns the same object, modifying it in-place. If the scale is used to set an attribute or style, this is typically acceptable (and desirable for performance); however, if you need to store the scale’s return value, you must specify your own interpolator or make a copy as appropriate.

linear.ticks(count)

Examples · Source · Returns approximately count representative values from the scale’s domain.

js
const x = d3.scaleLinear([10, 100], ["red", "blue"]);
x.ticks(); // [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

If count is not specified, it defaults to 10. The returned tick values are uniformly spaced, have human-readable values (such as multiples of powers of 10), and are guaranteed to be within the extent of the domain. Ticks are often used to display reference lines, or tick marks, in conjunction with the visualized data. The specified count is only a hint; the scale may return more or fewer values depending on the domain. See also d3-array’s ticks.

linear.tickFormat(count, specifier)

Examples · Source · Returns a number format function suitable for displaying a tick value, automatically computing the appropriate precision based on the fixed interval between tick values. The specified count should have the same value as the count that is used to generate the tick values.

js
const x = d3.scaleLinear([0.1, 1], ["red", "blue"]);
const f = x.tickFormat();
f(0.1); // "0.1"
f(1); // "1.0"

An optional specifier allows a custom format where the precision of the format is automatically set by the scale as appropriate for the tick interval. For example, to format percentage change, you might say:

js
const x = d3.scaleLinear([-1, 1], [0, 960]);
const T = x.ticks(5); // [-1, -0.5, 0, 0.5, 1]
const f = x.tickFormat(5, "+%");
T.map(f); // ["−100%", "−50%", "+0%", "+50%", "+100%"]

If specifier uses the format type s, the scale will return a SI-prefix format based on the largest value in the domain. If the specifier already specifies a precision, this method is equivalent to locale.format.

See also d3.tickFormat.

linear.nice(count)

Examples · Source · Extends the domain so that it starts and ends on nice round values.

js
const x = d3.scaleLinear([0.241079, 0.969679], [0, 960]).nice();
x.domain(); // [0.2, 1]

This method typically modifies the scale’s domain, and may only extend the bounds to the nearest round value. Nicing is useful if the domain is computed from data, say using extent, and may be irregular. If the domain has more than two values, nicing the domain only affects the first and last value.

An optional tick count argument allows greater control over the step size used to extend the bounds, guaranteeing that the returned ticks will exactly cover the domain.

js
const x = d3.scaleLinear([0.241079, 0.969679], [0, 960]).nice(40);
x.domain(); // [0.24, 0.98]

Nicing a scale only modifies the current domain; it does not automatically nice domains that are subsequently set using linear.domain. You must re-nice the scale after setting the new domain, if desired.

linear.copy()

Examples · Source · Returns an exact copy of this scale.

js
const x1 = d3.scaleLinear([0, 100], ["red", "blue"]);
const x2 = x1.copy();

Changes to this scale will not affect the returned scale, and vice versa.

tickFormat(start, stop, count, specifier)

Examples · Source · Returns a number format function suitable for displaying a tick value, automatically computing the appropriate precision based on the fixed interval between tick values, as determined by d3.tickStep.

js
const f = d3.tickFormat(0, 1, 20);
f(1); // "1.00"

An optional specifier allows a custom format where the precision of the format is automatically set by the scale as appropriate for the tick interval. For example, to format percentage change, you might say:

js
const f = d3.tickFormat(-1, 1, 5, "+%");
f(-0.5); // "-50%"

If specifier uses the format type s, the scale will return a SI-prefix format based on the larger absolute value of start and stop. If the specifier already specifies a precision, this method is equivalent to locale.format.

scaleIdentity(range)

Examples · Source · Constructs a new identity scale with the specified range (and by extension, domain).

js
const x = d3.scaleIdentity([0, 960]);

Identity scales are a special case of linear scales where the domain and range are identical; the scale and its invert method are thus the identity function. These scales are occasionally useful when working with pixel coordinates, say in conjunction with an axis. Identity scales do not support rangeRound, clamp or interpolate.

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

scaleRadial(domain, range)

Examples · Source · Constructs a new radial scale with the specified domain and range.

js
const r = d3.scaleRadial([100, 200], [0, 480]);

Radial scales are a variant of linear scales where the range is internally squared so that an input value corresponds linearly to the squared output value. These scales are useful when you want the input value to correspond to the area of a graphical mark and the mark is specified by radius, as in a radial bar chart. Radial scales do not support interpolate.

If domain or range is not specified, each defaults to [0, 1].