function hover(g, svg) {
const dot = svg.append("g").attr("display", "none");
dot
.append("circle")
.attr("r", 6)
.attr("stroke-width", 3)
.attr("fill", "white");
const tooltip = new Tooltip();
svg
.style("-webkit-tap-highlight-color", "transparent")
.style("cursor", "pointer")
.on("touchstart", event => event.preventDefault())
.on("pointerenter pointermove", enter)
.on("pointerout", exit);
svg.append(() => tooltip.node);
function enter(event) {
event.preventDefault();
const pointer = d3.pointer(event, this);
const xm = x.invert(pointer[0]);
const ym = y.invert(pointer[1]);
const i1 = d3.bisectLeft(dates, xm);
const i0 = i1 - 1;
const i = xm - dates[i0] > dates[i1] - xm ? i1 : i0;
const s = d3.least(series, d => Math.abs(d.values[i] - ym));
let xp = x(dates[i]);
let yp = y(s.values[i]);
g.attr("stroke", d =>
d === s ? colour(d.key) : traseColours.get("lightgrey")
)
.filter(d => d === s)
.raise();
dot
.attr("display", null)
.attr("stroke", colour(s.key))
.attr("transform", `translate(${xp},${yp})`);
tooltip.show(
s.key,
tooltipKeyValue(
formatTooltipKey(dates[i]),
formatTooltipValue(s.values[i])
)
);
xp += 10;
if (xp + tooltip.width > width) xp -= tooltip.width + 20;
if (yp + tooltip.height > height - margin.bottom)
yp = height - margin.bottom - tooltip.height - 10;
tooltip.position(xp, yp);
}
function exit() {
g.style("mix-blend-mode", "multiply").attr("stroke", ({ key }) =>
colour(key)
);
dot.attr("display", "none");
tooltip.hide();
}
}