Data Visualization with D3 4.x Cookbook - Nick Zhu - E-Book

Data Visualization with D3 4.x Cookbook E-Book

Nick Zhu

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Master D3.js and create amazing visualizations with the Data Visualization with D3 4.x Cookbook. Written by professional data engineer Nick Zhu, this D3.js cookbook features over 65 recipes. ? Solve real-world visualization problems using D3.js practical recipes ? Understand D3 fundamentals ? Includes illustrations, ready-to-go code samples and pre-built chart recipes

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 360

Veröffentlichungsjahr: 2017

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Data Visualization with D3 4.x Cookbook - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Why subscribe?
Customer Feedback
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Getting Started with D3.js
Introduction
Setting up a simple D3 development environment
Getting ready
How to do it...
How it works...
There's more...
How to get source code
Setting up an NPM-based development environment
Getting ready
How to do it...
How it works...
There's more...
Setting up a local HTTP server
Python Simple HTTP server
Node.js HTTP server
Understanding D3-style JavaScript
Getting ready
How to do it...
How it works...
Functions are objects
Static variable scoping
Getter-setter function
Function chaining
There's more...
Finding and sharing code
How to get help
2. Be Selective
Introduction
Introducing selection
CSS3 selector basics
Selecting a single element
Getting ready
How to do it...
How it works...
Selecting multiple elements
Getting ready
How to do it...
How it works...
Iterating through a selection
Getting ready
How to do it...
How it works...
Performing subselection
Getting ready
How to do it...
How it works...
Function chaining
Getting ready
How to do it...
How it works...
Manipulating the raw selection
Getting ready
How to do it...
How it works...
3. Dealing with Data
Introduction
The enter-update-exit pattern
Binding an array as data
Getting ready
How to do it...
How it works...
Binding object literals as data
Getting ready
How to do it...
How it works...
Binding functions as data
Getting ready
How to do it...
How it works...
Working with arrays
Getting ready
How to do it...
How it works...
Filtering with data
Getting ready
How to do it...
How it works...
Sorting with data
Getting ready
How to do it...
How it works...
Loading data from a server
Getting ready
How to do it...
How it works...
Asynchronous data loading using queue
Getting ready
How to do it...
How it works...
4. Tipping the Scales
Introduction
What are scales?
Using continuous scales
Getting ready
How to do it...
How it works...
Linear scale
Power scale
Log scale
Using the time scale
Getting ready
How to do it...
How it works...
There's more...
See also
Using the ordinal scale
Getting ready
How to do it...
How it works...
Interpolating a string
Interpolator
Getting ready
How to do it...
How it works...
There's more...
Interpolating colors
Getting ready
How to do it...
How it works...
See also
Interpolating compound objects
Getting ready
How to do it...
How it works...
5. Playing with Axes
Introduction
Working with basic axes
Getting ready
How to do it...
How it works...
Customizing ticks
Getting ready
How to do it...
How it works...
Drawing grid lines
Getting ready
How to do it...
How it works...
Dynamic rescaling of axes
Getting ready
How to do it...
How it works...
6. Transition with Style
Introduction
What is Transition?
Animating a single element
Getting ready
How to do it...
How it works...
Animating multiple elements
Getting ready
How to do it...
How it works...
Using ease
Getting Ready
How to do it...
How it works...
Using tweening
Getting ready
How to do it...
How it works...
There's more...
Using transition chaining
Getting ready
How to do it...
How it works...
Using transition filter
Getting ready
How to do it...
How it works...
See also
Listening to transitional events
Getting ready
How to do it...
How it works...
Working with timer
Getting ready
How to do it...
How it works...
See also
7. Getting into Shape
Introduction
What is SVG?
Vector
Scalability
Creating simple shapes
Getting ready
How to do it...
How it works...
There's more...
D3 SVG shape generators
See also
Using a line generator
Getting ready
How to do it...
How it works...
See also
Using line curve
Getting ready
How to do it...
How it works...
See Also
Changing line tension
Getting ready
How to do it...
How it works...
Using an area generator
Getting ready
How to do it...
How it works...
Using area curve
Getting ready
How to do it...
How it works...
There's more...
See also
Using an arc generator
Getting ready
How to do it...
How it works...
See also
Implementing arc transition
Getting ready
How to do it...
How it works...
There's more...
See also
8. Chart Them Up
Introduction
D3 chart convention
Creating a line chart
Getting ready
How to do it...
How it works...
Chart object and attributes
Chart body frame rendering
Render axes
Render data series
Creating an area chart
Getting ready
How to do it...
How it works...
Creating a scatterplot chart
Getting ready
How to do it...
How it works...
Creating a bubble chart
Getting ready
How to do it...
How it works...
Creating a bar chart
Getting ready
How to do it...
How it works...
See also
9. Lay Them Out
Introduction
Building a pie chart
Getting ready
How to do it...
How it works...
There's more...
See also
Building a stacked area chart
Getting ready
How to do it...
How it works...
There's more...
Expanded area chart
Streamgraph
See also
Building a treemap
Getting ready
How to do it...
How it works...
See also
Building a tree
Getting ready
How to do it...
How it works...
See also
Building an enclosure diagram
Getting ready
How to do it...
How it works...
See also
10. Interacting with Your Visualization
Introduction
Interacting with mouse events
Getting ready
How to do it...
How it works...
There's more...
See also
Interacting with a multi-touch device
Getting ready
How to do it...
How it works...
There's more...
See also
Implementing zoom and pan behavior
Getting ready
How to do it...
How it works...
There's more...
See also
Implementing drag behavior
Getting ready
How to do it...
How it works...
There's more...
See also
11. Using Force
Introduction
Using gravity and charge
Getting ready
How to do it...
How it works...
Alpha decay
Velocity decay
Charge
Positioning
Collision
Setting up zero force layout
Setting up mutual repulsion
Setting up gravity
Setting up positioning with gravity
Setting up positioning with repulsion
See also
Customizing velocity
Getting ready
How to do it...
How it works...
See also
Setting the link constraint
Getting ready
How to do it...
How it works...
See also
Using force to assist visualization
Getting ready
How to do it...
How it works...
See also
Manipulating force
Getting ready
How to do it...
How it works...
See also
Building a force-directed graph
Getting ready
How to do it...
How it works...
See also
12. Knowing Your Map
Introduction
Projecting the US map
GeoJSON
TopoJSON
Getting ready
How to do it...
How it works...
See also
Projecting the world map
Getting ready
How to do it...
How it works...
See also
Building a choropleth map
Getting ready
How to do it...
How it works...
See also
13. Test Drive Your Visualization
Introduction
Introduction to unit testing
Getting Jasmine and setting up the test environment
Getting ready
How to do it...
How it works...
See also
Test driving your visualization - chart creation
Getting ready
How to do it...
How it works...
Test driving your visualization - SVG rendering
Getting ready
How to do it...
How it works...
Test driving your visualization - pixel-perfect bar rendering
Getting ready
How to do it...
How it works...
See also
Appendix. Building Interactive Analytics in Minutes
Introduction
The crossfilter.js library
How to do it...
How it works...
There's more...
See also
Dimensional charting - dc.js
Getting ready
How to do it...
How it works...
There's more...
See also

