2018-10-10 22:03:03 +00:00
---
id: 587d7fa9367417b2b2512bcf
2021-02-06 04:42:36 +00:00
title: Dynamically Change the Height of Each Bar
2018-10-10 22:03:03 +00:00
challengeType: 6
2020-09-17 16:13:05 +00:00
forumTopicId: 301486
2021-01-13 02:31:00 +00:00
dashedName: dynamically-change-the-height-of-each-bar
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-02-06 04:42:36 +00:00
The height of each bar can be set to the value of the data point in the array, similar to how the `x` value was set dynamically.
2020-09-17 16:13:05 +00: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 07:37:30 +00:00
# --instructions--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
Change the callback function for the `height` attribute to return the data value times 3.
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
**Note**
Remember that multiplying all data points by the same constant scales the data (like zooming in). It helps to see the differences between bar values in this example.
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-02-06 04:42:36 +00:00
The first `rect` should have a `height` of 36.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert($('rect').eq(0).attr('height') == '36');
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
The second `rect` should have a `height` of 93.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert($('rect').eq(1).attr('height') == '93');
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
The third `rect` should have a `height` of 66.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert($('rect').eq(2).attr('height') == '66');
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
The fourth `rect` should have a `height` of 51.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert($('rect').eq(3).attr('height') == '51');
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
The fifth `rect` should have a `height` of 75.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert($('rect').eq(4).attr('height') == '75');
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
The sixth `rect` should have a `height` of 54.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert($('rect').eq(5).attr('height') == '54');
```
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
The seventh `rect` should have a `height` of 87.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert($('rect').eq(6).attr('height') == '87');
```
2020-09-17 16:13:05 +00:00
2021-02-06 04:42:36 +00:00
The eighth `rect` should have a `height` of 42.
2020-09-17 16:13:05 +00:00
2020-12-16 07:37:30 +00:00
```js
assert($('rect').eq(7).attr('height') == '42');
```
2020-09-17 16:13:05 +00:00
2021-02-06 04:42:36 +00:00
The ninth `rect` should have a `height` of 27.
2020-09-17 16:13:05 +00:00
2020-12-16 07:37:30 +00:00
```js
assert($('rect').eq(8).attr('height') == '27');
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 >
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 07:37:30 +00:00
# --solutions--
2021-01-13 02:31:00 +00: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 >
```