2018-10-10 18:03:03 -04:00
---
id: 587d7fac367417b2b2512bdb
2021-02-06 04:42:36 +00:00
title: Set a Domain and a Range on a Scale
2018-10-10 18:03:03 -04:00
challengeType: 6
2020-09-18 00:13:05 +08:00
forumTopicId: 301491
2021-01-13 03:31:00 +01:00
dashedName: set-a-domain-and-a-range-on-a-scale
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
By default, scales use the identity relationship. This means the input value maps to the output value. However, scales can be much more flexible and interesting.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Say a dataset has values ranging from 50 to 480. This is the input information for a scale, also known as the < dfn > domain< / dfn > .
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
You want to map those points along the `x` axis on the SVG canvas, between 10 units and 500 units. This is the output information, also known as the < dfn > range</ dfn > .
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
The `domain()` and `range()` methods set these values for the scale. Both methods take an array of at least two elements as an argument. Here's an example:
2020-09-18 00:13:05 +08:00
```js
2021-02-06 04:42:36 +00:00
// Set a domain
// The domain covers the set of input values
2020-09-18 00:13:05 +08:00
scale.domain([50, 480]);
2021-02-06 04:42:36 +00:00
// Set a range
// The range covers the set of output values
2020-09-18 00:13:05 +08:00
scale.range([10, 500]);
2021-02-06 04:42:36 +00:00
scale(50) // Returns 10
scale(480) // Returns 500
scale(325) // Returns 323.37
scale(750) // Returns 807.67
2020-09-18 00:13:05 +08:00
d3.scaleLinear()
```
2021-02-06 04:42:36 +00:00
Notice that the scale uses the linear relationship between the domain and range values to figure out what the output should be for a given number. The minimum value in the domain (50) maps to the minimum value (10) in the range.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Create a scale and set its domain to `[250, 500]` and range to `[10, 150]` .
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
**Note**
You can chain the `domain()` and `range()` methods onto the `scale` variable.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Your code should use the `domain()` method.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(code.match(/\.domain/g));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The `domain()` of the scale should be set to `[250, 500]` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(JSON.stringify(scale.domain()) == JSON.stringify([250, 500]));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
Your code should use the `range()` method.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(code.match(/\.range/g));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The `range()` of the scale should be set to `[10, 150]` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(JSON.stringify(scale.range()) == JSON.stringify([10, 150]));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The text in the `h2` should be -102.
2020-09-18 00:13:05 +08:00
2020-12-16 00:37:30 -07:00
```js
assert($('h2').text() == '-102');
2018-10-10 18:03:03 -04:00
```
2020-08-13 17:24:35 +02:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```html
< body >
< script >
// Add your code below this line
const scale = d3.scaleLinear();
// Add your code above this line
const output = scale(50);
d3.select("body")
.append("h2")
.text(output);
< / script >
< / body >
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```html
< body >
< script >
const scale = d3.scaleLinear();
scale.domain([250, 500])
scale.range([10, 150])
const output = scale(50);
d3.select("body")
.append("h2")
.text(output);
< / script >
< / body >
```