Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 44 ยท Scales and Axes

Power, Log, Symlog and Time Scales in D3

Choose the right continuous D3 scale: scalePow and scaleSqrt for areas, scaleLog for wide ranges, scaleSymlog for zeros, scaleTime and scaleUtc for dates.

scaleLinear handles most numbers, but not all data is linear. Populations span from thousands to billions, circle area should follow the value, and dates are not plain numbers. D3 has a family of continuous scales that all share the same API (domain, range, invert, ticks, nice, clamp), so once you know scaleLinear, the others take minutes to learn. Only the shape of the mapping changes.

scaleSqrt and scalePow: sizing circles correctly

When you encode a value in the radius of a circle, the human eye reads area. If the value doubles and the radius doubles, the area quadruples and the data looks exaggerated. The fix is to make the radius proportional to the square root of the value.

JavaScript
const r = d3.scaleSqrt().domain([0, 1000]).range([0, 40]);
r(250);  // 20: a quarter of the value gives half the radius

scaleSqrt is shorthand for scalePow().exponent(0.5). A generic scalePow().exponent(k) curves the mapping with any exponent; k greater than 1 emphasizes large values.

Radius versus area

With the linear radius, a value 9 times larger has an area 81 times larger. With the square root it is exactly 9 times, as it should be.

scaleLog: data spanning orders of magnitude

A log scale maps equal ratios to equal distances: 1 to 10 takes as much space as 10 to 100. It suits data like populations, file sizes or frequencies. The domain must not include zero (log of zero is undefined) and must not cross zero, so choose a positive domain such as [1, 1e6].

JavaScript
const x = d3.scaleLog().domain([1, 1_000_000]).range([0, 600]);
x(1000);   // 300
x.ticks()  // [1, 10, 100, ...] style ticks

The default base is 10; change it with .base(2). Axes on a log scale need a tick formatter, such as d3.format("~s"), otherwise the labels get crowded.

scaleSymlog: log-like, but zero-friendly

When data contains zeros or negatives, scaleSymlog bends in a log-like way yet is linear near zero. Use it for values like "number of incidents" which is often 0 and sometimes 10,000. You tune the linear region with .constant(c) (default 1).

Linear, log and symlog compared

On the linear scale, the four smaller values pile up at the left. The log scale spreads them evenly. log(0) returns a non-finite number (it prints as null in the JSON-style log), the sign that the domain should have been positive.

scaleTime and scaleUtc: dates

scaleTime takes Date objects as its domain and positions them on a time line. Ticks fall on sensible calendar boundaries (months, weeks, days) in the local time zone; scaleUtc does the same in UTC and is safer when your data is stored in UTC.

JavaScript
const x = d3.scaleTime()
  .domain([new Date(2025, 0, 1), new Date(2025, 11, 31)])
  .range([0, 600]);

x(new Date(2025, 6, 1));       // about 300
x.invert(300);                 // a Date around July 1

A time scale's domain returns Date objects, so use d3.extent(data, d => d.date) after parsing the dates (see Loading Data). Ticks and labels are produced by the axis, covered in Axes.

A time scale and its ticks

scaleRadial and sequential helpers

  • scaleRadial maps values to radii so that area (not radius) is linear in the value. It is made for radial charts such as radial bars and polar plots.
  • scaleSequential (and its log, pow, sqrt, symlog versions) maps a domain to a color interpolator and is used for heatmaps (see Color Scales).
  • scaleQuantize, scaleQuantile and scaleThreshold map continuous values into buckets, for choropleth maps and binned legends.
  • scaleIdentity returns its input unchanged; it appears mainly in tooling.

How to choose

Data Scale
Ordinary quantities, positions scaleLinear
Circle radius for area encoding scaleSqrt
Spans several orders of magnitude, all positive scaleLog
Spans orders of magnitude and includes zero or negatives scaleSymlog
Dates and times scaleTime or scaleUtc
Categories band, point or ordinal scales (next lesson)

All continuous scales share one rule: the number of values in domain and range must match when you use more than two (piecewise). With two values in each, the scale is a simple interpolation. A mismatch gives surprising results because the longer array is trimmed.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why use scaleSqrt for circle radius?

    Readers perceive area. With a sqrt scale, doubling the value doubles the area, which is honest.
  2. 2. What must be true of a log scale's domain?

    log(0) is undefined. For data that includes zero use scaleSymlog.
  3. 3. What does scaleTime().domain() contain?

    Time scales work with Date objects and return Dates from invert and domain.
  4. 4. Which scale suits a variable 'number of incidents' that ranges from 0 to 10,000?

    Symlog is log-like for large values but handles zero because it is linear near zero.

Summary

  • All continuous scales share the same API; only the shape of the mapping differs.
  • Use scaleSqrt for circle radius, scaleLog for wide positive ranges and scaleSymlog when zeros appear.
  • scaleTime and scaleUtc map Date domains and generate calendar-aware ticks.
  • Prefer UTC scales for UTC data to avoid time-zone surprises.

Next: Band, Point and Ordinal Scales