2021-06-15 00:49:18 -07:00
---
id: 587d7fa9367417b2b2512bd0
2021-07-03 20:07:10 +05:30
title: Invertire elementi SVG
2021-06-15 00:49:18 -07:00
challengeType: 6
forumTopicId: 301488
dashedName: invert-svg-elements
---
# --description--
2021-07-03 20:07:10 +05:30
Potresti aver notato che il grafico a barre è sotto soprra, o invertito. Questo è causato da come SVG usa le coordinate (x, y).
2021-06-15 00:49:18 -07:00
2021-07-03 20:07:10 +05:30
In SVG, il punto di origine per le coordinate è l'angolo in alto a sinistra. Una coordinata `x` di 0 posiziona la figura sul lato sinistro dell'area SVG. Una coordinata `y` di 0 posiziona la figura sul lato in alto dell'area SVG. Valori di `x` più alti spingono il rettangolo verso destra. Valori di `y` più alti spingono il rettangolo verso il basso.
2021-06-15 00:49:18 -07:00
2021-07-03 20:07:10 +05:30
Per fare le barre nella giusta direzione, devi cambiare come è calcolata la coordinata `y` . Bisogna considerare sia l'altezza della barra sia l'altezza totale dell'area SVG.
2021-06-15 00:49:18 -07:00
2021-07-03 20:07:10 +05:30
L'altezza dell'area SVG è 100. Se hai un valore di 0 nel tuo set, vuoi che la barra inizi al lato più in basso dell'area SVG (non in alto). Per fare ciò, la coordinata `y` deve avere un valore di 100. Se il valore fosse 1, inizieresti con una coordinata `y` di 100 per far iniziare la barra in basso. Poi avresti bisogno di considerare l'altezza della barra di 1, quindi la coordinata `y` finale sarebbe 99.
2021-06-15 00:49:18 -07:00
2021-07-03 20:07:10 +05:30
Una coordinata `y` calcolata come `y = heightOfSVG - heightOfBar` crea le barre nella direzione giusta.
2021-06-15 00:49:18 -07:00
# --instructions--
2021-07-03 20:07:10 +05:30
Cambia la funzione callback per l'attributo `y` per raddrizzare le barre. Ricorda che l'altezza (`height` ) della barra è 3 volte il valore `d` .
2021-06-15 00:49:18 -07:00
2021-07-03 20:07:10 +05:30
**Nota:** In generale, la relazione è `y = h - m * d` , dove `m` è la costante che moltiplica i dati.
2021-06-15 00:49:18 -07:00
# --hints--
2021-07-05 12:27:02 +05:30
Il primo `rect` dovrebbe avere un valore `y` di `64` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(0).attr('y') == h - dataset[0] * 3);
```
2021-07-03 20:07:10 +05:30
Il secondo `rect` dovrebbe avere un valore `y` di `7` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(1).attr('y') == h - dataset[1] * 3);
```
2021-07-03 20:07:10 +05:30
Il terzo `rect` dovrebbe avere un valore `y` di `34` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(2).attr('y') == h - dataset[2] * 3);
```
2021-07-03 20:07:10 +05:30
Il quarto `rect` dovrebbe avere un valore `y` di `49` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(3).attr('y') == h - dataset[3] * 3);
```
2021-07-03 20:07:10 +05:30
Il quinto `rect` dovrebbe avere un valore `y` di `25` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(4).attr('y') == h - dataset[4] * 3);
```
2021-07-03 20:07:10 +05:30
Il sesto `rect` dovrebbe avere un valore `y` di `46` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(5).attr('y') == h - dataset[5] * 3);
```
2021-07-03 20:07:10 +05:30
Il settimo `rect` dovrebbe avere un valore `y` di `13` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(6).attr('y') == h - dataset[6] * 3);
```
2021-07-03 20:07:10 +05:30
L'ottavo `rect` dovrebbe avere un valore `y` di `58` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(7).attr('y') == h - dataset[7] * 3);
```
2021-07-03 20:07:10 +05:30
Il nono `rect` dovrebbe avere un valore `y` di `73` .
2021-06-15 00:49:18 -07:00
```js
assert($('rect').eq(8).attr('y') == h - dataset[8] * 3);
```
# --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", (d, i) => {
// Add your code below this line
// Add your code above this line
})
.attr("width", 25)
.attr("height", (d, i) => 3 * d);
< / 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", (d, i) => h - 3 * d)
.attr("width", 25)
.attr("height", (d, i) => 3 * d);
< / script >
< / body >
```