Terminology (7)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Anchor
- A known pair of corresponding values, such as 100 ms drawn at 300 px. Two anchors with different input values fix a linear mapping.
- Domain and range
- The interval of input values a mapping is defined for, and the interval of output values it produces.
- Normalize Also t
- Express a value as its position between two anchors: t = (x − x₀)/(x₁ − x₀). The units cancel; 0 is the first anchor and 1 the second.
- Linear interpolation Also lerp
- Rebuild an output from a position: y = y₀ + t(y₁ − y₀). Between the anchors when 0 ≤ t ≤ 1.
- Extrapolation
- Applying the same linear formula outside the anchors, where t < 0 or t > 1.
- Clamp
- Hold a value inside an interval by replacing anything beyond an end with that end. A clamp changes where something is drawn, not what was measured.
- Inverse mapping
- The mapping from outputs back to inputs. An anchored linear mapping with nonzero input and output spans can be inverted by swapping the roles of its anchors. Clipping outside the axis loses the original value.
Why this matters
Progress bars, chart axes, sliders and animations all map a value from one range onto another. A scale that loses an anchor makes a quiet reading look loud, and a clamp that changes the value as well as the position makes an out-of-range sample look ordinary. A readout that inverts a scale with no inverse prints NaN or an invented number.
This lesson assumes fractions and signed arithmetic.
How two anchors define a scale
An anchor pairs an input value with its corresponding output, such as a latency with a pixel coordinate. Linear scaling uses two anchors with distinct input values: find the input’s fractional position between them, then use that position between the outputs. Linear here means equal steps in the input give equal steps on screen; the mapping says nothing about whether the measured quantity behaves linearly.
Interpolation locates a value between the anchors; extending the same rule beyond them is extrapolation. Clamping limits a drawn position to the visible interval. That display choice must remain separate from the measured reading, especially when a tooltip or inverse mapping uses the same scale.
01Define the quantities
Name both pairs of anchors
A latency chart’s axis runs from 100 ms at the bottom to 500 ms at the top, and SVG measures y downward. Its 150 ms marker landed at 216 px. Does that position agree with the axis, and what should happen when a reading goes beyond either end?
The operations dashboard uses these calibration values:
| Symbol | Meaning | Value |
|---|---|---|
| x₀ → y₀ | Bottom of the axis | 100 ms → 300 px |
| x₁ → y₁ | Top of the axis | 500 ms → 20 px |
| x | A reading to draw | 150 ms |
| y | Its coordinate (SVG y grows downward) | unknown |
02Normalize, then rebuild
Find the position, then rebuild the output
Subtracting x₀ measures from the first anchor; dividing by the span makes a unitless position t, which is 0 at the first anchor and 1 at the second. The output step reverses that work in the other range’s units. A negative output span, as here, needs no special case: it moves the point upward. Swapping the roles of the two ranges gives the inverse.
const [x0, x1] = [100, 500]; // ms at the ends of the axis
const [y0, y1] = [300, 20]; // px; SVG y grows downward
const t = (150 - x0) / (x1 - x0);
const y = y0 + t * (y1 - y0);
const back = x0 + ((y - y0) / (y1 - y0)) * (x1 - x0);
console.log(`t = ${t}`);
console.log(`y = ${y}`);
console.log(`back = ${back}`);
The formula y0 + t * (y1 - y0) is the whole interpolation; TypeScript has no built-in lerp. Floating rounding can leave a result a tiny distance from y1 at t = 1.
Each version prints t = 0.125, y = 265 and the round trip back = 150.
03Clip the drawing
Clip the drawing, keep the reading
A 520 ms reading has t = 1.05 and would be drawn above the top of the plot. Holding the marker at the edge is a drawing decision. The reading is still 520 ms, so the tooltip should say so and say that the marker is clipped. Predict what this clamp prints for a point that is inside the plot.
const [y0, y1] = [300, 20]; // px anchors of an upside-down axis
const y = 265; // a point inside the plot
console.log(Math.min(Math.max(y, y0), y1));
Clamping the position t to [0, 1] works whichever way either axis runs, because t always goes from 0 at the first anchor to 1 at the second. A reading exactly at an anchor has t = 0 or 1 and is inside the plot.
04Read back a pointer
Read a pointer back through the same anchors
A hover readout runs the mapping backward: from a pointer coordinate to a reading. Use the same anchors with their roles swapped. On a 0–400 ms axis drawn from 300 px to 20 px, a pointer at 125 px has position (125 − 300) ÷ (20 − 300) = 0.625, so it reads 250 ms.
05Ranges without an inverse
Some ranges have no inverse
The inverse divides by the output span y₁ − y₀. If both anchors share one coordinate, every reading is drawn at that pixel, and a pointer there cannot say which reading it is. That readout is ambiguous; dividing anyway gives NaN or an infinity, not an answer.
A collapsed input span is a different case. A chart that sets its domain from the minimum and maximum of a flat data window gets x₀ = x₁, say 75 ms at both ends. The forward mapping divides by zero, so the chart needs a drawing policy, such as padding the domain. For its readout, the dashboard can still map the nonzero pixel span to the constant 75 ms value. That is a useful readout policy for a flat window, rather than an inverse of the undefined forward mapping. A pointer beyond the pixel span still gets a clipped label. An axis with zero pixel span always reports ambiguous in this lesson, even if the data window is flat: there is no axis segment to locate the pointer on.
06Vary inputs, then decide
Vary one input at a time, then decide
| Case | t | Drawn at | Tooltip |
|---|---|---|---|
| 150 ms | 0.125 | 265 px | 150 ms |
| 500 ms | 1 | 20 px | 500 ms |
| 520 ms | 1.05 | 20 px | 520 ms, beyond the axis |
| 60 ms | −0.1 | 300 px | 60 ms, beyond the axis |
| 150 ms on 0–500 ms | 0.3 | 216 px | 150 ms |
The last row is the same reading on a different axis. It is the 216 px the dashboard drew, which is correct only if the axis starts at zero. Changing an anchor changes every coordinate; clamping changes only where outside readings are drawn.
07Build the chart axis
Build one chart axis
Repair the misplaced marker first. Then keep readings beyond the axis visible at its edges, with their original values intact. The final task lets a pointer use the same anchors and clipping policy in reverse, including an axis too short to read. The complete program supplies the other working helpers for each task.
A latency chart
Place a reading, clip the drawing, then read the pointer back.
In the frontend Chart markers in Next.js and Svelte Optional
An SVG chart marker is this lesson’s mapping. SVG’s y axis grows downward, so the top of the plot has the smaller coordinate and the output span is negative. Clamp the position before computing y, and let the tooltip report the reading itself.
In Next.js the marker can be a server or client component; the mapping is plain
arithmetic during render. In Svelte, $derived recomputes t and y when the
props change. Neither framework changes the anchors or the clamp. These marker excerpts
require distinct domain ends (min !== max). Pad a flat data window's domain
before passing it to the marker.
type Props = { value: number; min: number; max: number }; // ms
const TOP = 20; // px
const BOTTOM = 300; // px; SVG y grows downward
export function LatencyMarker({ value, min, max }: Props) {
const t = (value - min) / (max - min);
const drawn = Math.min(Math.max(t, 0), 1);
const y = BOTTOM + drawn * (TOP - BOTTOM);
return (
<g>
<circle cx={40} cy={y} r={4} />
<title>{`${value} ms${drawn !== t ? " (beyond the axis)" : ""}`}</title>
</g>
);
}
See SVG’s initial coordinate system, the title element and Svelte $derived.
Check this idea later
Sources
- C++: std::lerp and std::clamp
- Rust: f64::clamp
- Go: built-in min and max
- ECMAScript: Math.min and Math.max
- SVG 2: coordinate system
The chart and its readings are a worked model, not measured latencies.
Sign in to keep your read marks on every device. Sign in
Was this helpful?