liveBook · Manning

Rule of three

As explained in D3.js in Action, Third Edition

The rule of three is a proportional-reasoning method for finding an unknown quantity from three known quantities in two equivalent ratios.

The rule of three finds an unknown quantity from two equivalent proportions. If A corresponds to B and C corresponds to X, calculate X as B × C ÷ A. section 10.2.1

Scaling responsive chart labels

A 15 px label in a 1,200 px SVG container would scale to about 4 px in a 315 px container: 315 × 15 ÷ 1,200 ≈ 4. To preserve a visual 15 px appearance on phones, calculate 15 × 1,200 ÷ 315 ≈ 57 px, then use a clamped linear scale to resize labels dynamically. section 10.2.1

Figure 10.5 Because the viewBox attribute of the SVG container is set and its width and height attributes are not, all the graphical elements resize with the screen. If the font size of the axis label is set to 15 px, they will look much smaller on phone screens. We can use a rule of three to find the actual font size that we need to apply on phone screens.
figure

Shows why SVG axis labels become visually tiny on phone screens and how the rule of three helps determine their responsive size. section 10.2.1

How D3.js in Action, Third Edition covers it

How deep it goes
Defines the formula, works through two label-size calculations, and connects the result to a clamped linear scale.
Examples
Uses 1,200-pixel desktop and 315-pixel phone SVG widths to calculate responsive axis-label sizes.
What you can do after
Apply it via D3 to calculate and dynamically resize chart text as the window changes.
How it connects
Connects proportional reasoning with responsive design, SVG, linear scales, and window resize handling.

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.