Book Image

Learn D3.js

By : Helder da Rocha
2 (1)
Book Image

Learn D3.js

2 (1)
By: Helder da Rocha

Overview of this book

This book is a practical hands-on introduction to D3 (Data-driven Documents): the most popular open-source JavaScript library for creating interactive web-based data visualizations. Based entirely on open web standards, D3 provides an integrated collection of tools for efficiently binding data to graphical elements. If you have basic knowledge of HTML, CSS and JavaScript you can use D3.js to create beautiful interactive web-based data visualizations. D3 is not a charting library. It doesn’t contain any pre-defined chart types, but can be used to create whatever visual representations of data you can imagine. The goal of this book is to introduce D3 and provide a learning path so that you obtain a solid understanding of its fundamental concepts, learn to use most of its modules and functions, and gain enough experience to create your own D3 visualizations. You will learn how to create bar, line, pie and scatter charts, trees, dendograms, treemaps, circle packs, chord/ribbon diagrams, sankey diagrams, animated network diagrams, and maps using different geographical projections. Fundamental concepts are explained in each chapter and then applied to a larger example in step-by-step tutorials, complete with full code, from hundreds of examples you can download and run. This book covers D3 version 5 and is based on ES2015 JavaScript.
Table of Contents (13 chapters)

Modules (microlibraries)

You don't have to always load the entire D3 library. D3 is a modular library, so if you are only using some D3 features, you can include only the parts that you are using. The minified default bundle is about 240 KB in size, but you may be able to create your chart using as little as 13 KB if you don't need many modules. An animated interactive bar chart, complete with tooltips, transitions, colors, and SVG graphics can be created with less than 24 KB loading just the following two modules:

<script src="https://d3js.org/d3-selection.v1.min.js"></script>
<script src="https://d3js.org/d3-transition.v1.min.js"></script>

But if you need axes, maps, and other features, you will require more modules and dependencies. In this case, either use the default bundle, or set up a development environment where you can install each module using npm, since it automatically includes any dependencies. For production, you can generate and export a custom bundle using a packing tool such as Rollup. To install any module using npm, use the following:

npm install module-name

For the examples in this chapter (and most of the book) we will import D3 using the <script> tag with the CDN URL to the default bundle.

Even if you always use the default d3.js bundle, you should be aware of the modules it contains, and of the modules that are not part of the default bundle, because they need to be imported separately in case you require their functions. All the official documentation is also organized per module, and knowledge of the modular structure will allow you to tune the performance of your D3 application, in case you decide to create a custom bundle. Modules have their own versioning systems. You might need to load a module separately if you wish to use a feature included in a new major version, not yet included in the default bundle.

The following diagram shows the modules available in D3.js version 5, indicating direct dependencies (transitive dependencies are not shown). For example, if you need to use a function from the d3-scale module, you should import all the direct dependencies: (d3-array, d3-time-format, d3-collection, d3-format, d3-interpolate) and the transitive ones:( d3-time and d3-color). In this case, you should either use npm or import the default bundle.

Modules (microlibraries) available in D3.js v5 showing direct dependencies. The orange and yellow libraries are included as part of the default bundle. The other libraries (pink and purple) have to be explicitly imported

The following tables contain a quick reference of all modules available in the current version, classified according to their purpose. In this book, we will be using functions from almost all of them.

Data manipulation

The following modules listed are used to generate, manipulate, transform, parse, and format data, in the form of numbers, text, arrays, objects, and files. They are all included in the default d3.js bundle:

Module

Bundled (d3v5)

Description

d3-array

Yes

Several array utilities that extend the basic ES6 functions, optimized for use with datasets. Dependencies: none.

d3-collection

Yes

Maps and sets optimized for use with datasets; functions for object collections and nesting data. Dependencies: none.

d3-random

Yes

Random number generators. Dependencies: none.

d3-dsv

Yes

Parser functions for delimiter-separated data. Dependencies: none.

d3-interpolate

Yes

Several functions for interpolating numbers, colors, strings, and so on. Dependencies: d3-color.

d3-scale

Yes

Generator functions to map data dimensions to graphical dimensions. Dependencies: d3-array, d3-collection, d3-format, d3-interpolate, d3-time-format, d3-time.

d3-time

Yes

API for operations with time (intervals, ranges, and so on). Dependencies: none.

d3-format

Yes

Locale-sensitive methods for number formatting. Dependencies: none.

d3-time-format

Yes

Locale-sensitive methods for date/time formatting. Dependencies: d3-time.

Modules with methods for manipulating, transforming, parsing, and formatting data

Document manipulation

These are core modules in D3 used to select and manipulate HTML or SVG elements by providing a concise API to the DOM. With these modules, you can select and filter elements (using CSS selectors), create elements, append, insert, or remove from the DOM tree, add attributes and contents, change styles or classes, connect event handlers, and join data. Practically any D3 application uses at least d3-selection.

Module

Bundled (d3v5)

Description

d3-selection

Yes

Contains the essential DOM API for selection and manipulation of DOM elements. Dependencies: none.

d3-selection-multi

No

Adds optional support for setting multiple attributes, styles, or properties in selections and transitions using an object syntax. Dependencies: d3-selection, d3-transition.

Modules with functions for selecting and manipulating graphical elements using the DOM

Interactivity and animation

The modules listed in the following table are used in dynamic visualizations when updating data, zooming, dragging, selecting and clicking charts and maps.

Module

Bundled (d3v5)

Description

d3-transition

Yes

Methods to configure the transition applied to a selection. Dependencies: d3-interpolate, d3-ease, d3-dispatch, d3-selection, d3-timer, d3-color.

