liveBook · Manning

Dimension

As explained in D3.js in Action, Third Edition

A chart axis is a reference line with a scale, ticks, and often labels that communicates values or categories in a chart.

An axis is a visual reference that helps people read the numbers or categories in a chart. A horizontal x-axis can show time or categories, while a vertical y-axis can show a measured value such as temperature. Axes, labels, and legends need planned space around the chart, so charts commonly reserve margins before drawing data marks. section 4.1 section 4.1.2

In D3, an axis generator receives a scale and creates the SVG elements for an axis. The scale maps a data domain to chart-space positions: a time scale can position dates across the inner width, and a linear scale can position temperatures from the bottom upward. Create the scale first, then create and place the axis. section 4.1.2

Build and refine an axis in a chart:

  • Reserve an inner chart area with margins for axes and labels. section 4.1
  • Define a scale with a domain from the data and a range matching the inner chart dimension. section 4.1.2
  • Create a bottom or left axis from that scale, append a group, and translate it into position. section 4.1.2
  • Format tick labels and adjust their text styling or position when readability needs improvement. section 4.1.2

Temperature line chart

A time scale maps the first and last dates to the chart width for a bottom axis. A linear scale maps temperatures from 0 to the maximum temperature across an inverted y-range for a left axis. Formatting the time ticks as abbreviated months makes the horizontal labels easier to scan. section 4.1.2

Axes can also be built manually in React rather than with D3’s axis generator. A bottom axis renders its main line, calculated ticks, short tick lines, and text labels; a left axis applies the same pattern vertically. The number of ticks can respond to the available chart width or height. section 8.6.1

Figure 4.9 SVG elements composing an axis
figure

Inspect the SVG pieces of a generated axis: its main path, tick lines, and tick labels. section 4.1.2

How D3.js in Action, Third Edition covers it

How deep it goes
Builds axes from scales, positions and formats them, and also manually renders their SVG lines, ticks, and labels in React.
Examples
Uses temperature by month, salary histograms, salary box plots, and retention-versus-user-count scatterplots.
What it assumes
Uses scales and SVG positioning while introducing axis generators, ticks, labels, and margin-based layout.
What you can do after
Build it from scratch with D3 generators or React SVG components, placing and formatting bottom and left axes.
How it connects
Connects axes with scales, domains, ranges, ticks, labels, SVG groups, and the margin convention.

Also explained in

Compare how 8 books explain dimension →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.