Data Visualization with D3 4.x Cookbook - Second Edition

Data Visualization with D3 4.x Cookbook - Second Edition

Copyright © 2017 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: October 2013

Second edition: February 2017

Production reference: 1220217

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham 

B3 2PB, UK.

ISBN 978-1-78646-825-3

www.packtpub.com

Credits

Author

Nick Zhu

Copy Editor

Dhanya Baburaj

Reviewer

Scott Becker

Project Coordinator

Ritika Manoj

Commissioning Editor

Wilson Dsouza

Proofreader

Safis Editing

Acquisition Editor

Shweta Pant

Indexer

Mariammal Chettiyar

Content Development Editor

Arun Nadar

Graphics

Jason Monteiro

Technical Editor

Sachit Bedi

Production Coordinator

Deepika Naik

About the Author

Nick Zhu is a professional programmer and data engineer with more than a decade experience in software development, big data, and machine learning. Currently, he is one of the founders and CTO of Yroo.com - meta search engine for online shopping. He is also the creator of dc.js—a popular multidimensional charting library built on D3.

About the Reviewer

Scott Becker is a partner at Olio Apps, a software consulting company in Portland Oregon. He has built numerous systems including a marketplace for geospatial datasets, HIPAA compliant video services for the medical industry, and visualizations of breaches in data security products. He is currently building a next generation time tracking system atwww.shoutbase.com. He has also produced a video course on data visualization with D3.js available at deveo.tv.

