Using D3 for the first time
We talked about D3.js for the last 2 lessons, but did not write any line of code using it yet. This is frustrating! 🙈
This lesson shows you how to install D3, how to import it — either the full bundle or individual modules — and how to call your first D3 function in a React app.
📦 Installing D3.js
Installing D3.js is straightforward: it works like any JavaScript library using npm.
If you're not familiar with this step, I strongly recommend going through the npm lesson in Module B. Otherwise, just type this in your terminal at the root of your project:
npm install d3That's it! D3 is now part of your project. You can verify by checking your package.json: you should see "d3" listed under dependencies.
On top of that, all the d3 functions are basically now copy-pasted inside the node_modules folder of your project!
🔌 Importing and using functions
Once the library is installed, you can import it in any component of your React application using an import statement. Once more, we already covered this in depth in the npm lesson in Module B
import * as d3 from "d3";This gives you access to every D3 function through the d3 namespace. For example, you can compute the maximum value of an array thanks to the max function:
d3.max() instead of max() avoids name collisions and makes it clear the function comes from D3.import * as d3 from "d3";
const data = [4, 8, 15, 16, 23, 42];
const maxValue = d3.max(data);
// 42Or create a scale that maps data values to pixel positions — something we'll use in almost every chart:
import * as d3 from "d3";
const scale = d3.scaleLinear()
.domain([0, 100]) // data goes from 0 to 100
.range([0, 500]); // pixels go from 0 to 500
scale(50); // 250
scale(75); // 375Don't worry about understanding scales fully yet — there's a whole module dedicated to them! The key takeaway is: import D3, call a function, get a result. That's the pattern.
📐 Full bundle or individual modules?
The import * as d3 from "d3" syntax loads the full D3 bundle → all the core modules at once.
But D3 is modular by design, so you can also install individual modules:
// Install only the scale module
npm install d3-scaleWhich means you then have to import functions from each module specifically:
// Import only what you need
import { scaleLinear } from "d3-scale";
const scale = scaleLinear()
.domain([0, 100])
.range([0, 500]);Both options make sense, depending on the situation!
Here's a quick comparison:
Full bundle (d3) | Individual modules | |
|---|---|---|
| Install | npm install d3 | npm install d3-scale d3-shape ... |
| Import | import * as d3 from "d3" | import { scaleLinear } from "d3-scale" |
| Bundle size | Larger (tree-shaking helps) | Smaller, only what you use |
| Convenience | Very easy, one install | More installs to manage |
In this course we'll mostly use the full bundle because it's simpler and lets you explore all functions without worrying about which module they come from.
💡 In production apps with strict performance requirements, importing individual modules can reduce bundle size. But for learning and prototyping, the full bundle is the way to go.
🎥 Video recap
How to install and import D3.js in a React app
Oh no! 😱
It seems like you haven't enrolled in the course yet!
Join many other students today and learn how to create bespoke, interactive graphs with d3.js and React!
Enrollment is currently closed. Join the waitlist to be notified when doors reopen:
Or Login