Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hints+Solution - 'Create a Bar for Each Data Point in the Set' #29527

Merged
merged 2 commits into from
Jun 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,47 @@ title: Create a Bar for Each Data Point in the Set
---
## Create a Bar for Each Data Point in the Set

This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/data-visualization/data-visualization-with-d3/create-a-bar-for-each-data-point-in-the-set/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
To complete this challenge, you must make use of D3's **.data()**, **.enter()**, and **.append()** methods.

<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>.
## Hint 1

<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
As with the previous challenges, make use of D3's .data() method, passing **dataset** as an argument.

## Hint 2

Ensure that you follow using .data(arg) with .enter()

## Hint 3

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.

## Solution (!!Spoiler Alert!!)

```
<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>
```