www.PacktPub.com

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www.packtpub.com/mapt

Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

Why subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Customer Feedback

Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1786468255.

If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!

Preface

D3.js is a JavaScript library designed to display digital data in dynamic graphical form. It helps you bring data to life using HTML, SVG, and CSS. D3 allows great control over the final visual result, and it is the hottest and most powerful web-based data visualization technology on the market today.

D3 v4 is the latest release of the D3 library. This second edition cookbook has been completely updated to cover and leverage the D3 v4 API, modular data structure, as well as revamped force implemented. It is designed to provide you with all the guidance you need to get to grips with data visualization with D3. With this book, you will create breathtaking data visualization with professional efficiency and precision with the help of practical recipes, illustrations, and code samples.

This cookbook starts off by touching upon data visualization and D3 basics before gradually taking you through a number of practical recipes covering a wide range of topics you need to know about D3.

You will learn the fundamental concepts of data visualization, functional JavaScript, and D3 fundamentals, including element selection, data binding, animation, and SVG generation. You will also learn how to leverage more advanced techniques such as interpolators, custom tweening, timers, queueing, hierarchy, force manipulation, and so on. This book also provides a number of pre-built chart recipes with ready-to-go sample code to help you bootstrap quickly.

What this book covers

Chapter 1, Getting Started with D3.js, is designed to get you up and running with D3.js, covering fundamental aspects, such as what D3.js is, and how to set up a typical D3.js data visualization environment.

Chapter 2, Be Selective, covers one of the most fundamental tasks you need to perform with any data visualization project using D3: selection. Selection helps you target certain visual elements on the page.

Chapter 3, Dealing with Data, explores the most essential question in any data visualization project: how to represent data in both programming constructs and its visual metaphor.

Chapter 4, Tipping the Scales, covers the  one key task that you need to perform over and over again as a data visualization developer, that is, mapping values in your data domain to visual domain, which is the focus of this chapter.

Chapter 5, Playing with Axes, explores the usage of the axes component and some related techniques commonly used in the visualization based on the Cartesian coordinates system.

Chapter 6, Transition with Style, deals with a saying that is arguably one of the most important cornerstones of data visualization, "a picture is worth a thousand words." This chapter covers transition and animation support provided by the D3 library.

Chapter 7, Getting into Shape,  deals with Scalable Vector Graphic (SVG), which is a mature World Wide Web Consortium (W3C) standard widely used in visualization projects.

Chapter 8, Chart Them Up, explores one of the oldest and trusted companions in data visualization: charts. Charts are a well-defined and well-understood graphical representation of data.

Chapter 9, Lay Them Out, focuses on the D3 layout. D3 layouts are algorithms that calculate and generate placement information for a group of elements capable of generating some of the most complex and interesting visualizations.

Chapter 10, Interacting with Your Visualization, focuses on D3 human visualization interaction support or, in other words, how to add computational steering capability to your visualization.

Chapter 11, Using Force, covers one of the most fascinating aspects of D3: force. Force simulation is one of the most awe-inspiring techniques that you can add to your visualization.

Chapter 12, Know Your Map, introduces the basic D3 cartographic visualization techniques and how to implement a fully functional geographic visualization in D3.

Chapter 13, Test Drive Your Visualization, guides you to implement your visualization like a pro with Test-Driven Development (TDD).

Appendix A, Building Interactive Analytics in Minutes, serves as an introduction to Crossfilter.js and DC.js on interactive dimensional charting.

What you need for this book

A text editor to edit and create HTML, CSS. and JavaScript filesA modern web browser (Firefox 3, IE 9, Chrome, Safari 3.2, and later)A local HTTP server to host data file for some of the more advanced recipes in this book. We will cover how to set up a Node-based or Python-based simple HTTP server in the first chapter.Optionally, you will need a Git client if you would like to check out the recipe source code directly from our Git repository

Who this book is for

If you are a developer or an analyst familiar with HTML, CSS, and JavaScript, and you wish to get the most out of D3, then this book is for you. This book can also serve as a desktop quick-reference guide for experienced data visualization developers.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors .

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. 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.

