ScriptsApr 11, 2026·2 min read

D3.js — Bring Data to Life with SVG, Canvas & HTML

D3 is the grandparent of data visualization on the web — a low-level toolkit for binding data to DOM, applying data-driven transformations, and building any chart imaginable. Powers the New York Times, Observable, and thousands of dashboards.

TL;DR
D3.js is a low-level JavaScript library for building custom data visualizations with SVG, Canvas, and HTML.
§01

What it is

D3 (Data-Driven Documents) is a JavaScript library for creating data visualizations using SVG, Canvas, and HTML. Created by Mike Bostock in 2011, D3 provides the building blocks for bindng data to DOM elements and applying data-driven transformations. It powers visualizations at the New York Times, Financial Times, The Guardian, and Observable.

D3 is best suited for developers and data journalists who need full control over visual output. Unlike chart libraries that offer pre-built chart types, D3 gives you primitives -- selections, scales, axes, shapes, layouts, transitions, and geo projections -- to build any visualization imaginable.

§02

How it saves time or tokens

D3 consolidates dozens of data visualization tasks into one consistent API. Instead of writing custom SVG manipulation code, you use D3 selections to bind data arrays to elements and let the enter/update/exit pattern handle DOM changes. The modular architecture means you only import the sub-packages you need, keeping bundle sizes small. For AI-assisted workflows, D3 code follows predictable patterns that LLMs can generate reliably.

§03

How to use

  1. Install D3 via npm: npm i d3 or load from CDN.
  2. Select a container element and bind your data array.
  3. Use scales to map data values to visual properties (position, color, size).
  4. Append SVG elements and set attributes based on bound data.
§04

Example

import * as d3 from 'd3';

const data = [10, 20, 30, 40, 50];

const svg = d3.select('body')
  .append('svg')
  .attr('width', 400)
  .attr('height', 200);

svg.selectAll('rect')
  .data(data)
  .enter()
  .append('rect')
  .attr('x', (_, i) => i * 60)
  .attr('y', (d) => 200 - d * 3)
  .attr('width', 50)
  .attr('height', (d) => d * 3)
  .attr('fill', 'steelblue');
§05

Related on TokRepo

§06

Common pitfalls

  • Confusing D3 v7 API with older v3/v4 tutorials. The module structure and import paths changed significantly. Always check the version in examples.
  • The enter/update/exit pattern requires understanding data joins. Missing the .enter() call means new data points never render.
  • D3 does not provide pre-built charts. If you want a bar chart in 5 lines, use a wrapper like Observable Plot or Chart.js instead.

Frequently Asked Questions

What is D3.js used for?+

D3.js is used for creating custom, interactive data visualizations on the web. It binds data to DOM elements and applies transformations using SVG, Canvas, or HTML. Common use cases include dashboards, maps, network graphs, and data journalism pieces.

Is D3.js still relevant in 2026?+

Yes. D3 remains the foundation for custom data visualization on the web. While higher-level libraries like Observable Plot and Chart.js handle simple charts more easily, D3 is still the go-to for bespoke, interactive, or complex visualizations that require full control.

How does D3.js compare to Chart.js?+

Chart.js provides pre-built chart types (bar, line, pie) with minimal configuration. D3.js provides low-level primitives for building any visualization from scratch. Use Chart.js for standard charts, D3 for custom or complex visualizations.

Can D3.js work with React or Vue?+

Yes, but with caveats. D3 manipulates the DOM directly, which conflicts with React and Vue virtual DOM. The recommended approach is to use D3 for calculations (scales, layouts) and let React/Vue handle rendering. Libraries like visx wrap D3 for React.

What is the learning curve for D3.js?+

D3 has a steep learning curve because it is low-level. You need to understand SVG, data joins (enter/update/exit), scales, and the module system. Most developers spend a few weeks before feeling productive. Observable notebooks provide good interactive learning.

Citations (3)
  • D3.js GitHub— D3 is a JavaScript library for data visualization created by Mike Bostock
  • D3.js Documentation— D3 uses a data join pattern with enter, update, and exit selections
  • Observable Plot— Observable Plot is a higher-level charting library built on D3

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets