Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 44 ยท Getting Started

What Is D3.js?

Learn what D3.js is, how it differs from charting libraries, how data, selections, scales and shapes fit together, and build your first chart in minutes.

D3 (Data-Driven Documents) is a JavaScript library for turning data into visuals in the browser. It does not give you a "bar chart" function. Instead it gives you small, sharp tools: tools to select elements, tie data to them, convert numbers into pixel positions, and generate shapes. You combine them to draw exactly what you want, whether that is a plain bar chart, a force-directed network, or an interactive map.

That freedom is what separates D3 from ready-made charting libraries. A charting library asks "which chart type do you want?" and draws it with options. D3 asks "what should each data point look like?" and leaves the picture to you. The price is that you write more code; the reward is that nothing is off limits.

The core idea: bind data to elements

Everything in D3 grows from one idea. You have an array of data, and you want one page element per item. D3 lets you select elements, bind the array to them, and then style each element using the datum it holds.

JavaScript
d3.selectAll("circle")
  .data([10, 20, 30])
  .attr("r", d => d);

The arrow function d => d receives each data value, so the three circles get radii 10, 20 and 30. The rest of the track is the story of doing this well.

Your first data-driven circles

Change the numbers in data and watch the circles follow. Add a fifth number: a fifth circle appears without any extra code, because the join creates one element per datum.

What D3 is made of

D3 is not one big block. It is a bundle of about thirty small modules, and d3 is a convenience bundle that exposes them all under one name. You can also import only what you need.

Module What it does
d3-selection Select and change elements, bind data
d3-scale Map data values to pixels, colors and sizes
d3-axis Draw axes from a scale
d3-shape Lines, areas, arcs, pies, symbols, stacks
d3-array Statistics, grouping, binning, sorting helpers
d3-fetch, d3-dsv Load and parse CSV, TSV and JSON
d3-transition Animate changes
d3-hierarchy, d3-force Trees, treemaps, packs, network layouts
d3-geo Map projections and paths
d3-format, d3-time-format Numbers and dates as text
flowchart LR
  A["Data<br/>(array of objects)"] --> B["Selection<br/>(elements)"]
  A --> C["Scales<br/>(value to pixel)"]
  C --> D["Shapes<br/>(paths, arcs)"]
  B --> E["SVG on the page"]
  D --> E

Data is bound to elements; scales and shape generators decide how each datum looks.

D3 versus charting libraries

D3 is often compared with Chart.js, Highcharts or Plotly. Here is an honest comparison.

  • Ready-made libraries are faster for standard charts. You pass a config object and get a bar chart with tooltips and a legend.
  • D3 is better when the design is custom, the data is unusual (networks, trees, maps, flows), or you need fine control of animation and interaction.
  • D3 does not draw on its own. It manipulates the DOM (usually SVG, sometimes HTML or canvas). That means every chart is accessible to CSS, to browser dev tools, and to screen readers if you add the right attributes.

Many teams use D3 even inside React, Vue or Angular for its math (scales, layouts, shape generators) while letting the framework own the DOM. You will see that in chapter 8.

Why SVG?

D3 works with any DOM element, but SVG (Scalable Vector Graphics) is the usual canvas. SVG shapes are real elements: you can select them, style them with CSS, attach events, and they stay sharp at any zoom level. The next lessons explain SVG fundamentals because most D3 confusion is really SVG confusion.

A tiny bar chart in about ten lines

The multiplications such as d.value * 1.8 are hand-made scales. In Scales you will let D3 do that math so you never hard-code a number again.

What you need to know first

You should be comfortable with HTML, CSS and basic JavaScript: arrays, objects, functions and arrow functions. If array.map is new to you, read the JavaScript tutorial first. No math beyond simple proportions is required.

D3 version 7 is the current major version and is what this track uses. Older tutorials show D3 v3 or v4 code such as d3.scale.linear() and .enter().append() followed by .exit().remove() everywhere. v7 uses d3.scaleLinear() and the simpler selection.join().

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does D3 primarily provide?

    D3 is a toolkit of low-level building blocks. You assemble charts yourself from selections, scales and shape generators.
  2. 2. In d3.selectAll('circle').data([10, 20, 30]).attr('r', d => d), what is d?

    Accessor functions receive the bound datum first and the index second. For this array, d is 10, then 20, then 30.
  3. 3. Why is SVG a popular target for D3 charts?

    Because SVG shapes are real elements, D3's selections and data joins work on them directly, and they stay sharp at any size.
  4. 4. Which statement about D3 and frameworks such as React is true?

    Many projects use only d3-scale, d3-shape or d3-hierarchy for calculations and render the result with the framework.

Summary

  • D3 binds data to DOM elements and gives you tools to shape the result; it is a toolkit, not a chart type menu.
  • It is made of small modules (selection, scale, shape, array, fetch, transition, hierarchy, force, geo, format) bundled as d3.
  • SVG is the usual canvas because its shapes are selectable, stylable DOM elements.
  • Version 7 uses d3.scaleLinear() and selection.join().

Next: Setting Up D3