Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 44 ยท Scales and Axes

Band, Point and Ordinal Scales in D3

Position categories with scaleBand and scalePoint, control padding and bandwidth, and map categories to colors or shapes with scaleOrdinal.

Linear and time scales handle numbers. But a bar chart of "Mon, Tue, Wed" needs to place categories: discrete values with no natural arithmetic. D3's band, point and ordinal scales do that. They take a list of categories as the domain and return positions or other visual values.

scaleBand: slots for bars

scaleBand divides a continuous range into equal "bands", one per category, with optional gaps. Calling the scale with a category returns the start of that category's band, and bandwidth() returns the width of a band.

JavaScript
const x = d3.scaleBand()
  .domain(["Mon", "Tue", "Wed", "Thu"])
  .range([0, 400])
  .padding(0.2);

x("Tue");        // start pixel of the Tue band
x.bandwidth();   // width of one band
x.step();        // distance between starts of two bands

Draw a bar with x(d.name) as x and x.bandwidth() as width. Padding is a fraction (0 to 1) of the step: .paddingInner() controls gaps between bands, .paddingOuter() the space at the two ends, .padding() sets both, and .align(0.5) decides how leftover space is distributed. The domain is a list of unique values; D3 will not stop you from passing duplicates but later duplicates are ignored.

A band scale positions the bars

Try .padding(0): the bars touch each other. Try .padding(0.8): thin bars. A band scale for an unknown category returns undefined.

Sorting categories

The order of the domain is the order on screen. To sort by value, sort the data before building the domain:

JavaScript
const sorted = d3.sort(data, (a, b) => d3.descending(a.n, b.n));
x.domain(sorted.map(d => d.day));

Or use d3.groupSort(data, g => -d3.sum(g, d => d.n), d => d.day) to order categories by an aggregate directly.

scalePoint: positions for dots and lines

scalePoint is scaleBand with the bandwidth fixed at 0: each category maps to a point, evenly spaced. Use it for dot plots, lollipop charts and line charts over categories.

JavaScript
const x = d3.scalePoint(["Q1", "Q2", "Q3", "Q4"], [0, 300]).padding(0.5);
x("Q1");   // 37.5

Note that padding on a point scale means outer padding only (there is no inner gap).

scalePoint and horizontal bars

For horizontal bars, put the band scale on the y axis and the linear scale on x. Nothing else changes.

scaleOrdinal: categories to anything

scaleOrdinal maps each domain value to the corresponding range value, and cycles through the range if there are more categories than outputs. It is the standard way to map categories to colors, shapes, or labels.

JavaScript
const color = d3.scaleOrdinal()
  .domain(["cats", "dogs", "birds"])
  .range(["#e8590c", "#1971c2", "#2f9e44"]);

color("dogs");   // "#1971c2"
color("fish");   // "#e8590c" (implicitly added: takes the next range value)

If you pass a value not in the domain, D3 adds it to the domain and gives it the next color. That implicit extension is convenient for exploration but makes colors depend on call order, which is bad for a stable legend. Set .unknown("#aaa") to prevent the growth and show a neutral grey instead.

The module ships with ready-made color palettes as arrays: d3.schemeCategory10, d3.schemeTableau10, d3.schemeSet2, d3.schemePaired and more, which you can plug in as the range.

Ordinal scales for colors

Which scale for which job?

Scale Domain Range Typical use
scaleBand categories continuous interval bar charts
scalePoint categories continuous interval dot, lollipop, parallel coordinates
scaleOrdinal categories any list of values colors, shapes, dash patterns

For bars you can also use a band scale to place a second level inside each band (grouped bars): create an outer band scale for the groups and an inner one over outer.bandwidth() for the series. The Bar Charts lesson builds this.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does x(d.name) return for a scaleBand, and what is bandwidth()?

    A band scale returns the left (start) position, and bandwidth gives the band's width, ready for a bar's x and width.
  2. 2. What is the relationship between scalePoint and scaleBand?

    Point scales are band scales with bandwidth fixed at zero, so each category is a single position.
  3. 3. Why call .unknown('#aaa') on an ordinal color scale?

    By default ordinal scales add unseen values to the domain. unknown() prevents that implicit growth and gives stable colors.
  4. 4. How do you make horizontal bars?

    Swap the roles: category band scale on y and value scale on x. Widths follow the value.

Summary

  • scaleBand gives each category a start position and a bandwidth(); padding controls gaps.
  • scalePoint gives single positions, perfect for dots and lines over categories.
  • scaleOrdinal maps categories to colors, shapes or any list, and unknown() stops implicit domain growth.
  • The domain order is the display order; sort the data first to sort the chart.

Next: Color Scales and Interpolators