2018-10-12 15:37:13 -04:00
---
title: Create a Bar for Each Data Point in the Set
---
2019-07-24 00:59:27 -07:00
# Create a Bar for Each Data Point in the Set
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2019-06-11 17:48:11 +01:00
To complete this challenge, you must make use of D3's ** .data()**, ** .enter()**, and ** .append()** methods.
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Hints
### Hint 1
2018-10-12 15:37:13 -04:00
2019-06-11 17:48:11 +01:00
As with the previous challenges, make use of D3's .data() method, passing **dataset** as an argument.
2019-07-24 00:59:27 -07:00
### Hint 2
2019-06-11 17:48:11 +01:00
Ensure that you follow using .data(arg) with .enter()
2019-07-24 00:59:27 -07:00
### Hint 3
2019-06-11 17:48:11 +01:00
Finally, to create and add the **rect** shape, make use of the .append() method, passing "rect" as an argument. Ensure that you enclose "rect" in quotation marks.
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2019-06-11 17:48:11 +01:00
2019-07-24 00:59:27 -07:00
```html
2019-06-11 17:48:11 +01:00
< 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
.data(dataset)
.enter()
.append("rect")
// Add your code above this line
.attr("x", 0)
.attr("y", 0)
.attr("width", 25)
.attr("height", 100);
< / script >
< / body >
```
2019-07-24 00:59:27 -07:00
< / details >