D3.js: Cutting-edge Data Visualization Turn your raw data into real knowledge by creating and deploying complex data visualizations with D3.js A course in three modules BIRMINGHAM - MUMBAI D3.js: Cutting-edge Data Visualization Copyright © 2016 Packt Publishing All rights reserved. No part of this course 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 course to ensure the accuracy of the information presented. However, the information contained in this course is sold without warranty, either express or implied. Neither the authors, 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 course. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this course by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Published on: December 2016 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78728-177-6 www.packtpub.com Credits Authors Content Development Editor Ændrew H. Rininsland Samantha Gonsalves Michael Heydt Pablo Navarro Castillo Graphics Jason Monteiro Reviewers Elliot Bentley Production Coordinator Shraddha Falebhai Patrick Cason Pablo Núñez Navarro William Sankey Andrew Berls Simon Heimler Lars Kotthoff Nathan Vander Wilt Preface D3 is an amazing library. On its website, there are hundreds of beautiful examples, visualizations, and charts created mainly with D3. Looking at the examples, we soon realize that D3 allows us to create an uncanny variety of visuals. We can find everything from simple bar charts to interactive maps. The ability to create almost anything with D3 comes at a price; we must think about our charts at a more abstract level and learn how to bind data elements with elements in our page. This association between properties of our data items and visual attributes of the elements in our chart will allow us to create complex charts and visualizations. In real-life projects, we will have to integrate components and charts created with D3 with other components and libraries. In most of the examples in this module, we will cover how to integrate D3 with other libraries and tools, creating complete applications that leverage the best of each library. D3 is a great tool to experiment with visuals and data! What this learning path covers Module 1, Learning d3.js Data Visualization, Second Edition, Over the course of this module, you'll learn the basics of one of the world's most ubiquitous and powerful data visualization libraries, but we won't stop there. By the end of our time together, you'll have all the skills you need to become a total D3 ninja, able to do everything from building visualizations from scratch to using it on the server and writing automated tests. As well, if you haven't leveled up your JavaScript skills for a while, you're in for a treat—this module endeavors to use the latest features that are currently being added to the language, all this while explaining why they're cool and how they differ from old-school JavaScript. [ i ] Preface Module 2, D3.js By Example, This module uses examples that take you right from the beginning, with the basic concepts of D3.js, using practical examples that progressively build on each other both within a specific chapter and also with reference to previous chapters. We will focus on the examples created for this module as well as those found online that are excellent but could use some additional explanation. Each example will explain how the example works either line by line or by comparison with other examples and concepts learned earlier in the module. Module 3, Mastering D3.js, In this module, we will cover reusable charts using external data sources, thereby creating user interface elements and interactive maps with D3. At the end, we will implement an application to visualize topics mentioned on Twitter in real time. What you need for this learning path Module 1: You will need a machine that is capable of running Node.js. We will discuss how to install this in the first chapter. You can run it on pretty much anything, but having a few extra gigabytes of RAM available will probably help while developing. Some of the mapping examples later in the module are kind of CPU-intensive, though most machines produced since 2014 should be able to handle them. You will also need the latest version of your favorite web browser; mine is Chrome, and I use it in the examples, but Firefox also works well. You can try to work in Safari, Internet Explorer/Edge, Opera, or any other browser, but I feel that Chrome's Developer Tools are the best. All the code for this module is hosted on GitHub. We talk about how to clone the repo and switch between branches (which are used to separate the code into chapters) in Chapter 1, Getting Started with D3, ES2016, and Node.js but you can take a look ahead of time by visiting https://github.com/aendrew/learning-d3. Module 2: All of the tools used in this module are available on the Internet free of charge. All that is required is a modern web browser to run the samples, and all code can be edited and run online within the browser. To be specific about what makes up a modern browser, this includes Firefox, Chrome, Safari, Opera, IE9+, Android, and iOS. [ ii ] Preface All examples in this text are available to review, execute, and edit online. References to code are referred to as a bl.ock and be referenced as follows: bl.ock (2.13): http://bl.ocks.org/ d3byex/35641fbe385e5a162b84 This will take you to a page on http://bl.ocks.org/ for the example. This page will also contain a link to take you to jsbin.com where you can interactively make changes to the code. Module 3: The code bundle of this module was created using Jekyll, which is a static website generator. To run most of the examples in the code bundle, you will need a static web server and a modern web browser. The following list summarizes the main dependencies: • A modern web browser • D3 3.4 • Jekyll or other static web servers • Text editor Some chapters require you to install additional frontend libraries, such as Backbone, TopoJSON, Typeahead, and Bootstrap. Additional instructions on installing these libraries can be found in the corresponding chapters. In other chapters, we will use additional software to compile assets or process files. In those cases, installing the software is optional (the compiled files will be present as well), but it might be useful for you to install them for your own projects: • Node and Node packages • Git • Make • TopoJSON • GDAL Instructions to install these applications can also be found in the corresponding chapters. [ iii ] Preface Who this learning path is for Whether you are new to data and data visualization, a seasoned data scientist, or a computer graphics specialist, this Learning Path will provide you with the skills you need to create web-based and interactive data visualizations. Some basic JavaScript knowledge is expected, but no prior experience with data visualization or D3 is required. Reader feedback Feedback from our readers is always welcome. Let us know what you think about this course—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 course'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 course, 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 course from your account at http://www.packtpub.com. If you purchased this course 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: 1. Log in or register to our website using your e-mail address and password. 2. Hover the mouse pointer on the SUPPORT tab at the top. 3. Click on Code Downloads & Errata. 4. Enter the name of the course in the Search box. 5. Select the course for which you're looking to download the code files. 6. Choose from the drop-down menu where you purchased this course from. 7. Click on Code Download. [ iv ] Preface You can also download the code files by clicking on the Code Files button on the course's webpage at the Packt Publishing website. This page can be accessed by entering the course'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 Windows • Zipeg / iZip / UnRarX for Mac • 7-Zip / PeaZip for Linux The code bundle for the course is also hosted on GitHub at https://github.com/PacktPublishing/D3js-Cutting-edge-Data- Visualization. We also have other code bundles from our rich catalog of course 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 courses—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 course. If you find any errata, please report them by visiting http://www. packtpub.com/submit-errata, selecting your course, 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 course 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. [ v ]