attachVoronoiTip = (Chart.prototype.attachVoronoiTip = function ({
data,
chart,
scaleX,
scaleY,
tip,
calc: { chartWidth, chartHeight }
}) {
const voronoi = d3.Delaunay.from(
data.flat(),
(d) => scaleX(d.x),
(d) => scaleY(d.y)
).voronoi([0, 0, chartWidth, chartHeight]);
chart
._add({ tag: "g", className: "voronoi-wrapper" })
._add({ tag: "path", className: "voronoi-path", data: data.flat() })
.attr("opacity", 0.5)
.attr("stroke", "#ff1493")
.attr("fill", "none")
.style("pointer-events", "all")
.attr("d", (d, i) => {
const pathD = voronoi.renderCell(i);
return pathD;
})
.on("mouseenter", (event, d, i) => {
const c = chart
._add({
tag: "circle",
className: "mouse-enter-circle"
})
.attr("r", 3)
.attr("cx", scaleX(d.x))
.attr("cy", scaleY(d.y));
tip.show(event, d, c.node());
})
.on("mouseleave", () => {
chart.selectAll(".mouse-enter-circle").remove();
tip.hide();
});
})