Skip to content

Instantly share code, notes, and snippets.

@Kamalabot
Forked from biovisualize/index.html
Last active June 22, 2022 16:09
Show Gist options
  • Save Kamalabot/53648c543490273760b501f305a11d02 to your computer and use it in GitHub Desktop.
Save Kamalabot/53648c543490273760b501f305a11d02 to your computer and use it in GitHub Desktop.
Simple D3 tooltip
<!DOCTYPE html>
<html >
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.8/d3.min.js" type="text/JavaScript"></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")
.attr("class", "sample")
.attr("width", 300)
.attr("height", 300);
d3.select("svg")
.append("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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment