2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7fa9367417b2b2512bcf
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 动态更改每个条的高度
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 6
|
2020-09-18 00:13:05 +08:00
|
|
|
forumTopicId: 301486
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: dynamically-change-the-height-of-each-bar
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
|
|
|
和动态设置 `x` 值一样,每组的高也可以被设置成数组中数据点的值。
|
2020-09-18 00:13:05 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
selection.attr("property", (d, i) => {
|
|
|
|
/*
|
|
|
|
* d is the data point value
|
|
|
|
* i is the index of the data point in the array
|
|
|
|
*/
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
改变 `height` 属性的回调函数,让它返回数据值乘以 3 的值。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
**提示**
|
|
|
|
记住,把所有数据点乘以相同的常数来对数据进行缩放(就像放大)。这有利于看清例子中每组之间的差异。
|
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
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第一个 `rect` 的 `height` 应该为 36。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert($('rect').eq(0).attr('height') == '36');
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第二个 `rect` 的 `height` 应该为 93。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert($('rect').eq(1).attr('height') == '93');
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第三个 `rect` 的 `height` 应该为 66。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert($('rect').eq(2).attr('height') == '66');
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第四个 `rect` 的 `height` 应该为 51。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert($('rect').eq(3).attr('height') == '51');
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第五个 `rect` 的 `height` 应该为 75。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert($('rect').eq(4).attr('height') == '75');
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第六个 `rect` 的 `height` 应该为 54。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert($('rect').eq(5).attr('height') == '54');
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第七个 `rect` 的 `height` 应该为 87。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert($('rect').eq(6).attr('height') == '87');
|
|
|
|
```
|
2020-09-18 00:13:05 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第八个 `rect` 的 `height` 应该为 42。
|
2020-09-18 00:13:05 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert($('rect').eq(7).attr('height') == '42');
|
|
|
|
```
|
2020-09-18 00:13:05 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第九个 `rect` 的 `height` 应该为 27。
|
2020-09-18 00:13:05 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert($('rect').eq(8).attr('height') == '27');
|
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>
|
|
|
|
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
|
|
|
|
|
|
|
|
const w = 500;
|
|
|
|
const h = 100;
|
|
|
|
|
|
|
|
const svg = d3.select("body")
|
|
|
|
.append("svg")
|
|
|
|
.attr("width", w)
|
|
|
|
.attr("height", h);
|
|
|
|
|
|
|
|
svg.selectAll("rect")
|
|
|
|
.data(dataset)
|
|
|
|
.enter()
|
|
|
|
.append("rect")
|
|
|
|
.attr("x", (d, i) => i * 30)
|
|
|
|
.attr("y", 0)
|
|
|
|
.attr("width", 25)
|
|
|
|
.attr("height", (d, i) => {
|
|
|
|
// Add your code below this line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add your code above this line
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```html
|
|
|
|
<body>
|
|
|
|
<script>
|
|
|
|
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
|
|
|
|
|
|
|
|
const w = 500;
|
|
|
|
const h = 100;
|
|
|
|
|
|
|
|
const svg = d3.select("body")
|
|
|
|
.append("svg")
|
|
|
|
.attr("width", w)
|
|
|
|
.attr("height", h);
|
|
|
|
|
|
|
|
svg.selectAll("rect")
|
|
|
|
.data(dataset)
|
|
|
|
.enter()
|
|
|
|
.append("rect")
|
|
|
|
.attr("x", (d, i) => i * 30)
|
|
|
|
.attr("y", 0)
|
|
|
|
.attr("width", 25)
|
|
|
|
.attr("height", (d, i) => {
|
|
|
|
return d * 3
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
```
|