2018-10-10 18:03:03 -04:00
---
id: 587d7fa9367417b2b2512bd0
2021-02-06 04:42:36 +00:00
title: Invert SVG Elements
2018-10-10 18:03:03 -04:00
challengeType: 6
2020-09-18 00:13:05 +08:00
forumTopicId: 301488
2021-01-13 03:31:00 +01:00
dashedName: invert-svg-elements
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
You may have noticed the bar chart looked like it's upside-down, or inverted. This is because of how SVG uses (x, y) coordinates.
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
In SVG, the origin point for the coordinates is in the upper-left corner. An `x` coordinate of 0 places a shape on the left edge of the SVG area. A `y` coordinate of 0 places a shape on the top edge of the SVG area. Higher `x` values push the rectangle to the right. Higher `y` values push the rectangle down.
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
To make the bars right-side-up, you need to change the way the `y` coordinate is calculated. It needs to account for both the height of the bar and the total height of the SVG area.
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The height of the SVG area is 100. If you have a data point of 0 in the set, you would want the bar to start at the bottom of the SVG area (not the top). To do this, the `y` coordinate needs a value of 100. If the data point value were 1, you would start with a `y` coordinate of 100 to set the bar at the bottom. Then you need to account for the height of the bar of 1, so the final `y` coordinate would be 99.
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The `y` coordinate that is `y = heightOfSVG - heightOfBar` would place the bars right-side-up.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Change the callback function for the `y` attribute to set the bars right-side-up. Remember that the `height` of the bar is 3 times the data value `d` .
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
**Note**
In general, the relationship is `y = h - m * d` , where `m` is the constant that scales the data points.
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
2021-02-06 04:42:36 +00:00
The first `rect` should have a `y` value of 64.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert($('rect').eq(0).attr('y') == h - dataset[0] * 3);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The second `rect` should have a `y` value of 7.
2020-12-16 00:37:30 -07:00
```js
assert($('rect').eq(1).attr('y') == h - dataset[1] * 3);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The third `rect` should have a `y` value of 34.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert($('rect').eq(2).attr('y') == h - dataset[2] * 3);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The fourth `rect` should have a `y` value of 49.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert($('rect').eq(3).attr('y') == h - dataset[3] * 3);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The fifth `rect` should have a `y` value of 25.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert($('rect').eq(4).attr('y') == h - dataset[4] * 3);
```
2020-09-18 00:13:05 +08:00
2021-02-06 04:42:36 +00:00
The sixth `rect` should have a `y` value of 46.
2020-09-18 00:13:05 +08:00
2020-12-16 00:37:30 -07:00
```js
assert($('rect').eq(5).attr('y') == h - dataset[5] * 3);
```
2021-02-06 04:42:36 +00:00
The seventh `rect` should have a `y` value of 13.
2020-12-16 00:37:30 -07:00
```js
assert($('rect').eq(6).attr('y') == h - dataset[6] * 3);
```
2020-09-18 00:13:05 +08:00
2021-02-06 04:42:36 +00:00
The eighth `rect` should have a `y` value of 58.
2020-09-18 00:13:05 +08:00
2020-12-16 00:37:30 -07:00
```js
assert($('rect').eq(7).attr('y') == h - dataset[7] * 3);
2018-10-10 18:03:03 -04:00
```
2020-08-13 17:24:35 +02:00
2021-02-06 04:42:36 +00:00
The ninth `rect` should have a `y` value of 73.
2020-12-16 00:37:30 -07:00
```js
assert($('rect').eq(8).attr('y') == h - dataset[8] * 3);
```
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", (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 >
```
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", (d, i) => h - 3 * d)
.attr("width", 25)
.attr("height", (d, i) => 3 * d);
< / script >
< / body >
```