liveBook · Manning

Linear scale

As explained in D3.js in Action, Third Edition

A linear scale maps continuous input values to output values in direct proportion across its domain and range.

A linear scale turns a continuous input value into a continuous output value in direct proportion. In D3, `d3.scaleLinear()` creates a function: `domain()` sets the input interval, and `range()` sets the output interval. For example, a domain of `[0, 250]` and range of `[0, 25]` map 100 to 10 and 150 to 15. Use linear scales for quantitative data, such as counts, rather than category names. section 3.4.2 section 3.1.2

A scale is useful when raw data values do not fit the space available on screen. For a bar chart, `d3.scaleLinear().domain([0, 1078]).range([0, 450])` maps the largest count to a 450-pixel bar. Starting the domain at zero gives the bars a zero baseline; limiting the range leaves room for labels. Apply the scale when setting each rectangle’s width: `.attr("width", d => xScale(d.count))`. section 3.4.2

Position and size histogram bars

A histogram uses two linear scales. The horizontal scale maps salary-bin boundaries across the chart width. The vertical scale maps bin counts to positions, with a range from `innerHeight` to `0` because SVG’s vertical coordinates increase downward. For each bin, set its x-position from the lower boundary, calculate its width from the difference between scaled boundaries, and calculate its height from the scaled count. The same scales can supply the chart’s axes. section 6.2

Listing 6.2 Declaring the scales (histogram.js)
const minSalary = bins[0].x0;                    #1
const maxSalary = bins[bins.length - 1].x1;     




const xScale = d3.scaleLinear()                 #2
  .domain([minSalary, maxSalary])               
  .range([0, innerWidth]);                     

const binsMaxLength = d3.max(bins, d => d.length);   #3
const yScale = d3.scaleLinear()    #4
  .domain([0, binsMaxLength])     
  .range([innerHeight, 0])        
  .nice();

See how the histogram’s two domains and ranges are declared, including the reversed vertical range. section 6.2

Keep labels readable on smaller screens

A linear scale can map screen width to font size instead of mapping data to a shape. For a responsive line chart, `.domain([600, 1000]).range([26, 16]).clamp(true)` assigns larger text on narrower screens. `clamp(true)` keeps outputs within the chosen font-size range. Reapply the scale on a window resize so labels change when the screen width changes. section 9.2.1

This also helps accessibility: SVG text can shrink with the rest of a chart until axis labels are hard to read on a phone. A screen-width-to-font-size scale can compensate, while adjusted margins make room for the larger labels. section 10.2.1

How D3.js in Action, Third Edition covers it

How deep it goes
Works through domain and range configuration, proportional outputs, SVG attributes, reversed vertical ranges, clamping, and resize handling.
Examples
Uses a 100-to-10 mapping, count-based bar widths, salary-bin rectangles, and screen-width-based label sizes.
What it assumes
Introduces continuous domains and ranges through numeric examples while using D3, SVG rectangles, and JavaScript functions in chart code.
What you can do after
Apply it via D3 `scaleLinear()` to size and position chart elements or adjust label sizes across screen widths.
How it connects
Connects quantitative data, SVG data binding, bar charts, histograms, axes, and responsive typography.

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.