liveBook · Manning

Ticks()

As explained in D3.js in Action, Third Edition

ticks() is a D3 continuous-scale method that returns an array of representative values across the scale's domain.

ticks() is a D3 continuous-scale method that returns representative values across a scale’s domain. Pass a desired tick count, then use the returned array to render labels and tick marks. section 8.6.1

Use the result to construct an axis:

  • Estimate a tick count from available chart space, such as innerWidth divided by 100 pixels. section 8.6.1
  • Call props.scale.ticks(numberOfTicks) to obtain domain values. section 8.6.1
  • Map over the values, position each group with scale(tick), and display the value as text. section 8.6.1

Bottom-axis example

With a 300-pixel inner width, requesting one tick per 100 pixels produces [0, 5000, 10000]. Each value becomes a labeled mark on the axis. section 8.6.1

<ScatterplotReactControlled
  margin={margin}
  data={props.data.experience}
  colorScale={colorScale}
/>

See ticks() used inside a React scatterplot whose axes and marks are rendered from scale-driven components. section 8.6.1

How D3.js in Action, Third Edition covers it

How deep it goes
Explains the call, returned array, spacing heuristic, and rendering loop, but defers tick-formatting details.
Examples
Uses [0, 5000, 10000] for a 300-pixel bottom axis and maps each value into a labeled tick group.
What it assumes
Assumes a configured continuous scale with a domain and range; teaches how to turn its values into axis elements.
What you can do after
Build it from scratch by generating scale-derived tick values and rendering positioned labels and marks.
How it connects
Connects continuous scales with axes, tick marks, scatterplots, and SVG text and line elements.

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.