Skip to content

Instantly share code, notes, and snippets.

@damianak1
Last active November 19, 2016 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damianak1/c0baa4ac91f8e2c338857877b228710c to your computer and use it in GitHub Desktop.
Save damianak1/c0baa4ac91f8e2c338857877b228710c to your computer and use it in GitHub Desktop.
Tooltip on Ellipse
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<style>
body { font: 10px sans-serif; }
.d3-tip {
background: rgba(0, 0, 0, 0.8);
border-radius: 2px;
color: #fff;
font-weight: bold;
line-height: 1;
padding: 12px;
}
</style>
</head>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script>
var w = window.innerWidth,
h = window.innerHeight,
margin = { top: 40, right: 20, bottom: 20, left: 40 };
var svg = d3.select("body").append("svg").attr({
width: w,
height: h
});
var dataset = [
{ toolTip: "one", d: "M 50 200 a 100 50 0 1 1 250 50" },
{ toolTip: "two", d: "M 400 100 a 100 50 30 1 1 250 50" },
{ toolTip: "three", d: "M 400 300 a 100 50 45 1 1 250 50" },
{ toolTip: "four", d: "M 750 200 a 100 50 135 1 1 250 50" }
];
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Elipse:</strong> <span style='color:red'>" + d.toolTip + "</span>";
});
svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.attr("stroke-width", 3)
.attr("stroke", "black")
.attr("fill", "none")
.append("path")
.attr("d", function(d) { return d.d })
.on('mouseover.tooltip', tip.show)
.on('mouseover.hover', highLight)
.on('mouseout.hover', unHighLight)
.on('mouseout.tooltip', tip.hide);
svg.call(tip);
function highLight(){
var foo = d3.select(this);
foo.attr("stroke","red");
}
function unHighLight(){
var foo = d3.select(this);
foo.attr("stroke","black");
}
</script>
</body>
</html>
@damianak1
Copy link
Author

damianak1 commented Nov 19, 2016

This was created in response to stackoverflow question about highlighting paths combined with showing tool tips.

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