Skip to content

Instantly share code, notes, and snippets.

@henryjameslau
Last active March 3, 2020 12:24
Show Gist options
  • Save henryjameslau/9b68f5a540e9bbbec9178d51e1f10cdf to your computer and use it in GitHub Desktop.
Save henryjameslau/9b68f5a540e9bbbec9178d51e1f10cdf to your computer and use it in GitHub Desktop.
Everything you need to make a tooltip with d3-delaunay
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/d3-delaunay.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
x=d3.scaleLinear().range([0,960]).domain([0,960])
y=d3.scaleLinear().range([0,500]).domain([0,500])
data=d3.range(960).map(function(d){return [x(d*Math.random()),y(d*Math.random())]})
svg.selectAll(".circle").data(data).enter().append('circle').attr('cx',function(d){return d[0]}).attr('cy',function(d){return d[1]}).attr('r',3)
var delaunay = d3.Delaunay.from(data)
svg.on('mousemove',function(d){
console.log(data[delaunay.find(x.invert(d3.mouse(this)[0]),y.invert(d3.mouse(this)[1]))])
})
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment