|
| 1 | +import {pointer as pointof} from "d3"; |
| 2 | +import {applyFrameAnchor} from "../style.js"; |
| 3 | + |
| 4 | +const states = new WeakMap(); |
| 5 | + |
| 6 | +function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, ...options} = {}) { |
| 7 | + maxRadius = +maxRadius; |
| 8 | + // When px or py is used, register an extra channel that the pointer |
| 9 | + // interaction can use to control which point is focused; this allows pointing |
| 10 | + // to function independently of where the downstream mark (e.g., a tip) is |
| 11 | + // displayed. Also default x or y to null to disable maybeTuple etc. |
| 12 | + if (px != null) (x ??= null), (channels = {...channels, px: {value: px, scale: "x"}}); |
| 13 | + if (py != null) (y ??= null), (channels = {...channels, py: {value: py, scale: "y"}}); |
| 14 | + return { |
| 15 | + x, |
| 16 | + y, |
| 17 | + channels, |
| 18 | + ...options, |
| 19 | + render(index, scales, values, dimensions, context) { |
| 20 | + const mark = this; |
| 21 | + const svg = context.ownerSVGElement; |
| 22 | + |
| 23 | + // Isolate state per-pointer, per-plot; if the pointer is reused by |
| 24 | + // multiple marks, they will share the same state (e.g., sticky modality). |
| 25 | + let state = states.get(svg); |
| 26 | + if (!state) states.set(svg, (state = {sticky: false, roots: [], renders: []})); |
| 27 | + |
| 28 | + // This serves as a unique identifier of the rendered mark per-plot; it is |
| 29 | + // used to record the currently-rendered elements (state.roots) so that we |
| 30 | + // can tell when a rendered element is clicked on. |
| 31 | + let renderIndex = state.renders.push(render) - 1; |
| 32 | + |
| 33 | + // For faceting, we want to compute the local coordinates of each point, |
| 34 | + // which means subtracting out the facet translation, if any. (It’s |
| 35 | + // tempting to do this using the local coordinates in SVG, but that’s |
| 36 | + // complicated by mark-specific transforms such as dx and dy.) Also, since |
| 37 | + // band scales return the upper bound of the band, we have to offset by |
| 38 | + // half the bandwidth. |
| 39 | + const {x, y, fx, fy} = scales; |
| 40 | + let tx = fx ? fx(index.fx) - dimensions.marginLeft : 0; |
| 41 | + let ty = fy ? fy(index.fy) - dimensions.marginTop : 0; |
| 42 | + if (x?.bandwidth) tx += x.bandwidth() / 2; |
| 43 | + if (y?.bandwidth) ty += y.bandwidth() / 2; |
| 44 | + |
| 45 | + // For faceting, we also need to record the closest point per facet per |
| 46 | + // mark (!), since each facet has its own pointer event listeners; we only |
| 47 | + // want the closest point across facets to be visible. |
| 48 | + const faceted = index.fi != null; |
| 49 | + let facetState; |
| 50 | + if (faceted) { |
| 51 | + let facetStates = state.facetStates; |
| 52 | + if (!facetStates) state.facetStates = facetStates = new Map(); |
| 53 | + facetState = facetStates.get(mark); |
| 54 | + if (!facetState) facetStates.set(mark, (facetState = new Map())); |
| 55 | + } |
| 56 | + |
| 57 | + // The order of precedence when determining the point position is: px & |
| 58 | + // py; the middle of x1 & y1 and x2 & y2; or lastly x & y. If any |
| 59 | + // dimension is unspecified, we fallback to the frame anchor. |
| 60 | + const {x: X, y: Y, x1: X1, y1: Y1, x2: X2, y2: Y2, px: PX, py: PY} = values; |
| 61 | + const [cx, cy] = applyFrameAnchor(this, dimensions); |
| 62 | + const px = PX ? (i) => PX[i] : X2 ? (i) => (X1[i] + X2[i]) / 2 : X ? (i) => X[i] : () => cx; |
| 63 | + const py = PY ? (i) => PY[i] : Y2 ? (i) => (Y1[i] + Y2[i]) / 2 : Y ? (i) => Y[i] : () => cy; |
| 64 | + |
| 65 | + let i; // currently focused index |
| 66 | + let g; // currently rendered mark |
| 67 | + let f; // current animation frame |
| 68 | + |
| 69 | + // When faceting, if more than one pointer would be visible, only show |
| 70 | + // this one if it is the closest. We defer rendering using an animation |
| 71 | + // frame to allow all pointer events to be received before deciding which |
| 72 | + // mark to render; although when hiding, we render immediately. |
| 73 | + function update(ii, ri) { |
| 74 | + if (faceted) { |
| 75 | + if (f) f = cancelAnimationFrame(f); |
| 76 | + if (ii == null) facetState.delete(index.fi); |
| 77 | + else { |
| 78 | + facetState.set(index.fi, ri); |
| 79 | + f = requestAnimationFrame(() => { |
| 80 | + f = null; |
| 81 | + for (const r of facetState.values()) { |
| 82 | + if (r < ri) { |
| 83 | + ii = null; |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + render(ii); |
| 88 | + }); |
| 89 | + return; |
| 90 | + } |
| 91 | + } |
| 92 | + render(ii); |
| 93 | + } |
| 94 | + |
| 95 | + function render(ii) { |
| 96 | + if (i === ii) return; // the tooltip hasn’t moved |
| 97 | + i = ii; |
| 98 | + const I = i == null ? [] : [i]; |
| 99 | + if (faceted) (I.fx = index.fx), (I.fy = index.fy), (I.fi = index.fi); |
| 100 | + const r = mark.render(I, scales, values, dimensions, context); |
| 101 | + if (g) { |
| 102 | + // When faceting, preserve swapped mark and facet transforms; also |
| 103 | + // remove ARIA attributes since these are promoted to the parent. This |
| 104 | + // is perhaps brittle in that it depends on how Plot renders facets, |
| 105 | + // but it produces a cleaner and more accessible SVG structure. |
| 106 | + if (faceted) { |
| 107 | + const p = g.parentNode; |
| 108 | + const ft = g.getAttribute("transform"); |
| 109 | + const mt = r.getAttribute("transform"); |
| 110 | + ft ? r.setAttribute("transform", ft) : r.removeAttribute("transform"); |
| 111 | + mt ? p.setAttribute("transform", mt) : p.removeAttribute("transform"); |
| 112 | + r.removeAttribute("aria-label"); |
| 113 | + r.removeAttribute("aria-description"); |
| 114 | + r.removeAttribute("aria-hidden"); |
| 115 | + } |
| 116 | + g.replaceWith(r); |
| 117 | + } |
| 118 | + state.roots[renderIndex] = r; |
| 119 | + return (g = r); |
| 120 | + } |
| 121 | + |
| 122 | + function pointermove(event) { |
| 123 | + if (state.sticky || (event.pointerType === "mouse" && event.buttons === 1)) return; // dragging |
| 124 | + let [xp, yp] = pointof(event); |
| 125 | + (xp -= tx), (yp -= ty); // correct for facets and band scales |
| 126 | + let ii = null; |
| 127 | + let ri = maxRadius * maxRadius; |
| 128 | + for (const j of index) { |
| 129 | + const dx = kx * (px(j) - xp); |
| 130 | + const dy = ky * (py(j) - yp); |
| 131 | + const rj = dx * dx + dy * dy; |
| 132 | + if (rj <= ri) (ii = j), (ri = rj); |
| 133 | + } |
| 134 | + update(ii, ri); |
| 135 | + } |
| 136 | + |
| 137 | + function pointerdown(event) { |
| 138 | + if (event.pointerType !== "mouse") return; |
| 139 | + if (i == null) return; // not pointing |
| 140 | + if (state.sticky && state.roots.some((r) => r?.contains(event.target))) return; // stay sticky |
| 141 | + if (state.sticky) (state.sticky = false), state.renders.forEach((r) => r(null)); // clear all pointers |
| 142 | + else state.sticky = true; |
| 143 | + event.stopImmediatePropagation(); // suppress other pointers |
| 144 | + } |
| 145 | + |
| 146 | + function pointerleave(event) { |
| 147 | + if (event.pointerType !== "mouse") return; |
| 148 | + if (!state.sticky) update(null); |
| 149 | + } |
| 150 | + |
| 151 | + // We listen to the svg element; listening to the window instead would let |
| 152 | + // us receive pointer events from farther away, but would also make it |
| 153 | + // hard to know when to remove the listeners. (Using a mutation observer |
| 154 | + // to watch the entire document is likely too expensive.) |
| 155 | + svg.addEventListener("pointerenter", pointermove); |
| 156 | + svg.addEventListener("pointermove", pointermove); |
| 157 | + svg.addEventListener("pointerdown", pointerdown); |
| 158 | + svg.addEventListener("pointerleave", pointerleave); |
| 159 | + |
| 160 | + return render(null); |
| 161 | + } |
| 162 | + }; |
| 163 | +} |
| 164 | + |
| 165 | +export function pointer(options) { |
| 166 | + return pointerK(1, 1, options); |
| 167 | +} |
| 168 | + |
| 169 | +export function pointerX(options) { |
| 170 | + return pointerK(1, 0.01, options); |
| 171 | +} |
| 172 | + |
| 173 | +export function pointerY(options) { |
| 174 | + return pointerK(0.01, 1, options); |
| 175 | +} |
0 commit comments