Scale access and pixel-to-value inversion for the Observable Plot adapter.
Plot draws marks at pixel coordinates and keeps the scales that produced
them on the returned node. Recovering a datum is therefore a matter of
reading an element's geometry and running it back through the right scale —
invert for a continuous axis, a walk of the domain for a band one.
Two things make that less mechanical than it sounds, and both are handled
here:
Float noise. Inverting a pixel is a divide followed by a multiply, so
a bar drawn for 3.14159 inverts to 3.141589999999809. Announcing that
to a screen reader is a defect, not a rounding detail, so every inverted
number goes through cleanNumber.
A missing scale. The function is an own property of the node Plot
returned; it does not survive serialization. A plot found in a static
HTML file has the markup and not the scales, so deriveScale fits
one from the axis ticks Plot rendered.
Scale access and pixel-to-value inversion for the Observable Plot adapter.
Plot draws marks at pixel coordinates and keeps the scales that produced them on the returned node. Recovering a datum is therefore a matter of reading an element's geometry and running it back through the right scale —
invertfor a continuous axis, a walk of the domain for a band one.Two things make that less mechanical than it sounds, and both are handled here:
3.14159inverts to3.141589999999809. Announcing that to a screen reader is a defect, not a rounding detail, so every inverted number goes through cleanNumber.scale. The function is an own property of the node Plot returned; it does not survive serialization. A plot found in a static HTML file has the markup and not the scales, so deriveScale fits one from the axis ticks Plot rendered.