* chore(learn): audit d3 projects * chore: audit data vis * chore: audit json apis * Update curriculum/challenges/english/04-data-visualization/data-visualization-projects/visualize-data-with-a-scatterplot-graph.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: apply suggestions Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: no colour backticks Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/04-data-visualization/json-apis-and-ajax/get-geolocation-data-to-find-a-users-gps-coordinates.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
92 lines
2.0 KiB
Markdown
92 lines
2.0 KiB
Markdown
---
|
|
id: 587d7fa7367417b2b2512bc8
|
|
title: Add Classes with D3
|
|
challengeType: 6
|
|
forumTopicId: 301473
|
|
dashedName: add-classes-with-d3
|
|
---
|
|
|
|
# --description--
|
|
|
|
Using a lot of inline styles on HTML elements gets hard to manage, even for smaller apps. It's easier to add a class to elements and style that class one time using CSS rules. D3 has the `attr()` method to add any HTML attribute to an element, including a class name.
|
|
|
|
The `attr()` method works the same way that `style()` does. It takes comma-separated values, and can use a callback function. Here's an example to add a class of `container` to a selection:
|
|
|
|
`selection.attr("class", "container");`
|
|
|
|
Note that the `class` parameter will remain the same whenever you need to add a class and only the `container` parameter will change.
|
|
|
|
# --instructions--
|
|
|
|
Add the `attr()` method to the code in the editor and put a class of `bar` on the `div` elements.
|
|
|
|
# --hints--
|
|
|
|
Your `div` elements should have a class of `bar`.
|
|
|
|
```js
|
|
assert($('div').attr('class') == 'bar');
|
|
```
|
|
|
|
Your code should use the `attr()` method.
|
|
|
|
```js
|
|
assert(code.match(/\.attr/g));
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```html
|
|
<style>
|
|
.bar {
|
|
width: 25px;
|
|
height: 100px;
|
|
display: inline-block;
|
|
background-color: blue;
|
|
}
|
|
</style>
|
|
<body>
|
|
<script>
|
|
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
|
|
|
|
d3.select("body").selectAll("div")
|
|
.data(dataset)
|
|
.enter()
|
|
.append("div")
|
|
// Add your code below this line
|
|
|
|
|
|
|
|
// Add your code above this line
|
|
</script>
|
|
</body>
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```html
|
|
<style>
|
|
.bar {
|
|
width: 25px;
|
|
height: 100px;
|
|
display: inline-block;
|
|
background-color: blue;
|
|
}
|
|
</style>
|
|
<body>
|
|
<script>
|
|
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
|
|
|
|
d3.select("body").selectAll("div")
|
|
.data(dataset)
|
|
.enter()
|
|
.append("div")
|
|
// Add your code below this line
|
|
.attr("class","bar");
|
|
// Add your code above this line
|
|
</script>
|
|
</body>
|
|
```
|