- What is SVG?
- What makes D3 so special?
- This book's approach to learning
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:
But with SVG, a set of succinct drawing directions is stored. For example, the drawing command for a circle is as follows:
This code produces a much smaller file size, and because it's a set of drawing directions, the image can be enlarged without any pixelation. A raster image becomes blurry and pixelated as it's enlarged. The advantage of raster graphics over vector graphics is that they're great for storing complex images such as photographs. With a photograph, where each pixel probably has a different color, it's better to use a raster image. Imagine writing SVG drawing commands for a photograph: you would end up creating a new element for each pixel, and the file size would be too large.
This was the massive breakthrough that allowed complex interactive data visualizations to be hosted on the web.
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 published a series of amazing visualizations. Check out some examples here:
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 its core, D3 easily maps SVG image properties to data values. As the data values change, due to user interactions, so do the images.
D3 is a massive library, full of millions of options, but its core concepts are easy to learn. You do not need to know every detail of the library 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 to give the reader a final set of builds that are fun to work, toward 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, 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.
Each chapter focuses on a specific build. The completed build code for each chapter can be found at: https://github.com/PacktPublishing/D3.js-Quick-Start-Guide.
- Cubic Bezier Curves:
The completed code for this section can be found here: https://github.com/PacktPublishing/D3.js-Quick-Start-Guide/tree/master/Chapter02.
The completed code for this section can be found here: https://github.com/PacktPublishing/D3.js-Quick-Start-Guide/tree/master/Chapter03.
- Create new points:
- Remove points:
- Update points:
- Zoom and pan:
The completed code for this section can be found here: https://github.com/PacktPublishing/D3.js-Quick-Start-Guide/tree/master/Chapter04.
The completed code for this section can be found here:https://github.com/PacktPublishing/D3.js-Quick-Start-Guide/tree/master/Chapter05.
Then we'll turn it into a donut chart:
And then we'll create functionality so that the user can remove a section of the chart and it will close the gap with a smooth transition:
The completed code for this section can be found here:https://github.com/PacktPublishing/D3.js-Quick-Start-Guide/tree/master/Chapter06.
The completed code for this section can be found here:https://github.com/PacktPublishing/D3.js-Quick-Start-Guide/tree/master/Chapter07.
In Chapter 8, Mapping, we'll learn how to use GeoJSON data to create a map of the world. It will look as follows:
The completed code for this section can be found here:https://github.com/PacktPublishing/D3.js-Quick-Start-Guide/tree/master/Chapter08.
For this book, you really only need to download and install the following:
- Chrome: https://www.google.com/chrome/.
- A web browser so that we can view our visualizations.
- Node: https://nodejs.org/en/.
- A code editor. I'd suggest Atom if you're new to coding: https://atom.io/.
In this chapter, you've received a high-level overview of what makes D3 so interesting. We examined what an SVG element is and set up our machine so that it is ready to create D3 code. We also took a look at this book's approach to learning and how it applies to the applications that we'll build. In Chapter 2, Using SVG to Create Images Using Code, we'll dive into creating SVG elements.