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 translates a continuous input domain into a continuous output range while preserving direct proportion. In D3, create one with d3.scaleLinear(), then set its input limits with domain() and its output limits with range(). A value halfway through the domain maps halfway through the range. section 3.4.2 section 3.6

const myLinearScale = d3.scaleLinear()
  .domain([0, 250])
  .range([0, 25]);

This scale maps values from 0–250 into 0–25; calling it with 100 returns 10 and calling it with 150 returns 15. section 3.4.2

Use a linear scale when quantitative values cannot be used directly as SVG pixels or when a visual attribute needs a different size, position, or color range. For a bar chart, count values can be mapped into the horizontal space available for bars instead of overflowing a smaller SVG container. section 3.0 section 3.4.2

Size bar-chart rectangles

Define an xScale with a domain from 0 to the largest count and a range from 0 to the usable chart width. Then pass each bound datum’s count to the scale: .attr("width", d => xScale(d.count)). Starting the domain at 0 gives the x-axis the usual zero baseline. section 3.4.2

Position and size histogram bars

A histogram uses one linear scale for salary-bin boundaries along the x-axis and another for bin frequencies on the y-axis. Because SVG’s y-axis increases downward, the y scale’s range runs from innerHeight to 0. Bar width is xScale(d.x1) - xScale(d.x0), and height is innerHeight - yScale(d.length). section 6.2

Keep labels readable on smaller screens

A linear scale can map screen width to font size. Set a larger font size for narrow screens and a smaller one for wide screens, then use clamp(true) to prevent values outside that chosen range. Update text styles again when the window resize event fires. section 9.2.1 section 10.2.1

Linear scales fit quantitative, continuous data. For discrete categories such as names or labels, use a scale designed for qualitative data; band scales are particularly useful for spreading bar-chart rectangles across available space. section 3.1.2 section 3.6

How D3.js in Action, Third Edition covers it

How deep it goes
Defines proportional continuous mapping and works through bar-chart, histogram, and responsive-label implementations.
Examples
Demonstrates 0–250 to 0–25 mapping, count-width bars, salary histograms, and screen-width-driven label sizing.
What it assumes
Introduces scale use alongside D3 data binding and SVG attributes without requiring advanced mathematical formalism.
What you can do after
Apply it via D3 scaleLinear() to map continuous values into chart geometry or text sizes.
How it connects
Connects quantitative data, SVG attributes, bar charts, histograms, axes, band scales, and window resize events.

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.