Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning D3.js Mapping
Learning D3.js Mapping

Learning D3.js Mapping: Build stunning maps and visualizations using D3.js

R$120.99 R$80.00
Book Dec 2014 126 pages 1st Edition
eBook
R$120.99 R$80.00
Print
R$150.99
Subscription
Free Trial
eBook
R$120.99 R$80.00
Print
R$150.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Dec 29, 2014
Length 126 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781783985609
Category :
Table of content icon View table of contents Preview book icon Preview Book

Learning D3.js Mapping

Chapter 1. Gather Your Cartographer's Toolbox

Welcome to the world of cartography with D3. In this chapter, you will be given all the tools you need to create a map using D3. These tools exist freely and openly, thanks to the wonderful world of open source. Given that we are going to be speaking in terms of the Web, our languages will be HTML, CSS, and JavaScript. After reading this book, you will be able to use all three languages effectively in order to create maps on your own.

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+.

    Note

    All screenshots and commands in this book have been done using Chrome. Therefore, we strongly recommend you to use this browser.

  • 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.

Quick bootstrap


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.

Type in the following in the command line:

# 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 git@github.com:climboid/d3jsMaps.git

# start the project
cd d3jsMaps
http-server

Tip

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:

Step-by-step bootstrap


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).

A lightweight web server

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

  • It will prevent common mistakes when making AJAX calls to fetch geographic and statistical data (for example, the same-origin policy)

For our choice of the web server and other tools in our toolbox, we will rely on a Node.js package named http-server. Node.js is a platform built on Chrome's JavaScript runtime, which is used to build fast, scalable network applications. The platform includes Node Package Manager (npm), which was created by other members of the vibrant Node.js community and allows the developer to quickly install packages of prebuilt software.

To install Node.js, simply perform the following steps:

  1. Go to the website http://nodejs.org.

  2. Click on the INSTALL button.

  3. Open the downloaded package and follow the defaults.

To test the installation, type in the following in the command line:

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:

http-server

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.

Optimizing geographic data files with TopoJSON


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

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

Once the installation is complete, you should check the version of topojson installed on your machine just as we did with Node.js:

topojson –version

If you see Version 1.x, it means you have successfully installed topojson.

Tip

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)

Using the web browser as a development tool


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.

Installing the sample code

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.

Working with the developer tools

It's time to open the developer tools. On the top-right corner of the browser, you will see the icon as shown in the following screenshot:

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.

Note

The option names mentioned here might differ according to the version of Chrome you are using.

Tip

For quick access to developer tools on the Mac, use alt + command + I; for Windows PCs, use Ctrl + Shift + I.

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.

Since we are going to focus on the Elements tab, click on it if it is not already selected.

You should see something similar to the preceding screenshot; it will have the following code statement:

<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)

  • JavaScript (the D3.js library and map code)

  • CSS (the styling of the HTML5 elements)

Creating maps and visualizations using these three components will be discussed and analyzed throughout the book.

Summary


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.

The remaining chapters will focus on creating detailed maps and achieving realistic visualizations through HTML, JavaScript, and CSS.

Let's jump right in!

Left arrow icon Right arrow icon

Key benefits

What you will learn

Access data resources to make maps and learn how to modify structures Render your maps on a browser Style your maps according to your needs and bind events to maps to make them interactive Tie paths to the geospatial data to outline an SVG map Use Chrome Dev Tools in order to inspect created code Fetch data through AJAX calls with the assistance of the D3.js library Work with data structures and compose blocks of logic into reusable functions Troubleshoot your code

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Dec 29, 2014
Length 126 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781783985609
Category :

Table of Contents

14 Chapters
Learning D3.js Mapping Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Authors Chevron down icon Chevron up icon
About the Reviewers Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
Gather Your Cartographer's Toolbox Chevron down icon Chevron up icon
Creating Images from Simple Text Chevron down icon Chevron up icon
Producing Graphics from Data – the Foundations of D3 Chevron down icon Chevron up icon
Creating a Map Chevron down icon Chevron up icon
Click-click Boom! Applying Interactivity to Your Map Chevron down icon Chevron up icon
Finding and Working with Geographic Data Chevron down icon Chevron up icon
Testing Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.