Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/interactions/pointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand All @@ -91,7 +91,7 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op
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()) if (c.ii != null && (!best || c.ri < best.ri)) best = c;
for (const c of pool.map.values()) c.render(c === best ? c.ii : null);
});
}
Expand Down
33 changes: 33 additions & 0 deletions test/pointer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,39 @@ describe("pointer pool", () => {
assert.strictEqual(visibleTips(svg), 1);
});

it("shows the tip in range when another tip has a smaller radius and no point in range", () => {
const svg = Plot.plot({
x: {domain: [0, 10]},
y: {domain: [0, 10]},
marks: [
Plot.dot([{x: 2, y: 2, id: "near"}], {x: "x", y: "y", title: "id", tip: true}),
Plot.dot([{x: 9, y: 9, id: "far"}], {x: "x", y: "y", title: "id", tip: {maxRadius: 1}})
]
});
pointer(svg, "pointermove", svg.scale("x").apply(2) + 10, svg.scale("y").apply(2));
flushAnimationFrame();
assert.strictEqual(visibleTips(svg), 1);
assert.include(svg.querySelector("[aria-label=tip] text").textContent, "near");
});

for (const tipFirst of [true, false]) {
it(`pools only participating marks when the tip is ${tipFirst ? "first" : "last"}`, () => {
const data = [{x: 2, y: 2, id: "near"}];
const first = Plot.tip(data, Plot.pointer({x: "x", y: "y", title: "id"}));
const second = Plot.tip([{x: 2.2, y: 2, id: "farther"}], Plot.pointer({x: "x", y: "y", title: "id"}));
const ring = Plot.dot([{x: 2.4, y: 2}], Plot.pointer({x: "x", y: "y", pool: false}));
const svg = Plot.plot({
x: {domain: [0, 10]},
y: {domain: [0, 10]},
marks: tipFirst ? [first, second, ring] : [ring, first, second]
});
pointer(svg, "pointermove", svg.scale("x").apply(2), svg.scale("y").apply(2));
flushAnimationFrame();
assert.strictEqual(visibleTips(svg), 1);
assert.isNotNull(svg.querySelector("[aria-label=dot] circle"));
});
}

it("crosshair renders all sub-marks (does not pool)", () => {
const svg = Plot.plot({
marks: [Plot.crosshair([{x: 1, y: 1}, {x: 2, y: 2}], {x: "x", y: "y"})] // prettier-ignore
Expand Down