Skip to content
Tutorials Center
My learning

Topics

Lesson 44 of 44 ยท Projects and Practice

Practice Exercises and Next Steps

Test your D3 skills with self-checking exercises on scales, joins, layouts and shapes, a list of chart challenges with hints, common interview questions and what to learn next.

You learn D3 by making charts, not by reading about them. This final lesson gives you three kinds of practice: self-checking exercises that tell you immediately whether your code is right, chart challenges to build from scratch, and a list of questions that come up in interviews. At the end there is a roadmap for what to explore afterwards.

Part 1: self-checking exercises

The TryIt below contains four small functions marked TODO. A tiny test runner checks each one and prints PASS or FAIL. Edit the code and press Run until everything passes. Try each exercise before opening the hints and solutions below.

Exercises: scales, summaries, joins, layouts

All five checks fail at first. When you finish, your output should show only PASS lines.

Hints

  1. A scale has .domain(), .range() and .clamp(true). You can also pass the domain and range directly to d3.scaleLinear([0, 200], [40, 440]).
  2. d3.rollup(rows, v => d3.sum(v, d => d.qty), d => d.shop) gives a Map. Convert with Array.from(map) or [...map], then sort((a, b) => b[1] - a[1]).
  3. svg.selectAll("circle").data(numbers).join("circle"), then .attr("r", d => d) and .attr("cx", (d, i) => 30 * i + 20).
  4. d3.hierarchy(tree).sum(d => d.size), then .leaves(), sort by value descending and map to d.data.name. A node without size contributes undefined, which sum treats as 0.

Solutions

JavaScript
function exercise1() { return d3.scaleLinear([0, 200], [40, 440]).clamp(true); }

function exercise2(rows) {
  return Array.from(d3.rollup(rows, v => d3.sum(v, d => d.qty), d => d.shop))
    .sort((a, b) => b[1] - a[1]);
}

function exercise3(svg, numbers) {
  return svg.selectAll("circle").data(numbers).join("circle")
    .attr("r", d => d).attr("cx", (d, i) => 30 * i + 20);
}

function exercise4(tree) {
  return d3.hierarchy(tree).sum(d => d.size).leaves()
    .sort((a, b) => b.value - a.value).map(d => d.data.name);
}

Part 2: chart challenges

Build each of these from an empty file, using made-up data you generate with d3.range and simple formulas. Do not copy: the goal is to recall the steps yourself.

# Challenge Skills practiced
1 A horizontal bar chart sorted by value, with value labels at the end of each bar band and linear scales, axes, text
2 A line chart with two series, a legend and a crosshair tooltip d3.line, bisector, events
3 A scatter plot with circle size by a third value and color by category, with a zoomable plot area scaleSqrt, ordinal color, d3.zoom
4 A stacked bar chart with a toggle to switch between stacked and grouped d3.stack, transitions
5 A donut chart that animates in and shows the total in the middle d3.pie, d3.arc, attrTween
6 A heatmap of day of week against hour of day band scales, scaleSequential
7 A radial bar chart or a radar chart polar coordinates, d3.lineRadial
8 A force-directed graph with draggable nodes and a hover highlight of neighbors d3.forceSimulation, d3.drag
9 A choropleth of countries by a value you compute, with a legend d3.geoPath, TopoJSON, scales
10 A small-multiples grid: the same line chart for six categories, sharing the y scale reusable chart, d3.group
11 A sunburst where clicking an arc zooms into it d3.partition, transitions
12 A streamgraph with d3.stack().offset(d3.stackOffsetWiggle) d3.area, curves
13 A calendar heatmap (a year of days in a week-by-week grid) d3.timeWeek, time scales
14 A parallel coordinates plot with brushes on each axis scalePoint, d3.brushY
15 A canvas scatter plot with 100,000 points and quadtree hover canvas, performance

Quality checklist for every challenge: it has a title, labelled axes, units, a sensible color palette, readable font sizes, a tooltip or direct labels, and it works at 320 px wide. Then ask: would someone understand the chart in five seconds?

Part 3: questions you may be asked

  • What does selection.join() do, and how is it different from enter().append()?
  • Why do we need a key function in .data(data, key)?
  • What is the difference between a scale's domain and its range?
  • When would you use scaleBand instead of scaleLinear? scaleOrdinal? scaleQuantize?
  • How do you draw a smooth line through points? How do you handle missing values (line.defined)?
  • What does a layout like d3.treemap return, and who draws the rectangles?
  • SVG or canvas for 50,000 points? Why? How do you detect hover on canvas?
  • How do you make a chart responsive? What does viewBox do?
  • How do you integrate D3 with React without both fighting over the DOM?
  • How do you animate a pie chart's arcs? What is an attrTween?
  • What changed in D3 v6 for event listeners? ((event, d) instead of d3.event.)
  • How would you make a D3 chart accessible and testable?

You should be able to answer most of these in two or three sentences after finishing this course.

Part 4: where to go next

  1. Read other people's code. Open interactive examples from the D3 community and the official gallery, and rebuild small parts of them without looking at the source.
  2. Explore the ecosystem. d3-sankey for flow diagrams, d3-hexbin for hex binning, d3-cloud for word clouds, d3-geo-projection for dozens of extra projections, d3-delaunay and d3-voronoi style helpers (d3.Delaunay is built into D3 and is excellent for nearest-point hover and Voronoi diagrams), d3-contour for density plots, d3-scale-chromatic for the full color schemes.
  3. Combine with other tools. Observable Plot (from the D3 authors) for fast, high-level charts; WebGL libraries such as deck.gl for millions of points; chart libraries (ECharts, Chart.js, Vega-Lite) for standard charts where custom drawing is not needed. A good rule is: use a high-level library when your chart is standard, and D3 when your design is custom.
  4. Learn visualization design. Study chart types and when each one fits, perception (position beats length beats angle beats area beats color), color theory, annotation and narrative. Tools matter less than the ability to choose the right encoding.
  5. Ship something. Publish one chart or one dashboard on a real page with real data. Handle loading, errors, empty states, mobile screens and accessibility. Finishing a project teaches more than ten tutorials.
flowchart LR
  A["Basics: selections, joins"] --> B["Scales, axes, shapes"]
  B --> C["Layouts, transitions, interaction"]
  C --> D["Maps, canvas, frameworks"]
  D --> E["Your own projects"]
  E --> F["Ecosystem: Plot, Sankey, deck.gl, Vega"]

You have finished the left side of this path. The right side is built by making things.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which visual encoding do people compare most accurately?

    Position on a common axis is the most accurate channel, which is why bar charts and dot plots are such reliable choices.
  2. 2. When is a high-level charting library a better choice than hand-written D3?

    D3 shines for custom designs and interactions. For ordinary bar or line charts a library is faster to build and maintain.
  3. 3. In exercise 2, which method turns the Map returned by d3.rollup into an array you can sort?

    Array.from (or spread) converts a Map to an array of [key, value] pairs, which you can sort.
  4. 4. What is the best way to make sure a new chart is useful?

    Clarity beats decoration: labels, units and a clear message make a chart useful.

Summary

  • Practice with self-checking exercises first, then rebuild classic charts from an empty file.
  • Use the challenge table and the quality checklist to measure your progress.
  • Know the key concepts well enough to explain them: joins and keys, scales, layouts, SVG versus canvas, accessibility.
  • Keep learning by reading examples, exploring the ecosystem, studying visualization design and shipping real projects.

Next: D3.js track overview