2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7fac367417b2b2512bdb
|
|
|
|
|
title: Set a Domain and a Range on a Scale
|
|
|
|
|
challengeType: 6
|
2020-09-18 00:13:05 +08:00
|
|
|
|
forumTopicId: 301491
|
|
|
|
|
localeTitle: 按比例设置域和范围
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-09-18 00:13:05 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
默认情况下,比例尺使用同一关系(identity relationship),即输入值直接映射为输出值。但是比例尺可以更灵活更有趣。
|
|
|
|
|
假设有一个数据集范围为 50 到 480,这是缩放的输入信息,也被称为域(domain)。
|
|
|
|
|
你想沿着 10 个单位到 500 个单位的 <code>x</code> 轴映射这些点到 SVG 画布上。这是输出信息,也被称为范围(range)。
|
|
|
|
|
<code>domain()</code> 和 <code>range()</code> 方法设置缩放的值,它们都以至少有两个元素的数组为参数。下面是一个例子:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// 设置域
|
|
|
|
|
// 域覆盖了一组输入值
|
|
|
|
|
scale.domain([50, 480]);
|
|
|
|
|
// 设置范围
|
|
|
|
|
// 范围覆盖了一组输出值
|
|
|
|
|
scale.range([10, 500]);
|
|
|
|
|
scale(50) // 返回 10
|
|
|
|
|
scale(480) // 返回 500
|
|
|
|
|
scale(325) // 返回 323.37
|
|
|
|
|
scale(750) // 返回 807.67
|
|
|
|
|
d3.scaleLinear()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
注意,比例尺使用了域和范围之间的线性关系来找出给定数字的输出值。域中的最小值(50)映射为范围中的最小值(10)。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-09-18 00:13:05 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
创建一个比例尺,将它的域设置为 <code>[250, 500]</code>,范围设置为 <code>[10, 150]</code>。
|
|
|
|
|
<strong>提示</strong><br>你可以将 <code>domain()</code> 和 <code>range()</code> 方法串联在 <code>scale</code> 变量后。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2020-09-18 00:13:05 +08:00
|
|
|
|
- text: 你应该使用 <code>domain()</code> 方法。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/\.domain/g));
|
2020-09-18 00:13:05 +08:00
|
|
|
|
- text: 比例尺的 <code>domain()</code> 应该设置为 <code>[250, 500]</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(JSON.stringify(scale.domain()) == JSON.stringify([250, 500]));
|
2020-09-18 00:13:05 +08:00
|
|
|
|
- text: 你应该使用 <code>range()</code> 方法。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/\.range/g));
|
2020-09-18 00:13:05 +08:00
|
|
|
|
- text: 比例尺的 <code>range()</code> 应该设置为 <code>[10, 150]</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(JSON.stringify(scale.range()) == JSON.stringify([10, 150]));
|
2020-09-18 00:13:05 +08:00
|
|
|
|
- text: <code>h2</code> 的文本应该为 -102。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert($('h2').text() == '-102');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='html-seed'>
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<body>
|
|
|
|
|
<script>
|
2020-09-18 00:13:05 +08:00
|
|
|
|
// 在下面添加你的代码
|
2018-10-10 18:03:03 -04:00
|
|
|
|
const scale = d3.scaleLinear();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-18 00:13:05 +08:00
|
|
|
|
// 在上面添加你的代码
|
2018-10-10 18:03:03 -04:00
|
|
|
|
const output = scale(50);
|
|
|
|
|
d3.select("body")
|
|
|
|
|
.append("h2")
|
|
|
|
|
.text(output);
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
2020-09-18 00:13:05 +08: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>
|
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2020-09-18 00:13:05 +08:00
|
|
|
|
</section>
|