You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.Hover the mouse pointer on the SUPPORT tab at the top.Click on Code Downloads & Errata.Enter the name of the book in the Search box.Select the book for which you're looking to download the code files.Choose from the drop-down menu where you purchased this book from.Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for WindowsZipeg / iZip / UnRarX for Mac7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Data-Visualization-with-D3-4.x-Cookbook . We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.

Chapter 1. Getting Started with D3.js

In this chapter, we will cover:

Setting up a simple D3 development environmentSetting up an NPM-based D3 development environmentUnderstanding D3-style functional JavaScript

Introduction

This chapter is designed to get you up and running with D3.js and covers fundamental aspects, such as what D3.js is and how to set up a typical D3.js data visualization environment. One particular section is also devoted to covering some lesser known areas of JavaScript that D3.js relies heavily on.

What is D3? D3 refers to Data-Driven Documents, and according to the official D3 Wiki:

D3 (Data-Driven Documents or D3.js) is a JavaScript library for visualizing data using web standards. D3 helps you bring data to life using SVG, Canvas and HTML. D3 combines powerful visualization and interaction techniques with a data-driven approach to DOM manipulation, giving you the full capabilities of modern browsers and the freedom to design the right visual interface for your data.

-D3 Github Wiki (2016, August)

In a sense, D3 is a specialized JavaScript library that allows you to create amazing data visualizations using a simple (data driven) approach by leveraging the existing Web standards. D3.js was created by Mike Bostock (https://bost.ocks.org/mike/ ) and superseded his previous work on a different JavaScript data visualization library called Protovis. For more information on how D3 was created and on the theory that influenced both Protovis and D3.js, please check out the links in the following information box. Here, in this book, we will focus more on how to use D3.js to power your visualization. Initially, some aspects of D3 maybe a bit confusing due to its different approach for data visualization. I hope that over the course of this book, a large number of topics, both basic and advanced, will make you comfortable and effective with D3. Once it is properly understood, D3 can improve your productivity and expressiveness with data visualizations by orders of magnitude.

Note

For a more formal introduction to the idea behind D3, refer to the Declarative Language Design for Interactive Visualization paper published by Mike Bostock on IEEE InfoVis 2010 at http://vis.stanford.edu/papers/protovis-design . If you are interested to know how D3 came about, I recommend that you check out the D3: Data-Driven Document paper published by Mike Bostock, Vadim Ogievestsky, and Jeffery Heer on IEEE InfoVis 2011 at http://vis.stanford.edu/papers/d3 . Protovis, the predecessor of D3.js, also created by Mike Bostock and Jeff Heer of the Stanford Visualization Group can be found at https://mbostock.github.io/protovis/ .

Setting up a simple D3 development environment

The first thing you will need when you start a D3-powered data visualization project is a working development environment. In this recipe, we will show you how a simple D3 development environment can be set up within minutes.

Getting ready

Before we start, make sure that you have your favorite text editor installed and ready on your computer.

How to do it...

We''ll start by downloading D3.js through the following steps:

Download the latest stable version of D3.js from https://d3js.org/ . You can download the archived, older releases from https://github.com/d3/d3/tags . Additionally, if you are interested in trying out the bleeding edge D3 build on master branch, then you can fork https://github.com/d3/d3 .Once it is downloaded and unzipped, you will find two D3 JavaScript files, d3.js and d3.min.js, and other informational files in the extracted folder. For development purpose, it is recommended that you use the d3.js file, the non-uglified (minimized) version, since it can help you trace and debug JavaScript inside the D3 library. Once extracted, place the d3.js file in the same folder with an index.html file containing the following HTML: <!-- index.html -->      <!DOCTYPE html>      <html>      <head>       <meta charset=""utf-8"">       <title>Simple D3 Dev Env</title>       <script type=""text/javascript"" src=""d3.js""></script>      </head>      <body>      </body>      </html>

This is all you need to create, in its simplest form, a D3-powered data visualization development environment. With this setup, you can essentially open the HTML file using your favorite text editor to start your development and also to view your visualization by opening the file in your browser.

Note

The source code for this recipe can be found at https://github.com/NickQiZhu/d3-cookbook-v2/tree/master/src/chapter1/simple-dev-env .

How it works...

D3 JavaScript library is very self-sufficient. It has no dependency on any other JavaScript library except what your browser already provides.

Note

If your visualization's target browser environment includes Internet Explorer 9, it is recommended that you use the compatibility library Aight, which can be found at https://github.com/shawnbot/aight , and Sizzle selector engine, which can be found at http://sizzlejs.com/ .

Having the following character encoding instruction in the header section was critical before D3 v4 release since the older version of D3 used UTF-8 symbols, such as π, in its source; however, with D3 v4.x, it is no longer necessary. It is still considered a good practice however, since other JavaScript libraries you will include might be using UTF-8 symbols, as shown in the following example:

<meta charset=""utf-8"">

Note

D3 is completely open source under a custom license agreement created by its author Michael Bostock. This license is pretty similar to the popular MIT license, with only one exception where it explicitly states that Michael Bostock's name cannot be used to endorse or promote products derived from this software without his permission.

There's more...

Throughout this cookbook, numerous recipe code examples will be provided. All example source code is provided and hosted on GitHub (https://github.com/ ), a popular open source social coding repository platform.

How to get source code

The easiest way to get all the recipe source code that you will need is to clone the Git repository (https://github.com/NickQiZhu/d3-cookbook-v2 ) for this book. If you are not planning to set up a development environment for the recipes, then you can safely skip this section.

Note

In case you are not familiar with Git, its clone concept is similar to the checkout concept in other version control software. However, cloning does a lot more than simply checking out the files. It also copies all branches and histories to your local machine, effectively cloning the entire repository to your local machine so you can work even when you are completely offline with this cloned repository in your own environment.

First, install a Git client on your computer. You can find a list of Git client software at https://git-scm.com/downloads , and a detailed guide on how to install it on different operating systems at https://git-scm.com/book/en/Getting-Started-Installing-Git .

Note

Another popular way to get Git and GitHub working is to install the GitHub client, which gives you a richer set of features than simply Git. However, at the time of writing this book, GitHub only offered client software for Windows and Mac OS; refer to https://desktop.github.com/ .

Once the Git client is installed, simply issuing the following command will download all recipe source code to your computer:

> git clone [email protected]:NickQiZhu/d3-cookbook-v2.git

Setting up an NPM-based development environment

The simple setup demonstrated in the previous recipe is enough for implementing most recipes in this book. However, when you work on a more complex data visualization project that requires the use of a number of JavaScript libraries, the simple solution we discussed before might become a bit clumsy and unwieldy. In this section, we will demonstrate an improved setup using Node Packaged Modules (NPM), a de facto JavaScript library repository management system. If you are as impatient as me and want to get to the meaty part of the book, the recipes, you can safely skip this section and come back when you need to set up a more production-ready environment for your project.

Getting ready

Before we start, please make sure that you have NPM properly installed. NPM comes as part of the Node.js installation. You can download Node.js from https://nodejs.org/ . Select the correct Node.js binary build for your OS. Once installed, the following npm command will become available in your terminal console:

> npm -v 2.15.8

The preceding command prints out the version number of your NPM client to indicate that the installation is successful.

How to do it...

With the NPM installed, we can now create a package descriptor file to automate some of the manual setup steps:

First, under your project folder, create a file named package.json that contains the following code:     {       ""name": "d3-project-template",       ""version": "0.1.0",       "description": "Ready to go d3 data visualization project template",       "keywords": [       "data visualization",       "d3"       ],       "homepage": "<project home page>",       "author": {       "name": "<your name>",       "url": "<your url>"       },       "repository": {       "type": "git",       "url": "<source repo url>"       },       "dependencies": {       "d3":"4.x"       },       "devDependencies": {       "uglify-js": "2.x"       }      } Once the package.json file is defined, you can simply run the following:       > npm install

How it works...

Most of the fields in the package.json file are for informational purpose only, such as its name, description, home page, author, and the repository. The name and the version fields will be used if you decide to publish your library into an NPM repository in the future. What we really care about, at this point, are the dependencies and devDependencies fields:

The dependencies field describes the runtime library dependencies that your project has, that is, the libraries your project will need to run properly in a browser.In this simple example, we only have one dependency on D3. d3 is the name of the D3 library that is published in the NPM repository. The version number 4.x signifies that this project is compatible with any of the version 4 releases, and NPM should retrieve the latest stable version 4 build to satisfy this dependency.

Note

D3 is a self-sufficient library with zero external runtime dependency. However, this does not mean that it cannot work with other popular JavaScript libraries. I regularly use D3 with other libraries to make my job easier, for example, JQuery, Zepto.js, Underscore.js, and ReactJs to name a few.

The devDependencies field describes development time (compile time) of library dependencies. What this means is that libraries specified under this category are only required in order to build this project, and not required to run your JavaScript project.

Note

Detailed NPM package JSON file documentation can be found at https://docs.npmjs.com/files/package.json .

Executing the npm install command will automatically trigger NPM to download all dependencies that your project requires, including your dependencies' dependencies recursively. All dependency libraries will be downloaded into the node_modules folder under your project's root folder. When this is done, you can just simply create your HTML file as shown in the previous recipe, and load your D3 JavaScript library directly from node_modules/d3/build/d3.js.

The source code for this recipe with an automated build script can be found at https://github.com/NickQiZhu/d3-cookbook-v2/tree/master/src/chapter1/npm-dev-env .

Relying on NPM is a simple and yet more effective way to save yourself from all the trouble of downloading JavaScript libraries manually and the constant need for keeping them up to date. However, an astute reader may have already noticed that with this power we can easily push our environment setup to the next level. What if you are building a large visualization project where thousands of lines of JavaScript code will be created? Then obviously, our simple setup described here will no longer be sufficient. However, modular JavaScript development by itself can fill an entire book; therefore, we will not try to cover this topic since our focus is on data visualization and D3. In later chapters, when unit test-related recipes is discussed, we will expand the coverage on this topic to show how our setup can be enhanced to run automated build and unit tests.

Tip

D3 v4.x is very modular; so if you only need a part of the D3 library for your project, you can also selectively include D3 submodule as your dependency. For example, if you only need d3-selection module in your project, then you can use the following dependency declaration in your package.json file: "dependencies": {      "d3-selection":"1.x"}

There's more...

Although in previous sections it was mentioned that you can just open the HTML page that you have created using your browser to view your visualization result directly, this approach does have its limitations. This simple approach stops working once we  need to load data from a separate data file (this is what we will do in later chapters, and it is also the most likely case in your daily working environment) due to the browser's built-in security policy. To get around this security constraint, it is highly recommended that you set up a local HTTP server so your HTML page and the data file can be accessed from this server instead of being loaded from a local file system directly.

Setting up a local HTTP server

There are probably more than a dozen different ways to set up an HTTP server on your computer based on the operating system you use and the software package you decide to use to act as an HTTP server. Here, I will attempt to cover some of the most popular setups.

Python Simple HTTP server

This is my favorite for development and fast prototyping. If you have Python installed on your OS, which is usually the case with any Unix/Linux/Mac OS distribution, then you can simply type the following command in your terminal with Python 2:

> python -m SimpleHTTPServer 8888

Alternatively, type the following command with Python 3 distribution:

> python -m http.server 8888

This little python program will launch an HTTP server and start serving any file right from the folder where this program is launched. This is by far the easiest way to get an HTTP server running on any OS.

Note

If you don't have python installed on your computer yet, you can get it from https://www.python.org/getit/ . It works on all modern OS, including Windows, Linux, and Mac.

Node.js HTTP server

If you have Node.js installed, perhaps as part of the development environment setup exercise we did in the previous section, then you can simply install the http-server module. Similar to the Python Simple HTTP Server, this module will allow you to launch a lightweight HTTP server from any folder and start serving pages right away.

First, you need to install the http-server module using the following command:

> npm install http-server -g

The -g option in this command will install http-server module globally, so it will become available in your command-line terminal automatically. Once this is done, you can launch the server from any folder you are in by simply issuing the following command:

> http-server -p 8888

This command will launch a Node.js-powered HTTP server on the default port 8080, or if you want, you can use the -p option to provide a custom port number for it.

Note

If you are running the npm install command on Linux, Unix, or Mac OS, you may need to run the command in the sudo mode or as root in order to use the -g global installation option.