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.
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.
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.
How to use
- Install D3 via npm:
npm i d3or load from CDN. - Select a container element and bind your data array.
- Use scales to map data values to visual properties (position, color, size).
- Append SVG elements and set attributes based on bound data.
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');
Related on TokRepo
- AI tools for coding -- explore developer tools and libraries curated on TokRepo.
- Prompt library -- find prompts for generating D3 visualizations with AI assistants.
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
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.
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.
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.
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.
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
Related on TokRepo
Discussion
Related Assets
Moodle — Open-Source Learning Management System
The most widely used open-source learning platform, providing course management, assessments, and collaboration tools for educators and organizations worldwide.
Sylius — Headless E-Commerce Framework on Symfony
An open-source headless e-commerce platform built on Symfony and API Platform, designed for developers who need a customizable and API-first commerce solution.
Akaunting — Free Self-Hosted Accounting Software
A free, open-source online accounting application built on Laravel for small businesses and freelancers to manage invoices, expenses, and financial reports.