After updating the answer, here is the code snippet for handling mouseout event:
https://plnkr.co/edit/9Ej1MYpGqxBdeWO2FUNO?p=preview
.on("mouseover", function(d) {
// Highlight the selected circle
d3.select(this)
.transition()
.duration(200)
.style("opacity", 0.9);
.on('mouseout', function(d) {
// Hide the circle
d3.select(this)
.transition()
.duration(100)
.style("opacity", 0);
// Hide the tooltip
d3.selectAll(".tooltip")
.transition()
.duration(100)
.style("opacity", 0);
For proper usage of mouseout
, adjust the tooltip position slightly higher and move the entire svg
down a bit:
div.html(
'<a href= "http://google.com">' +
formatTime(d.date) +
"</a>" +
"<br/>" + d.close)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 42) + "px");
var margin = {top: 50, right: 20, bottom: 30, left: 50},
Since mouseout
can be abrupt, consider increasing the circle radius for a smoother user experience:
svg.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("r", 8)
Alternatively, you may find it better to work without mouseout
for a more intuitive interaction:
View the old working example here: https://plnkr.co/edit/IitMgKW0jDYlWifokcZB?p=preview
To implement this, add the following code within .on("mouseover", function(d)
:
.on("mouseover", function(d) {
// Hide other circles
d3.selectAll('circle')
.style("opacity", 0);
// Highlight the selected circle
d3.select(this)
.transition()
.duration(200)
.style("opacity", 0.9);
It's important to note that using mouseout
in this scenario won't be effective due to overlapping circles and tooltip.