liveBook · Manning

D3.js

As explained in D3.js in Action, Third Edition

D3.js is a JavaScript library for binding data to documents and creating interactive, customizable visualizations in web browsers.

D3.js, short for Data-Driven Documents, is a JavaScript library for creating dynamic, interactive web visualizations. Choose it when a chart needs unusual shapes, tailored interactions, or a presentation that predefined chart tools cannot easily provide. A conventional bar chart takes more work in D3 than in a dedicated charting library; that explicit control is the trade-off for creative freedom. section 1.1 section 1.1.2 section 1.1.3

D3 works with the technologies of an ordinary web page: HTML, CSS, JavaScript, and the document object model (DOM). Visualizations often use SVG shapes, though D3 can also work with HTML elements or Canvas. For a bar chart, you create an SVG container, add rectangles for data points, calculate their dimensions with scales, and add axes. There is no single “make bar chart” function. section 1.1.3 section 1.2

A practical path from dataset to graphic is:

  • Load a dataset, commonly CSV or JSON, then format and measure its values. Data cleaning may require substantial work before visualization begins. section 1.1.3 section 3.0
  • Select DOM elements with d3.select() or d3.selectAll(). Use append() to add elements, then attr() and style() to control their appearance. section 2.7
  • Bind data to elements so values can generate shapes or HTML items. Use scales when raw values need translating into positions, lengths, or colors. section 3.0 section 5.3
  • Add interactions, such as mouse-driven filtering or zooming, after the graphic communicates its data clearly. section 1.1.3 section 1.3
Figure 1.7 How to approach data visualization with D3.js
figure

Follow the workflow diagram from loading and preparing data through drawing a visualization and adding interaction. section 1.1.3

Start with a visible change

In an HTML page, load D3 before your own JavaScript file. Then `d3.select("h1").style("color", "plum");` selects the first heading and changes its color. The same selection-and-modification pattern extends to SVG shapes. For local development, run the page through a web server; the introductory setup uses VS Code’s Live Server. section 2.2.2 section 2.7

D3 can also build an HTML legend: bind an array of color-and-label records to list items, append spans, and let CSS arrange them. In a React project, another approach is to let React render and update the DOM while D3 supplies calculations such as scales and shape generators. Whichever approach you use, check that readers can interpret the result—not just that it looks impressive. section 5.3 section 8.6 section 1.3

How D3.js in Action, Third Edition covers it

How deep it goes
Works through setup, DOM manipulation, data binding, scales, and framework integration; leaves substantial data cleaning outside its scope.
Examples
Uses a survey bar chart, a data-bound HTML color legend, and a React scatterplot.
What it assumes
Assumes a little JavaScript experience while teaching SVG basics, method chaining, and object manipulation needed to begin.
What you can do after
Build it from scratch: assemble data-driven browser graphics and interactions, or apply D3 calculations while React renders the elements.
How it connects
Connects data binding and scales to DOM elements, SVG, CSS, and React rendering.

Also explained in

Compare how 3 books explain D3.js →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.