|
| 1 | +import {pointer, select} from "d3"; |
| 2 | +import {Mark} from "../mark.js"; |
| 3 | +import {maybeFrameAnchor, maybeTuple} from "../options.js"; |
| 4 | +import {applyFrameAnchor} from "../style.js"; |
| 5 | + |
| 6 | +export class Tooltip extends Mark { |
| 7 | + constructor(data, options = {}) { |
| 8 | + const {x, y, maxRadius = 40, frameAnchor} = options; |
| 9 | + super( |
| 10 | + data, |
| 11 | + { |
| 12 | + x: {value: x, scale: "x", optional: true}, |
| 13 | + y: {value: y, scale: "y", optional: true} |
| 14 | + }, |
| 15 | + options |
| 16 | + ); |
| 17 | + this.frameAnchor = maybeFrameAnchor(frameAnchor); |
| 18 | + this.indexesBySvg = new WeakMap(); |
| 19 | + this.maxRadius = +maxRadius; |
| 20 | + } |
| 21 | + render(index, {x, y, fx, fy}, {x: X, y: Y, channels}, dimensions, context) { |
| 22 | + // TODO |
| 23 | + // - ✅ Get local coordinates of the pointer |
| 24 | + // - ✅ Register one pointermove listener per plot |
| 25 | + // - ✅ Find the closest point in screen (scaled) coordinates |
| 26 | + // - ✅ Find the closest point across all facets |
| 27 | + // - ✅ Limit the search radius |
| 28 | + // - ✅ Suppress the tooltip on pointerdown |
| 29 | + // - ✅ Display unscaled values in a tooltip |
| 30 | + // - ✅ Handle x or y not existing; respect frameAnchor |
| 31 | + // - ✅ Handle fx or fy not existing (for the entire plot) |
| 32 | + // - Handle faceting being disabled for this mark (facet: null) |
| 33 | + // - Tooltips for rect, with configurable anchor point |
| 34 | + // - Tooltips for line, searching first on x and then on y, and vice versa |
| 35 | + // - Tooltips for area… matching between topline and baseline? |
| 36 | + // - Tooltips for link and arrow, treating {x1, y1} and {x2, y2} as distinct points |
| 37 | + // - Tooltips for vector, but matching the end of the vector instead of the start? |
| 38 | + // - Tooltips for tick and rule? |
| 39 | + // - Tooltips for bar and cell? |
| 40 | + // - Tooltips for geo and contour? |
| 41 | + // - [nice to have] Handle multiple dots in the same position (e.g., click to cycle)? |
| 42 | + // - Remove the red dot for testing purposes |
| 43 | + const [cx, cy] = applyFrameAnchor(this, dimensions); |
| 44 | + const {maxRadius} = this; |
| 45 | + const {marginLeft, marginTop} = dimensions; |
| 46 | + const svg = context.ownerSVGElement; |
| 47 | + let indexes = this.indexesBySvg.get(svg); |
| 48 | + if (indexes) return void indexes.push(index); |
| 49 | + this.indexesBySvg.set(svg, (indexes = [index])); |
| 50 | + const dot = select(svg) |
| 51 | + .on("pointermove", (event) => { |
| 52 | + let i, xi, yi, fxi, fyi; |
| 53 | + if (event.buttons === 0) { |
| 54 | + const [xp, yp] = pointer(event); |
| 55 | + let ri = maxRadius * maxRadius; |
| 56 | + for (const index of indexes) { |
| 57 | + const fxj = index.fx; |
| 58 | + const fyj = index.fy; |
| 59 | + const oxj = fx ? fx(fxj) - marginLeft : 0; |
| 60 | + const oyj = fy ? fy(fyj) - marginTop : 0; |
| 61 | + for (const j of index) { |
| 62 | + const xj = (X ? X[j] : cx) + oxj; |
| 63 | + const yj = (Y ? Y[j] : cy) + oyj; |
| 64 | + const dx = xj - xp; |
| 65 | + const dy = yj - yp; |
| 66 | + const rj = dx * dx + dy * dy; |
| 67 | + if (rj <= ri) (i = j), (ri = rj), (xi = xj), (yi = yj), (fxi = fxj), (fyi = fyj); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + if (i === undefined) { |
| 72 | + dot.attr("display", "none"); |
| 73 | + } else { |
| 74 | + dot.attr("display", "inline"); |
| 75 | + dot.attr("transform", `translate(${xi},${yi})`); |
| 76 | + const text = []; |
| 77 | + if (x) text.push(`${x.label ?? "x"} = ${channels.x.value[i]}`); |
| 78 | + if (y) text.push(`${y.label ?? "y"} = ${channels.y.value[i]}`); |
| 79 | + if (fx) text.push(`${fx.label ?? "fx"} = ${fxi}`); |
| 80 | + if (fy) text.push(`${fy.label ?? "fy"} = ${fyi}`); |
| 81 | + title.text(text.join("\n")); |
| 82 | + } |
| 83 | + }) |
| 84 | + .on("pointerdown pointerleave", () => dot.attr("display", "none")) |
| 85 | + .append("g") |
| 86 | + .attr("display", "none") |
| 87 | + .attr("pointer-events", "all") |
| 88 | + .attr("fill", "none") |
| 89 | + .call((g) => g.append("circle").attr("r", maxRadius).attr("fill", "none")) |
| 90 | + .call((g) => g.append("circle").attr("r", 4.5).attr("stroke", "red").attr("stroke-width", 1.5)); |
| 91 | + const title = dot.append("title"); |
| 92 | + return null; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +export function tooltip(data, {x, y, ...options} = {}) { |
| 97 | + if (options.frameAnchor === undefined) [x, y] = maybeTuple(x, y); |
| 98 | + return new Tooltip(data, {...options, x, y}); |
| 99 | +} |
0 commit comments