Skip to content

Commit 6bccc91

Browse files
committed
append tooltip asynchronously
1 parent f5dc9dc commit 6bccc91

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/marks/tooltip.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {pointer, select} from "d3";
1+
import {pointer} from "d3";
2+
import {create} from "../context.js";
23
import {formatDefault} from "../format.js";
34
import {Mark} from "../mark.js";
45
import {keyword, maybeFrameAnchor, maybeKeyword, maybeTuple, number, string} from "../options.js";
@@ -218,16 +219,7 @@ export class Tooltip extends Mark {
218219
}
219220
}
220221

221-
// We listen to the svg element; listening to the window instead would let
222-
// us receive pointer events from farther away, but would also make it hard
223-
// to know when to remove the listeners. (Using a mutation observer to watch
224-
// the entire document is likely too expensive.)
225-
svg.addEventListener("pointermove", pointermove);
226-
svg.addEventListener("pointerdown", pointerdown);
227-
svg.addEventListener("pointerleave", pointerleave);
228-
229-
const dot = select(svg)
230-
.append("g")
222+
const dot = create("svg:g", context)
231223
.attr("aria-label", "tooltip")
232224
.attr("pointer-events", "none") // initially not sticky
233225
.attr("display", "none");
@@ -245,6 +237,17 @@ export class Tooltip extends Mark {
245237
.call(applyIndirectTextStyles, this)
246238
.on("pointerdown pointermove", stopPropagation);
247239

240+
// We listen to the svg element; listening to the window instead would let
241+
// us receive pointer events from farther away, but would also make it hard
242+
// to know when to remove the listeners. (Using a mutation observer to watch
243+
// the entire document is likely too expensive.)
244+
svg.addEventListener("pointermove", pointermove);
245+
svg.addEventListener("pointerdown", pointerdown);
246+
svg.addEventListener("pointerleave", pointerleave);
247+
248+
// The tooltip is added asynchronously to draw on top of all other marks.
249+
Promise.resolve().then(() => svg.append(dot.node()));
250+
248251
return null;
249252
}
250253
}

test/output/tooltipBinStack.svg

Lines changed: 3 additions & 0 deletions
Loading

test/plots/tooltip.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export async function tooltipBinStack() {
1616
return Plot.plot({
1717
marks: [
1818
Plot.rectY(olympians, Plot.binX({y: "count"}, {x: "weight", fill: "sex"})),
19-
Plot.tooltip(olympians, Plot.stackY({}, Plot.binX({y: "count"}, {x: "weight", fill: "sex", axis: "x"})))
19+
Plot.tooltip(olympians, Plot.stackY({}, Plot.binX({y: "count"}, {x: "weight", fill: "sex", axis: "x"}))),
20+
Plot.ruleY([0])
2021
]
2122
});
2223
}

0 commit comments

Comments
 (0)