2018-10-10 18:03:03 -04:00
---
id: 587d7fa7367417b2b2512bc7
2021-02-06 04:42:36 +00:00
title: Change Styles Based on Data
2018-10-10 18:03:03 -04:00
challengeType: 6
2020-09-18 00:13:05 +08:00
forumTopicId: 301479
2021-01-13 03:31:00 +01:00
dashedName: change-styles-based-on-data
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
D3 is about visualization and presentation of data. It's likely you'll want to change the styling of elements based on the data. You can use a callback function in the `style()` method to change the styling for different elements.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
For example, you may want to color a data point blue if it has a value less than 20, and red otherwise. You can use a callback function in the `style()` method and include the conditional logic. The callback function uses the `d` parameter to represent the data point:
2020-09-18 00:13:05 +08:00
```js
selection.style("color", (d) => {
/* Logic that returns the color based on a condition */
});
```
2021-02-06 04:42:36 +00:00
The `style()` method is not limited to setting the `color` - it can be used with other CSS properties as well.
2020-12-16 00:37:30 -07:00
# --instructions--
2021-02-06 04:42:36 +00:00
Add the `style()` method to the code in the editor to set the `color` of the `h2` elements conditionally. Write the callback function so if the data value is less than 20, it returns "red", otherwise it returns "green".
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
**Note**
You can use if-else logic, or the ternary operator.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2021-02-06 04:42:36 +00:00
The first `h2` should have a `color` of red.
2020-12-16 00:37:30 -07:00
```js
assert($('h2').eq(0).css('color') == 'rgb(255, 0, 0)');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The second `h2` should have a `color` of green.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert($('h2').eq(1).css('color') == 'rgb(0, 128, 0)');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The third `h2` should have a `color` of green.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert($('h2').eq(2).css('color') == 'rgb(0, 128, 0)');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The fourth `h2` should have a `color` of red.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert($('h2').eq(3).css('color') == 'rgb(255, 0, 0)');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The fifth `h2` should have a `color` of green.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert($('h2').eq(4).css('color') == 'rgb(0, 128, 0)');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The sixth `h2` should have a `color` of red.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert($('h2').eq(5).css('color') == 'rgb(255, 0, 0)');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The seventh `h2` should have a `color` of green.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert($('h2').eq(6).css('color') == 'rgb(0, 128, 0)');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The eighth `h2` should have a `color` of red.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert($('h2').eq(7).css('color') == 'rgb(255, 0, 0)');
```
2020-09-18 00:13:05 +08:00
2021-02-06 04:42:36 +00:00
The ninth `h2` should have a `color` of red.
2020-09-18 00:13:05 +08:00
2020-12-16 00:37:30 -07:00
```js
assert($('h2').eq(8).css('color') == 'rgb(255, 0, 0)');
2018-10-10 18:03:03 -04:00
```
2020-08-13 17:24:35 +02:00
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];
d3.select("body").selectAll("h2")
.data(dataset)
.enter()
.append("h2")
.text((d) => (d + " USD"))
// Add your code below this line
// Add your code above this line
< / 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];
d3.select("body").selectAll("h2")
.data(dataset)
.enter()
.append("h2")
.text((d) => (d + " USD"))
.style("color", (d) => d < 20 ? " red " : " green " )
< / script >
< / body >
```