liveBook · Manning

D3-scale

As explained in D3.js in Action, Third Edition

d3-scale is a D3.js module providing functions for mapping data domains to output ranges.

d3-scale maps data domains to output ranges. An ordinal scale maps discrete labels, such as language families, to discrete values, such as colors. section 11.2.2

Build a color scale for a hierarchical circle pack:

  • Install the module with npm install d3-scale. section 11.2.2
  • Import scaleOrdinal and derive the domain and range with JavaScript map(). section 11.2.2
  • Apply the scale to depth-1 language-family circles; interpolate parent colors toward white for deeper branches. section 11.2.2
Listing 11.11 Creating the color scale (scales.js)
import { scaleOrdinal } from "d3-scale";
import { languageFamilies } from "./helper";

export const colorScale = scaleOrdinal()
  .domain(languageFamilies.map(d => d.label))
  .range(languageFamilies.map(d => d.color));

Inspect the complete ordinal scale definition, including its language-family domain and associated color range. section 11.2.2

How D3.js in Action, Third Edition covers it

How deep it goes
Defines and applies an ordinal scale, then combines it with interpolation for depth-based color gradation.
Examples
Uses language-family labels and hexadecimal colors to color circles across four hierarchy depths.
What you can do after
Apply it via d3-scale to assign category-specific colors in a D3 visualization.
How it connects
Connects ordinal scales with D3 pack layouts, hierarchical data, color interpolation, and SVG attributes.

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.