Skip to content
Open
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
11 changes: 6 additions & 5 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,17 +82,18 @@ 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);
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);
});
}

Expand Down
92 changes: 92 additions & 0 deletions test/pointer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -126,6 +136,88 @@ 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("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: [
Expand Down