2021-06-15 00:49:18 -07:00
---
id: 587d7fa9367417b2b2512bcf
2021-07-03 20:07:10 +05:30
title: Cambiare dinamicamente l'altezza di ogni barra
2021-06-15 00:49:18 -07:00
challengeType: 6
forumTopicId: 301486
dashedName: dynamically-change-the-height-of-each-bar
---
# --description--
2021-07-03 20:07:10 +05:30
L'altezza di ogni barra può essere impostata al valore del punto dati nell'array, in modo simile a come il valore `x` è stato impostato dinamicamente.
2021-06-15 00:49:18 -07:00
```js
selection.attr("property", (d, i) => {
})
```
2021-07-03 20:07:10 +05:30
Qui `d` sarebbe il valore del punto dati, e `i` sarebbe l'indice del punto di dati nell'array.
2021-06-15 00:49:18 -07:00
# --instructions--
2021-07-03 20:07:10 +05:30
Modifica la funzione di callback per l'attributo altezza `height` in modo che restituisca i valori dei dati moltiplicati per 3.
2021-06-15 00:49:18 -07:00
2021-07-03 20:07:10 +05:30
**Nota:** Ricorda che moltiplicando tutti i punti dati per la stessa costante riscala i dati (come nel fare uno zoom). In questo esempio aiuta a vedere le differenze tra i valori della barra.
2021-06-15 00:49:18 -07:00
# --hints--
2021-07-03 20:07:10 +05:30
Il primo `rect` dovrebbe avere un'altezza `height` di `36` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(0).attr('height') == '36');
```
2021-07-03 20:07:10 +05:30
Il secondo `rect` dovrebbe avere un'altezza `height` di `93` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(1).attr('height') == '93');
```
2021-07-03 20:07:10 +05:30
Il terzo `rect` dovrebbe avere un'altezza `height` di `66` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(2).attr('height') == '66');
```
2021-07-03 20:07:10 +05:30
Il quarto `rect` dovrebbe avere un'altezza `height` di `51` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(3).attr('height') == '51');
```
2021-07-03 20:07:10 +05:30
Il quinto `rect` dovrebbe avere un'altezza `height` di `75` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(4).attr('height') == '75');
```
2021-07-03 20:07:10 +05:30
Il sesto`rect` dovrebbe avere un'altezza `height` di `54` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(5).attr('height') == '54');
```
2021-07-03 20:07:10 +05:30
Il settimo `rect` dovrebbe avere un'altezza `height` di `87` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(6).attr('height') == '87');
```
2021-07-03 20:07:10 +05:30
L'ottavo `rect` dovrebbe avere un'altezza `height` di `42` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(7).attr('height') == '42');
```
2021-07-03 20:07:10 +05:30
Il nono `rect` dovrebbe avere un'altezza `height` di `27` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(8).attr('height') == '27');
```
# --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 >
```
# --solutions--
```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 >
```