From 620fa62fe84e2529ae5c97a4d9a0169f7e6eba09 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 1 Dec 2026 03:30:31 -0400 Subject: [PATCH 1/2] fix: respect pointer pooling per mark --- src/interactions/pointer.js | 4 ++-- test/pointer-test.js | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/interactions/pointer.js b/src/interactions/pointer.js index 7aa19f25f9..0999072d59 100644 --- a/src/interactions/pointer.js +++ b/src/interactions/pointer.js @@ -33,7 +33,7 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op // the pointer (e.g., tips); only the closest point is shown. let state = states.get(svg); if (!state) { - state = {sticky: false, roots: [], renders: [], pool: this.pool ? {map: new Map()} : null}; + state = {sticky: false, roots: [], renders: []}; states.set(svg, state); } @@ -82,7 +82,7 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op // only show the closest. We defer rendering using an animation frame to // allow all pointer events to be received before deciding which mark to // render; although when hiding, we render immediately. - const pool = state.pool ?? facetPool; + const pool = this.pool ? (state.pool ??= {map: new Map()}) : facetPool; function update(ii, ri) { if (!pool) return void render(ii); if (ii == null) render(ii); diff --git a/test/pointer-test.js b/test/pointer-test.js index f00282f0ac..4fc0093d2c 100644 --- a/test/pointer-test.js +++ b/test/pointer-test.js @@ -126,6 +126,27 @@ describe("pointer pool", () => { assert.strictEqual(visibleTips(svg), 1); }); + it.each([false, true])("pool: false keeps the highlight independent (rendered first: %s)", (highlightFirst) => { + const near = Plot.tip([{x: 1, y: 1, title: "near"}], Plot.pointer({x: "x", y: "y", title: "title"})); + const far = Plot.tip([{x: 1.01, y: 1.01, title: "far"}], Plot.pointer({x: "x", y: "y", title: "title"})); + const highlight = Plot.dot( + [{x: 1.02, y: 1.02}], + Plot.pointer({x: "x", y: "y", pool: false, r: 8, stroke: "red", fill: "none"}) + ); + const svg = Plot.plot({ + x: {domain: [1, 2]}, + y: {domain: [1, 2]}, + marks: highlightFirst ? [highlight, near, far] : [near, far, highlight] + }); + const x = svg.scale("x").apply(1); + const y = svg.scale("y").apply(1); + pointer(svg, "pointerenter", x, y); + pointer(svg, "pointermove", x, y); + flushAnimationFrame(); + assert.strictEqual(svg.querySelectorAll("[aria-label=dot] circle").length, 1); + assert.strictEqual(visibleTips(svg), 1); + }); + it("pointerleave hides all tips", () => { const svg = Plot.plot({ marks: [ From aacbd334231bc146176a9f8e7c9bab7aef59b7ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Tue, 1 Dec 2026 13:51:27 +0100 Subject: [PATCH 2/2] rank only pointers that picked something MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A pointer that picks nothing is removed from the pool instead of competing with maxRadius², so it can no longer hide a real pick (case 1 of #2475). This also restores the 0.6.17 facet behavior: the value resets to null on pointerleave, and the winning pick renders last so that a losing pointer cannot overwrite the dispatched value with null. --- src/interactions/pointer.js | 7 ++-- test/pointer-test.js | 71 +++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/interactions/pointer.js b/src/interactions/pointer.js index 0999072d59..1b4698fdcc 100644 --- a/src/interactions/pointer.js +++ b/src/interactions/pointer.js @@ -85,14 +85,15 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op const pool = this.pool ? (state.pool ??= {map: new Map()}) : facetPool; function update(ii, ri) { if (!pool) return void render(ii); - if (ii == null) render(ii); - pool.map.set(renderIndex, {ii, ri, render}); + if (ii == null) pool.map.delete(renderIndex), render(ii); + else pool.map.set(renderIndex, {ii, ri, render}); if (pool.frame !== undefined) cancelAnimationFrame(pool.frame); pool.frame = requestAnimationFrame(() => { pool.frame = undefined; let best = null; for (const c of pool.map.values()) if (!best || c.ri < best.ri) best = c; - for (const c of pool.map.values()) c.render(c === best ? c.ii : null); + for (const c of pool.map.values()) if (c !== best) c.render(null); + best?.render(best.ii); }); } diff --git a/test/pointer-test.js b/test/pointer-test.js index 4fc0093d2c..870be73d8d 100644 --- a/test/pointer-test.js +++ b/test/pointer-test.js @@ -50,6 +50,16 @@ function visibleTips(svg) { return [...svg.querySelectorAll("[aria-label=tip]")].filter((g) => g.querySelector("text")).length; } +// The pointer position of the point (x, y) in the facet f. +function facetPoint(svg, f, x, y) { + const fx = svg.scale("fx"); + const fy = svg.scale("fy"); + return [ + svg.scale("x").apply(x) + (fx ? fx.apply(f) - svg.scale("x").range[0] : 0), + svg.scale("y").apply(y) + (fy ? fy.apply(f) - svg.scale("y").range[1] : 0) + ]; +} + describe("pointer pool", () => { let pointer, flushAnimationFrame; @@ -147,6 +157,67 @@ describe("pointer pool", () => { assert.strictEqual(visibleTips(svg), 1); }); + it("a pointer that picks nothing does not outrank a pick", () => { + const svg = Plot.plot({ + x: {domain: [0, 10]}, + y: {domain: [0, 10]}, + marks: [ + Plot.dot([{id: "near", x: 2, y: 2}], {x: "x", y: "y", title: "id", tip: true}), + Plot.dot([{id: "far", x: 9, y: 9}], {x: "x", y: "y", title: "id", tip: {maxRadius: 1}}) + ] + }); + const x = svg.scale("x").apply(2) + 10; + const y = svg.scale("y").apply(2); + pointer(svg, "pointerenter", x, y); + pointer(svg, "pointermove", x, y); + flushAnimationFrame(); + assert.strictEqual(visibleTips(svg), 1); + }); + + it.each([ + {fx: "f", tip: "x"}, + {fx: "f", tip: {pointer: "x", pool: false}}, + {fy: "f", tip: "y"}, + {fy: "f", tip: {pointer: "y", pool: false}} + ])("a facet that picks nothing does not outrank a pick (%o)", (options) => { + const data = [{f: "a", x: 1, y: 1}, {f: "b", x: 1, y: 1}]; // prettier-ignore + const svg = Plot.plot({ + x: {domain: [0, 2]}, + y: {domain: [0, 2]}, + marks: [Plot.dot(data, {x: "x", y: "y", ...options})] + }); + let [x, y] = facetPoint(svg, "a", 1, 1); + if (options.fx) y -= 60; // facet b picks nothing + else x += 60; + pointer(svg, "pointerenter", x, y); + pointer(svg, "pointermove", x, y); + flushAnimationFrame(); + assert.strictEqual(visibleTips(svg), 1); + assert.strictEqual(svg.value, data[0]); + pointer(svg, "pointerleave", 0, 0); + flushAnimationFrame(); + assert.strictEqual(svg.value, null); + }); + + it("a faceted tip dispatches the value of the closest pick", () => { + const data = [{f: "a", x: 2, y: 5}, {f: "b", x: 0, y: 5}]; // prettier-ignore + const svg = Plot.plot({ + x: {domain: [0, 2]}, + y: {domain: [0, 10]}, + marks: [Plot.dot(data, {x: "x", y: "y", fx: "f", tip: "x"})] + }); + const [xa, y] = facetPoint(svg, "a", 2, 5); // both facets pick between xa and xb + const [xb] = facetPoint(svg, "b", 0, 5); + pointer(svg, "pointerenter", xb - 1, y); + pointer(svg, "pointermove", xb - 1, y); + flushAnimationFrame(); + assert.strictEqual(svg.value, data[1]); + pointer(svg, "pointermove", xa + 1, y); + flushAnimationFrame(); + assert.strictEqual(visibleTips(svg), 1); + assert.strictEqual(svg.value, data[0]); + }); + it("pointerleave hides all tips", () => { const svg = Plot.plot({ marks: [