Created
June 28, 2019 18:50
-
-
Save sechilds/cfa4307594eb1c0c7106381a52955994 to your computer and use it in GitHub Desktop.
Tooltips in D3.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script> | |
</head> | |
<style> /* set the CSS */ | |
body { font: 12px Arial;} | |
path { | |
stroke: steelblue; | |
stroke-width: 2; | |
fill: none; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: grey; | |
stroke-width: 1; | |
shape-rendering: crispEdges; | |
} | |
div.tooltip { | |
position: absolute; | |
text-align: center; | |
width: 60px; | |
height: 28px; | |
padding: 2px; | |
font: 12px sans-serif; | |
background: lightsteelblue; | |
border: 0px; | |
border-radius: 8px; | |
pointer-events: none; | |
} | |
</style> | |
<body> | |
<div id="viz"></div> | |
<script type="text/javascript"> | |
var sampleSVG = d3.select("#viz") | |
.append("svg") | |
.attr("width", 300) | |
.attr("height", 100); | |
// Define the div for the tooltip | |
var div = d3.select("body").append("div") | |
.attr("class", "tooltip") | |
.style("opacity", 0); | |
sampleSVG.append("circle") | |
.style("stroke", "gray") | |
.style("fill", "white") | |
.attr("r", 40) | |
.attr("cx", 50) | |
.attr("cy", 50) | |
.on("mouseover", function() { | |
d3.select(this).style("fill", "blue"); | |
div.transition() | |
.duration(200) | |
.style("opacity", .9); | |
div .html("Blue!") | |
.style("left", (d3.event.pageX) + "px") | |
.style("top", (d3.event.pageY - 28) + "px"); | |
}) | |
.on("mouseout", function() { | |
d3.select(this).style("fill", "white"); | |
div.transition() | |
.duration(500) | |
.style("opacity", 0)}); | |
sampleSVG.append("circle") | |
.style("stroke", "gray") | |
.style("fill", "white") | |
.attr("r", 40) | |
.attr("cx", 150) | |
.attr("cy", 50) | |
.on("mouseover", function() { | |
d3.select(this).style("fill", "green"); | |
div.transition() | |
.duration(200) | |
.style("opacity", .9); | |
div .html("Green!") | |
.style("left", (d3.event.pageX) + "px") | |
.style("top", (d3.event.pageY - 28) + "px"); | |
}) | |
.on("mouseout", function() { | |
d3.select(this).style("fill", "white"); | |
div.transition() | |
.duration(500) | |
.style("opacity", 0)}); | |
sampleSVG.append("circle") | |
.style("stroke", "gray") | |
.style("fill", "white") | |
.attr("r", 40) | |
.attr("cx", 250) | |
.attr("cy", 50) | |
.on("mouseover", function() { | |
d3.select(this).style("fill", "red"); | |
div.transition() | |
.duration(200) | |
.style("opacity", .9); | |
div .html("Red!") | |
.style("left", (d3.event.pageX) + "px") | |
.style("top", (d3.event.pageY - 28) + "px"); | |
}) | |
.on("mouseout", function() { | |
d3.select(this).style("fill", "white"); | |
div.transition() | |
.duration(500) | |
.style("opacity", 0)}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment