2018-10-10 22:03:03 +00:00
---
id: 587d7fa8367417b2b2512bcd
2021-02-06 04:42:36 +00:00
title: Create a Bar for Each Data Point in the Set
2018-10-10 22:03:03 +00:00
challengeType: 6
2020-09-17 16:13:05 +00:00
forumTopicId: 301482
2021-01-13 02:31:00 +00:00
dashedName: create-a-bar-for-each-data-point-in-the-set
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 last challenge added only one rectangle to the `svg` element to represent a bar. Here, you'll combine what you've learned so far about `data()` , `enter()` , and SVG shapes to create and append a rectangle for each data point in `dataset` .
2020-12-16 07:37:30 +00:00
2021-02-06 04:42:36 +00:00
A previous challenge showed the format for how to create and append a `div` for each item in `dataset` :
2020-09-17 16:13:05 +00:00
```js
d3.select("body").selectAll("div")
.data(dataset)
.enter()
.append("div")
```
2021-02-06 04:42:36 +00:00
There are a few differences working with `rect` elements instead of `divs` . The `rects` must be appended to an `svg` element, not directly to the `body` . Also, you need to tell D3 where to place each `rect` within the `svg` area. The bar placement will be covered in the next challenge.
2018-10-10 22:03:03 +00:00
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
Use the `data()` , `enter()` , and `append()` methods to create and append a `rect` for each item in `dataset` . The bars should display all on top of each other; this will be fixed in the next challenge.
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
Your document should have 9 `rect` elements.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert($('rect').length == 9);
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
Your code should use the `data()` method.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(code.match(/\.data/g));
```
2020-09-17 16:13:05 +00:00
2021-02-06 04:42:36 +00:00
Your code should use the `enter()` method.
2020-09-17 16:13:05 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(code.match(/\.enter/g));
```
2020-09-17 16:13:05 +00:00
2021-02-06 04:42:36 +00:00
Your code should use the `append()` method.
2020-09-17 16:13:05 +00:00
2020-12-16 07:37:30 +00:00
```js
assert(code.match(/\.append/g));
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")
// Add your code below this line
// Add your code above this line
.attr("x", 0)
.attr("y", 0)
.attr("width", 25)
.attr("height", 100);
< / 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", 0)
.attr("y", 0)
.attr("width", 25)
.attr("height", 100);
< / script >
< / body >
```