2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 587d7fac367417b2b2512bdb
|
2021-03-09 15:51:59 +00:00
|
|
|
|
title: 按比例设置域和范围
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 6
|
2020-09-17 16:13:05 +00:00
|
|
|
|
forumTopicId: 301491
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: set-a-domain-and-a-range-on-a-scale
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-03-09 15:51:59 +00:00
|
|
|
|
默认情况下,比例尺使用一对一关系(identity relationship), 即输入值直接映射为输出值。 但是比例尺可以更灵活更有趣。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-03-09 15:51:59 +00:00
|
|
|
|
假设有一个数据集范围为 50 到 480, 这是缩放的输入信息,也被称为<dfn>域</dfn>。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-03-09 15:51:59 +00:00
|
|
|
|
你想沿着 `x` 轴将这些点映射到 SVG 画布上,位置介于 10 个单位到 500 个单位之间。 这是输出信息,也被称为<dfn>范围</dfn>。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-03-09 15:51:59 +00:00
|
|
|
|
`domain()` 和 `range()` 方法设置比例尺的值, 它们都接受一个至少有两个元素的数组作为参数。 下面是一个例子:
|
2020-09-17 16:13:05 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
scale.domain([50, 480]);
|
|
|
|
|
scale.range([10, 500]);
|
2021-03-09 15:51:59 +00:00
|
|
|
|
scale(50)
|
|
|
|
|
scale(480)
|
|
|
|
|
scale(325)
|
|
|
|
|
scale(750)
|
2020-09-17 16:13:05 +00:00
|
|
|
|
d3.scaleLinear()
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-09 15:51:59 +00:00
|
|
|
|
按顺序,将在控制台中显示以下值:`10`、`500`、`323.37` 和 `807.67`。
|
|
|
|
|
|
|
|
|
|
注意,比例尺使用了域和范围之间的线性关系来找出给定数字的输出值。 域中的最小值(50)映射为范围中的最小值(10)。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-09 15:51:59 +00:00
|
|
|
|
创建一个比例尺,将它的域设置为 `[250, 500]`,范围设置为 `[10, 150]`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-09 15:51:59 +00:00
|
|
|
|
**注意:**你可以将 `domain()` 和 `range()` 方法串联在 `scale` 变量后。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-09 15:51:59 +00:00
|
|
|
|
应使用 `domain()` 方法。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/\.domain/g));
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-09 15:51:59 +00:00
|
|
|
|
`scale` 的 `domain()` 应为 `[250, 500]`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(JSON.stringify(scale.domain()) == JSON.stringify([250, 500]));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-09 15:51:59 +00:00
|
|
|
|
应使用 `range()` 方法。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/\.range/g));
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-09 15:51:59 +00:00
|
|
|
|
`scale` 的 `range()` 应为 `[10, 150]`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(JSON.stringify(scale.range()) == JSON.stringify([10, 150]));
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-09 15:51:59 +00:00
|
|
|
|
`h2` 的文本应为 `-102`。
|
2020-09-17 16:13:05 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert($('h2').text() == '-102');
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00: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 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00: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>
|
|
|
|
|
```
|