addInteractionSupport = (context, xyData, tooltipTextGenerator) => {
const delaunay = d3.Delaunay.from(xyData);
const voronoi = delaunay.voronoi([0, 0, W, H]);
if (voronoiDrawOption == "true") {
context.globalAlpha = 0.5;
context.beginPath();
context.strokeStyle = 'rgba(0,0,0,0.3)';
voronoi.render(context);
context.stroke();
context.beginPath();
context.fillStyle = 'black';
delaunay.renderPoints(context, 2);
context.fill();
context.globalAlpha = 1;
}
d3.select(context.canvas)
.on("touchmove mousemove", handleMove)
.on("touchend mouseleave", hideTooltip);
function handleMove(event) {
event.preventDefault();
const mousePoint = d3.pointer(event, this);
mutable feedback = [event.pageX, event.pageY, mousePoint[0], mousePoint[1]];
if (
mousePoint[0] > margin.left &&
mousePoint[0] < width - margin.right &&
mousePoint[1] > margin.top &&
mousePoint[1] < height - margin.bottom
) {
const x = mousePoint[0] - margin.left,
y = mousePoint[1] - margin.top;
const index = delaunay.find(x, y);
const tooltipText = tooltipTextGenerator(index);
mutable feedback = tooltipText;
showTooltip(event, tooltipText);
} else {
hideTooltip();
}
}
}