Skip to content

Instantly share code, notes, and snippets.

@hoschi
Created August 10, 2012 09:50
Show Gist options
  • Save hoschi/3313065 to your computer and use it in GitHub Desktop.
Save hoschi/3313065 to your computer and use it in GitHub Desktop.
d3 voronoi sample
<!DOCTYPE html>
<html>
<body>
<div id='chart'>
</div>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.9.6"></script>
<script type="text/javascript">
var width = 960,
height = 500;
var vertices = [
[100, 100],
[101, 200],
[99, 300]
]
/*
doesn't work -> coincident vertices!!!!
var vertices = [
[100, 100],
[100, 200],
[100, 300]
]
*/
var svg = d3.select("#chart")
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "PiYG");
svg.selectAll("path")
.data(d3.geom.voronoi(vertices))
.enter().append("path")
.attr("class", function(d, i) { return i ? "q" + (i % 9) + "-9" : null; })
.attr("d", function(d) { return "M" + d.join("L") + "Z"; });
svg.selectAll("circle")
.data(vertices.slice(1))
.enter().append("circle")
.attr("transform", function(d) { return "translate(" + d + ")"; })
.attr("r", 2);
function update() {
vertices[0] = d3.mouse(this);
svg.selectAll("path")
.data(d3.geom.voronoi(vertices)
.map(function(d) { return "M" + d.join("L") + "Z"; }))
.filter(function(d) { return this.getAttribute("d") != d; })
.attr("d", function(d) { return d; });
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment