Skip to content

Instantly share code, notes, and snippets.

Created June 9, 2011 14:42
  • Star 33 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Simple D3 tooltip
<!DOCTYPE html>
<html >
<script type="text/javascript" src=""></script>
<div class="example_div"></div>
<script type="text/javascript">
var tooltip ="body")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.text("a simple tooltip");
var sampleSVG =".example_div")
.attr("class", "sample")
.attr("width", 300)
.attr("height", 300);".example_div svg")
.attr("stroke", "black")
.attr("fill", "aliceblue")
.attr("r", 50)
.attr("cx", 52)
.attr("cy", 52)
.on("mouseover", function(){return"visibility", "visible");})
.on("mousemove", function(){return"top", (event.pageY-10)+"px").style("left",(event.pageX+10)+"px");})
.on("mouseout", function(){return"visibility", "hidden");});
Copy link

cazzer commented Dec 3, 2013

If anyone else is getting event is not defined or similar add d3. to the events:

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

Copy link

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

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:

      .style("left","cx") + "px")     
      .style("top","cy") + "px");
       .style("left", dx + "px")     
  .style("top", dy + "px");

Can i have good solution for this?

Copy link

brilliant and simple, thanks!

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