My Journey Using D3
Tags: javascript, datadrivendocuments
Published: 2019-07-23 06:43:25

So, I recently decided to explore the D3 library, and of course, I was using freecodecamp. D3 sounds like an awesome front end library which enables you do things from manipulating your DOM elements to creating visualizations using SVG and Canvas. From what I've seen so far it's an awesome library.

To use it, all you need is to reference the script from a CDN or include it in your project. Just one script, no configurations. Well, for now...
## An Example Then
Ok, the most basic thing you can do with this library is to manipulate DOM elements

```html
<body>
  <script>
    d3.select("body")
      .append("h1")
      .text("Hello D3");
  </script>
</body>
```
Here, we used D3's <code>select()</code> method to select the body element. Then add a h1 node and and the text "Hello D3" to that node.

A more involved example would be
```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("New Title")
  </script>
</body>
```
Here, the library selects the <code>body</code> tag and selects all <code>h2</code> elements in it. It then gets data from the array called <code>dataset</code>. The enter method then enters each array item into the DOM tree, where new <code><h2></code> tags are added with <code>innerText</code> called "New Title". Here, d3 actually selects elements that don't exist yet and appends text to them based on data

## How Cool Can it get?
Very cool infact. You can use JS functions, conditions, callbacks to apply styles to elements. In this simple example, we would add style conditionally based on magnitude of values.
```html
<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    d3.select("body").selectAll("h2") //h2 elements which don't exist yet
      .data(dataset)
      .enter()
      .append("h2")    //now they do
      .text((d) => (d + " USD"))
      .style("color", d => d < 20 ? "red" : "green")
  </script>
</body>
```
And the output is...
![The output](https://thepracticaldev.s3.amazonaws.com/i/3bqbij7air2406epyege.PNG)

## A simple bar chart.
Let's create a simple bar chart using data in an array and CSS styling
```html
<style>
  .bar {
    width: 25px;
    height: 100px;
    margin: 2px;
    display: inline-block;
    background-color: cornflowerblue;
  }
</style>
<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    d3.select("body").selectAll("div")
      .data(dataset)
      .enter()
      .append("div")
      .attr("class", "bar")
      .style("height", d => d * 10 + "px")
  </script>
</body>
```
Here, the following are happening:
1. The <code>.attr()</code> sets the HTML attribute for the selected div elements, in this case, the attribute is the <code>class</code> attribute.
2. The style is attached based on the class defined in the CSS block.
3. The callback in the style method sets the height and increases it by 10. The unit used here is the pixel.

Here's the output 
![A simple bar chart](https://thepracticaldev.s3.amazonaws.com/i/djn82l2rmbrr1x5ezis5.PNG)


So there you have it. A very simple example that sets you up and running using this awesome data visualization library. I'd be making more posts in the future using D3.