Scales and Axes
How D3 turns numbers, dates and categories into positions and colours, how it picks round tick numbers, and how to draw axes that stay honest.
Every chart so far used a small piece of arithmetic: "value ÷ biggest × space". That rule has a name, a scale, and D3 has a whole family of them. In this phase you learn what each one does inside, so you can choose the right one and predict what it returns for odd inputs: zero, negative, missing, or text.
Then you draw axes: the tick marks and labels that let a reader turn a position back into a number. At the end you rebuild the dashboard's yearly rides line with a time scale, a linear scale and real axes, in two small files you can reuse.
Phase 6 · the story
Tomás, founder
“Why does my spreadsheet chart say 0, 730, 1,460 and the dashboard say 0, 1,000, 2,000? Which one is right?”
Before the board meeting, Tomás puts two charts of the same year of rides side by side. The dashboard's line chart (Phase 3 and 4) has an axis that reads 0, 1,000, 2,000 up to 5,000. The chart he made in a spreadsheet reads 0, 730, 1,460, 2,190, because it cut the busiest day, 4,380 rides, into six equal parts.
Neither is wrong, but only one is readable at a glance. Nobody can say where 1,000 sits on the spreadsheet axis. Tomás asks the question a board member will ask: "Why those numbers?" The team's own answer is a rule copied into one chart's code (niceTicks), and the other charts have no such rule.
In this phase the team learns how D3 chooses round numbers, how it maps dates and categories, and how an axis is built from a scale. By the end, the dashboard's rides-over-time chart runs on shared scales and axes, so Tomás can say why every number is where it is.
In this phase
You will learn
- Create linear, log, square-root, time, band and colour scales, and say what each returns for odd inputs
- Explain how D3 picks round tick numbers, and what nice() does to a domain
- Draw and update an axis with gridlines, formatted labels and a dark-mode-ready colour
- Build the dashboard's rides-over-time chart with real axes
You write code in
In your Vite project lumen-dashboard/ (run npm run dev), and in the editor on this page
Files you touch
- src/charts/rides-line.js
- src/parts/axis.js
- src/main.js
Practise this phase
Hands-on exercises: write the code, see your page live, and press Check to have it tested automatically.