When creating maps in D3, your toolbox is extraordinarily light. The goal is to focus on creating data visualizations and remove the burden of heavy IDEs and map-making software. The building blocks are as follows:
Quick bootstrap: We will cover installing Node.js, npm, and a lightweight server.
TopoJSON: We will see the working of this tool that is used to manage and optimize geographic information.
Web browser as a development tool: We will learn how to use a modern web browser, capable of rendering SVG files with built-in development tools. Some examples of common web browsers are Chrome, Firefox, Safari, and IE 9+.
Installing sample code: We will take the first steps in getting to grips with coding.
Working with developer tools: We will get familiar with developer tools.
If you're already familiar with working in D3 or web development, go ahead and set up your workstation based on the following commands by jumping directly to the Step-by-step bootstrap section.
A detailed explanation of this chapter's concepts will be covered if further instructions are needed. By the end of the chapter, you should feel comfortable in using the Quick bootstrap section as a convention throughout the book. The following instructions assume that Node.js and npm are already installed on your system.
# Install a light webserver npm install -g http-server # Install topojson npm install -g topojson # Clone the sample code with included libraries git clone --depth=1 email@example.com:climboid/d3jsMaps.git # start the project cd d3jsMaps http-server
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Now, open your web browser to
http://localhost:8080/chapter-1/example-1.html, and you should see the following map:
The next section covers detailed instructions to set up your development environment to rapidly create visualizations in later chapters. By the end of the chapter, you will have a working environment for the rest of the book (an example of a map running and an initial look at tools used to create visualizations).
Technically, most of the content we will craft can render directly in the browser without the use of a web server. However, we highly recommend you not to go ahead with this approach. Running a web server in your local development environment is extremely easy and provides several benefits:
Geographic information, statistical data, and visualization code can be clearly separated into isolated files
API calls can be stubbed and simulated, allowing easy integration into a full-stack application in the future
For our choice of the web server and other tools in our toolbox, we will rely on a Node.js package named
Go to the website http://nodejs.org.
Open the downloaded package and follow the defaults.
node –v v0.10.26 # or something similar should return
Now that Node.js and npm are installed, we can proceed to install the web server. When the web server is installed globally with the
–g command, it becomes easily accessible throughout the system.
Once on the command line, enter the following:
npm install -g http-server
You can quickly verify the package was installed by typing the following:
Then open your web browser to
http://localhost:8080. The browser should display a list of all the files in the directory from where you initiated the command.
The topojson is a command-line utility used to create files in the TopoJSON-serialized format. The TopoJSON format will be discussed in detail in Chapter 6, Finding and Working with Geographic Data. The topojson utility is also installed via npm.
Note that we will use the syntax TopoJSON to refer to the JSON format and topojson to indicate the command-line utility.
We already have installed Node.JS and npm, so enter the following on the command line:
npm install -g topojson
If you see Version 1.x, it means you have successfully installed topojson.
topojson uses node-gyp that has several dependencies based on the operating system. Please go to http://github.com/TooTallNate/node-gyp for details.
If you're using Windows, the basic steps are as follows:
Install Python 2.x (3.x not supported at the time of writing this book)
Install Microsoft Visual Studio C++ 2012 for Desktop (Express)
Although any modern browser supports SVG and has some kind of console, we strongly recommend you to use Google Chrome for these examples. It comes bundled with developer tools that will allow you to very easily open, explore, and modify the code. If you are not using Google Chrome, please go to http://www.google.com/chrome and install Google Chrome.
Go to https://github.com/climboid/d3jsMaps and either clone the repo, if you are familiar with Git cloning, or simply download the zipped version. Once it is downloaded, make sure to extract the file if you have it zipped.
Use the command prompt or terminal to go to the directory where you downloaded your file. For instance, if you downloaded the file to your desktop, type in the following:
cd ~/Desktop/d3jsMaps http-server
The last command will launch the simple server we installed previously for the supplied sample code. This means that, if you open your browser and go to
http://localhost:8080/chapter-1/example-1.html, you should see a map of Europe, similar to the one shown earlier.
This icon opens a submenu. Click on Tools. Then click on Developer tools. A panel will open at the bottom of the browser, containing all the developer tools at your disposal.
Within developer tools, you have a series of tabs (Elements, Network, Sources, and so on). These tools are extremely valuable and will allow you to inspect different aspects of your code. For more information on the Chrome developer tools, please go to the link https://developer.chrome.com/devtools/docs/authoring-development-workflow.
<svg width="812" height="584">
If you click on the SVG item, you should see it expand and display the path tag. The path tag will have several numbers and characters tied to a
d attribute. These numbers are control points that draw the path. We will cover how the path is drawn in the next chapter and how path tags are used to create maps in Chapter 4, Creating a Map and Chapter 5, Click-click Boom! Applying Interactivity to Your Map.
We also want to draw your attention to how the HTML5 application loads the D3 library. Again in the Elements tag, after the SVG tag, you should see the
<script> tag pointing to D3.js and topojson:
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> <script src="http://d3js.org/topojson.v1.min.js"></script>
If you click on the path located inside the SVG tag, you will see a new panel called the CSS inspector or the styles inspector. It shows and controls all the styles that are applied to a selected element—in this case the path element.
These three components create a D3 visualization:
HTML5 (the SVG and path elements)
CSS (the styling of the HTML5 elements)
Creating maps and visualizations using these three components will be discussed and analyzed throughout the book.
This chapter reveals a quick glimpse of the steps for basic setup in order to have a well-organized code base to create maps with D3. You should become familiar with this setup because we will be using this convention throughout the book.
Let's jump right in!