Blog · Frontend · · 4 min read
Morphing box-whisker plots with @property
Hovering across taxa in Ptree while a numeric overlay like Mass, Longevity, or Temperature is active glides the legend's box-whisker plot smoothly from one clade's distribution to the next. Whiskers, interquartile box, median tick, geometric-mean diamond, and endpoint labels all move together, yet the legend code contains no tweening, no timers, and no layout measurement.
Six registered percentages on one static track
Every numeric property has its own permanent .track element rendered in the HTML at build time, carrying its own inline --ramp background so switching properties slides a carousel of complete keys without flashing neighboring colors. Six CSS custom properties are registered as <percentage>:
@property --p5 { syntax: "<percentage>"; inherits: true; initial-value: 0%; }
@property --q1 { syntax: "<percentage>"; inherits: true; initial-value: 0%; }
@property --med { syntax: "<percentage>"; inherits: true; initial-value: 0%; }
@property --q3 { syntax: "<percentage>"; inherits: true; initial-value: 0%; }
@property --p95 { syntax: "<percentage>"; inherits: true; initial-value: 0%; }
@property --gmean { syntax: "<percentage>"; inherits: true; initial-value: 0%; }
Because each variable is registered with <percentage>, the .track transitions all six directly:
fieldset .track {
transition: --p5 var(--shift) ease, --q1 var(--shift) ease, --med var(--shift) ease,
--q3 var(--shift) ease, --p95 var(--shift) ease, --gmean var(--shift) ease;
}
The child elements read those inherited variables through inset-inline and calc():
fieldset .track .whisker {
inset-inline: min(var(--p5), var(--p95)) calc(100% - max(var(--p5), var(--p95)));
}
fieldset .track .box {
inset-inline: min(var(--q1), var(--q3)) calc(100% - max(var(--q1), var(--q3)));
}
fieldset .track .median { inset-inline-start: calc(var(--med) - 1px); }
fieldset .track .gmean { inset-inline-start: calc(var(--gmean) - 4px); }
Using min() and max() handles channel properties like temperature or depth, where independently summarized lower and upper bounds can occasionally cross without mutating either underlying number.
When you hover a taxon, the legend updater computes the six positions as percentages along the property's fixed log or linear domain, writes each changed custom property once, and updates two text nodes (.lolab and .hilab). The browser interpolates every primitive in CSS.
Crossfading modes instead of morphing shapes
A clade with many measured descendants shows a whisker (p5–p95), an interquartile box (q1–q3), a vertical .median bar, and a .gmean diamond. A species or clade with a single measured contributor (count === 1) has no spread, only one point. Rather than transforming the median line into a diamond in JavaScript, the track keeps .median and .point as two separate elements bound to the same --med position. Toggling .point on .track simply crossfades their opacities:
fieldset .track.point :is(.whisker, .box, .median, .lolab),
fieldset .track:not(.point, .envelope) .point {
opacity: 0;
}
Envelope properties (like depth, which carries both an observed outer extent and a normal range around a typical value) reuse the exact same six variables and elements, toggling .nocore, .noextent, or .nocenter when a taxon reports only part of the envelope. Paired axes (like two-sided properties sharing a central .origin) mirror the first track with transform: scaleX(-1) and un-mirror its labels with scale: -1 1, reusing the entire box-whisker stylesheet with zero duplicated geometry rules.
Endpoint labels that drop instead of colliding
Numeric labels sit outside the whisker's ends so they never obscure the box. Two zero-sized anchor markers (.glyphlo and .glyphhi) ride at min(var(--p5), var(--p95)) and max(var(--p5), var(--p95)), publishing --glyph-low and --glyph-high inside anchor-scope on .track. The .lolab and .hilab labels attach to those anchors with CSS anchor positioning:
fieldset .track :is(.lolab, .hilab) {
position: absolute;
position-visibility: no-overflow;
inline-size: max-content;
font-variant-numeric: tabular-nums;
}
fieldset .track .lolab {
position-anchor: --glyph-low;
position-area: inline-start;
justify-self: unsafe end;
margin-inline-end: var(--margin);
}
When a clade sits near the edge of the track and its outer label no longer fits outside the whisker, position-visibility: no-overflow hides that label automatically instead of flipping it across the glyph into its peer. For a single-point taxon where .lolab is empty, .lolab:empty + .hilab:not(:empty) switches .hilab's position-anchor to --point so the lone value stays parked beside the diamond.
Range filter handles and the coverage waterline
Each numeric track also doubles as a dual-ended range filter via two overlapping native <input type="range"> elements (.from and .to). Both inputs have pointer-events: none while their thumbs restore pointer-events: auto, so drags land on whichever handle sits under the pointer. While .Filtering is active, each thumb casts a 100vw box-shadow outward (--cast: -100vw on .from, 100vw on .to), clipped cleanly to the track by overflow: hidden on the input itself. The area outside the chosen range dims behind a translucent veil with no extra overlay elements and no JavaScript width updates.
Categorical legends follow the same pattern. Each <dt> cell reads four registered <number> properties (--p, --cov, --waterline, --palette). Its ::before pseudo-element draws the solid category bar translated horizontally by (max(var(--p), var(--palette)) - 1) * 100%, and its ::after pseudo-element draws a translucent --nodata wash clipped to a sinusoidal wave via clip-path: shape() and raised vertically by --waterline (Math.log1p(cov / 0.03) / Math.log1p(1 / 0.03)). Hovering any taxon writes --cov, --waterline, and each cell's --p once; both the horizontal share bars and the vertical confidence waterline glide on the compositor.