.scaleLinear()
Anonymous contributor
Published Dec 18, 2023Updated May 15, 2024
Contribute to Docs
The .scaleLinear()
method is used to construct a new linear scale by specifying its domain and range. This scale is particularly useful for linearly mapping input values with a specified domain to corresponding output values in a defined range. The syntax for the .scaleLinear()
involves specifying the input domain and the corresponding output range.
Syntax
d3.scaleLinear()
.domain([minValue, maxValue])
.range([minOutput, maxOutput]);
.scaleLinear()
: The method that creates a linear scale for continuous mapping..domain()
: Requires theminValue
andmaxValue
arguments to set the accepted range..range()
: The second step in defining a scale, the argumentsminOutput
andmaxOutput
are used to specify the output range of the scale.
Example
In the example given below the scale
will map values within the specified domain to the corresponding range given. The scale
is set to have a domain from 0
to 100
and range from 0
to 500
. When the values are mapped from the domain passed to the scale, the range produces the output accordingly:
const scale = d3.scaleLinear().domain([0, 100]).range([0, 500]);console.log(scale(0));console.log(scale(50));console.log(scale(100));
The output of the above example will be:
0250500
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript:D3 on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn D3
Learn how to create bar charts with D3, the popular interactive data visualization library.With CertificateIntermediate1 hour