Skip to content

Instantly share code, notes, and snippets.

@biovisualize
Created June 9, 2011 14:42
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save biovisualize/1016860 to your computer and use it in GitHub Desktop.
Save biovisualize/1016860 to your computer and use it in GitHub Desktop.
Simple D3 tooltip
<!DOCTYPE html>
<html >
<head>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
</head>
<body>
<div class="example_div"></div>
<script type="text/javascript">
var tooltip = d3.select("body")
.append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.text("a simple tooltip");
var sampleSVG = d3.select(".example_div")
.append("svg:svg")
.attr("class", "sample")
.attr("width", 300)
.attr("height", 300);
d3.select(".example_div svg")
.append("svg:circle")
.attr("stroke", "black")
.attr("fill", "aliceblue")
.attr("r", 50)
.attr("cx", 52)
.attr("cy", 52)
.on("mouseover", function(){return tooltip.style("visibility", "visible");})
.on("mousemove", function(){return tooltip.style("top", (event.pageY-10)+"px").style("left",(event.pageX+10)+"px");})
.on("mouseout", function(){return tooltip.style("visibility", "hidden");});
</script>
</body>
</html>
@nileshtrivedi
Copy link

svg:title does work as a tooltip in Firefox 37 and Chrome 42. That's good enough for me! :)

@srinivmw
Copy link

Can we apply anything instead of

    .style("left", (d3.event.pageX - 34) + "px")
    .style("top", (d3.event.pageY - 12) + "px"); 

because when i use this the tooltip is not displaying in its position correctly.....

Below two things also not working:
1)

      .style("left", d3.select(this).attr("cx") + "px")     
      .style("top", d3.select(this).attr("cy") + "px");
       .style("left", dx + "px")     
  .style("top", dy + "px");

Can i have good solution for this?

@billwestfall
Copy link

brilliant and simple, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment