The era of big data is upon us! Advances in hardware have made it possible for computers to store, analyze, and transmit massive amounts of information in a way that was previously impossible. Data Science has become one of the most in-demand fields in the United States, companies are constantly coming up with new techniques to analyze customer information, and it seems like every day there are new ways to visualize all this data.
The era of big data is upon us! Advances in hardware have made it possible for computers to store, analyze, and transmit massive amounts of information in a way that was previously impossible. Data Science has become one of the most in-demand fields in the United States, companies are constantly coming up with new techniques to analyze customer information, and it seems like every day there are new ways to visualize all this data. D3 has become the most popular library used to create dynamic, interactive, data-driven visualizations on the web. Unlike many technologies previously used in data-viz, D3 leverages the power of combining SVG images with web browsers and JavaScript. In this chapter we'll discuss:
1. What is SVG?
1. What Makes D3 So Special?
1. This Book's Approach to Learning
## What is SVG?
One of the best ways to present your data is via an interactive graphic on the web. The advantage of this approach is that its interactivity allows creators to pack more information into a single visualization, while the ubiquity of the web allows anyone to instantly access it. Gone are the days of power point presentations, or worse yet, printing static images onto paper for handout. There are many ways to create a web-based interactive data visualization, but none is more popular than a JavaScript library called D3.js.
To understand why D3.js works so well, it's important to understand what SVG is and how it relates to D3. SVG stands for Scalable Vector Graphics, and it's a way to display shapes using mathematical directions/commands. Traditionally, the information for an image is stored in a grid, also called a `raster`. Each pixel of the image has a specific color:
To understand why D3.js works so well, it's important to understand what SVG is and how it relates to D3. SVG stands for Scalable Vector Graphics, and it's a way to display shapes using mathematical directions/commands. Traditionally, the information for an image is stored in a grid, also called a `raster`. Each square (called a pixel) of the image has a specific color:
@ -24,6 +30,8 @@ Once an SVG drawing command is written, a program needs to interpret command and
This was the massive breakthrough that allowed complex interactive data visualizations to be hosted on the web.
## What Makes D3 So Special?
D3.js came in at this point because writing the code to make complex Data Driven Documents (how D3 got its name) that linked SVG images with the big data that had become available on the internet was a difficult task. It rose to prominence during the Obama/Romney presidential debates as the New York times publishes a series of amazing visualizations. Check out some examples here:
@ -35,6 +43,12 @@ D3.js came in at this point because writing the code to make complex Data Driven
D3 simplifies some of the most common, as well as some of the most complex tasks that a developer can run into when creating browser-based visualizations. At it's core, D3 easily maps SVG image properties to data values. As the data values change, due to user interactions, so do the images.
## This Book's Approach to Learning
D3 is a massive library, full of millions of options, but its core concepts are easy to learn. One does not need to know every detail of the library in order to become a functional D3 developer. Instead, this book attempts to teach the most fundamental aspects of D3, so that the reader can get job-ready quickly. It does so by stepping the user through a series of the most common graphs that a developer will be asked to make: a scatter plot, a bar graph, a pie chart, a force directed graph, and a map. The goal is not only to teach the basics but also give the reader a final set of builds that are fun to work towards as well as useful to draw from as their career continues.
Please note, the code demonstrated here was created to be easy to understand from an educational standpoint. It is not meant to be code that is ready for production. Nor does it employ ES6 or ES7 syntax. Often times demonstrating a concept in code that is production-ready or written in ES6/ES7 can hinder the educational experience. It is assumed that the reader is comfortable enough with the core concepts of programming that they can refine the code on their own, once they are comfortable with the fundamentals of D3.
## Conclusion
In this chapter you've received a high-level overview of what makes D3 so interesting. In the next section, we'll dive into create SVG elements
`geoPath()` generates the function that we'll use for the `d` attribute, and `projection(worldProjection)` tells it to use the `worldProjection` var created earlier so that the `path` elements appear as an equirectangular projection
`geoPath()` generates the function that we'll use for the `d` attribute, and `projection(worldProjection)` tells it to use the `worldProjection` var created earlier so that the `path` elements appear as an equirectangular projection like this:
This lesson covers how to create various SVG elements, the foundation of D3.js
This lesson covers how to create various SVG elements, the foundation of D3.js. In it, we will discuss:
1. Base tags
1. Basic Elements
1. Positioning
1. Styling
1. Important SVG elements
## Base tag
@ -54,7 +61,7 @@ This tells the browser to give the circle a radius of 50px:
At the moment though, we only see the bottom right quarter of the `<circle>`. This is because the center of the `<circle>` is being drawn at the very upper left corner of the `<svg>`, and the rest of it is being clipped outside the `<svg>`. We can change this by changing the position of the circle, which we'll do next.
## Positioning
## Positioning an Element
- The `<svg>` tag is an inline element, like an image (as opposed to a block element like a `<div>`)
- Elements within the `<svg>` are positioned similar to photoshop, with a set of coordinates which follow the form (x,y). This is different from HTML, where elements are laid out relative to each other
@ -69,7 +76,7 @@ At the moment though, we only see the bottom right quarter of the `<circle>`. T
- -x moves left
- -y moves up
Let's adjust the position of our circle in our previous section by adjusting `cx` and `cy` values:
Let's adjust the position of our circle in our previous section by adjusting `cx` and `cy` values (the x and y values for the center of the element):
```html
<!DOCTYPE html>
@ -88,11 +95,11 @@ Now we see the full circle:

## Styling
## Styling Elements
Each tag inside an `<svg>` can be styled with various attributes:
- `fill=red` or `fill=#ff0000` will alter fill color
- `fill=red` or `fill=#ff0000` will alter the color that fills the shape
- `stroke=red` or `stroke=#ff0000` will alter stroke color. Stroke is a line that surrounds each element
- `stroke-width=4` will adjust the width of the stroke
- `fill-opacity=0.5` will adjust the transparency of the fill color
@ -176,10 +183,11 @@ To demo each element, we'll use the following code as a starting point and then
### Circle
- attributes
- `r` radius
- `cx` x position
- `cy` y position
Circles have the following attributes:
- `r` radius
- `cx` x position
- `cy` y position
```xml
<circler="50"cx="200"cy="300"/>
@ -283,6 +291,8 @@ What if we want to draw complex organic shapes? To do this, we'll need to use p