I’m using d3.js v.6. I have HTML div tooltips:
JavaScript
x
2
1
<div id="tooltip"></div>
2
appear on the hovering event according this code:
JavaScript
1
7
1
g_points.on("mouseover", function (d, i) {
2
d3.select(this).style("fill", "black");
3
d3.select("#place").text("Place: " + i.place);
4
d3.select("#tooltip")
5
.style("left", d3.select(this).attr("cx") + "px")
6
.style("top", d3.select(this).attr("cy") + "px")})
7
I need to slightly shift positioning of #tooltip
. I’ve already tested these options which didn’t work:
JavaScript
1
3
1
// .style("left", d3.event.pageX + 20 + "px")
2
// .style("left", d3.event.pageY - 80 + "px")
3
and
JavaScript
1
3
1
// .style("left", d3.mouse(this)[0] + 70 + "px")
2
// .style("top", d3.mouse(this)[1] + "px")
3
Advertisement
Answer
Try this code (should work with V6):
JavaScript
1
8
1
g_points.on("mouseover", function (e, d, i) {
2
d3.select(this).style("fill", "black");
3
d3.select("#place").text("Place: " + i.place);
4
d3.select("#tooltip")
5
.style("left", `${e.layerX}px`)
6
.style("top", `${e.layerX}px`);
7
});
8