d3-ease

Yes

Easing functions for animations. Dependencies: none.

d3-zoom

Yes

Apply transforms in HTML, SVG, or Canvas using mouse or touch. Also includes support for programmatic transforms. Dependencies: d3-dispatch, d3-drag, d3-interpolate, d3-selection, d3-transition.

d3-drag

Yes

Drag and drop SVG, HTML, or Canvas using mouse or touch. Dependencies: d3-dispatch, d3-selection.

d3-brush

Yes

Selects a one or two-dimensional region for detailing using the mouse or touch. Dependencies: d3-dispatch, d3-drag, d3-interpolate, d3-selection, d3-transition.

d3-quadtree

Yes

Partitions two-dimensional space recursively into squares. Used to calculate regions for brushing and zooming. Dependencies: none.

d3-timer

Yes

A queue for managing concurrent animations. Dependencies: none.

d3-dispatch

Yes

Mechanism for event dispatching to named callbacks. Dependencies: none.

Modules with methods for event handling and dispatching, animations, and interactions

Colors

The following table lists modules that contain representations for machine-friendly and human-friendly color spaces and color schemes.

Module

Bundled (d3v5)

Description

d3-color

Yes

Support for several color spaces. Supports RGB, HSL, Cubehelix, Lab (CIELAB), and HCL (CIELCH). Dependencies: none.

d3-scale-chromatic

Yes

Sequential, diverging, and categorical color schemes and palettes. Dependencies: d3-color, d3-interpolate.

d3-hsv

No

Support for the HSV color space. Dependencies: d3-color.

d3-hcg

No

Support for the HCG color space. Dependencies: d3-color.

d3-cam16

No

Support for the CIECAM16 color space. Dependencies: d3-color.

Modules with methods for operations with colors spaces and schemes

Asynchronous operations and packaging

These modules are used to load files using Ajax.

Module

Bundled (d3v5)

Description

d3-fetch

Yes

Methods for fetching files asynchronously using Ajax. Also includes parsing methods for several data formats. Dependencies: d3-dsv.

d3-queue

No

Supports queueing of concurrent asynchronous requests. Dependencies: none. This module is deprecated in favor of ES6 promises.

d3-require

No

Supports AMD for loading modules and dependencies. Dependencies: none.

Modules for Ajax operations and module packaging

2D geometry

The following modules listed contain a complete two-dimensional graphical API that can be used to create any type of visualization, from simple line, area, or pie charts to arbitrary paths and shapes.

Module

Bundled (d3v5)

Description

d3-shape

Yes

Generators and utility functions to create and manipulate arcs, curves, lines, areas, stacks, curves, pie charts, symbols, and links in SVG or Canvas. Dependencies: d3-path.

d3-axis

Yes

Generates SVG axes. Dependencies: none.

d3-path

Yes

A Canvas API that generates SVG paths (which can be rendered as a Canvas or SVG). Dependencies: none.

d3-polygon

Yes

Utility functions for two-dimensional polygons. Dependencies: none.

Modules with methods for primitive geometric SVG operations and generators for shape-based charts

Spherical geometry and geographic maps

The following table lists modules that are used to display information in geographic maps. They contain methods for spherical trigonometry, geographic projections, and other utilities.

Module

Bundled (d3v5)

Description

d3-geo

Yes

Generators for geographic paths and standard projections, spherical shapes, and trigonometric functions, transforms, streams, and other utilities. Dependencies: d3-array.

d3-contour

Yes

Creates contour polygons, which are commonly used for geographical density, bathymetry, or relief maps. Dependencies: d3-array.

d3-geo-projection

No

Additional projections for d3-geo. Dependencies: d3-geo, d3-array.

d3-geo-polygon

No

Several utility functions for spherical polygons. Dependencies: d3-geo, d3-array, d3-geo-projection.

Modules with methods for displaying data from geographical information systems (GIS)

Layouts

The following modules listed, include generator functions, utilities, and algorithms to create visualizations for complex relationships such as node-link hierarchies, graphs, trees, networks, flow diagrams, tiles, and Voronoi diagrams.

Module

Bundled (d3v5)

Description

d3-hierarchy

Yes

Generator functions for hierarchic layouts such as node-link, adjacency, and enclosure diagrams. Functions create stratifications, clusters, trees, dendrograms, treemaps, circle packs, and partitions. Dependencies: none.

d3-force

Yes

Implementation of a force simulation (uses the Stormer-Verlet method), used for interactive visualizations of graphs, networks, and hierarchies. Dependencies: d3-collection, d3-dispatch, d3-quadtree, d3-timer.

d3-chord

Yes

Generator function and utilities for creating chord flow diagrams. Dependencies: d3-array, d3-path.

d3-sankey

No

Generator function and utilities for creating Sankey flow diagrams. Dependencies: d3-array, d3-collection, d3-shape.

d3-tile

No

A layout for raster image-based tiles, normally used for displaying tiles of geographic maps behind a vector layer. Dependencies: d3-array.

d3-hexbin

No

Generator function and utilities for creating scatterplots of hexagonal bins, useful for color-encoded heatmaps. Dependencies: none.

d3-voronoi

Yes

Generator functions and utilities for creating Voronoi diagrams (nature-like diagrams that show regions in a plane that are nearest to a point). Dependencies: none.

d3-delaunay

No

Compute the Delaunay triangulation of a set of points; a simpler and faster method to create Voronoi diagrams. Dependencies: none.

Modules containing algorithms and generator functions for graphical layouts