O'Reilly logo

Expert Data Visualization by Jos Dirksen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Create the geo Voronoi

To create the Voronoi, we change the callback from the previous code fragment to this:

var color = function() {return d3.interpolateYlOrRd(Math.random())};  function (data) {       var positions = data.map(function(row) {return [row.lon, row.lat]})       var voronoi = d3.voronoi().extent([[-180, -90], [180, 90 ]]);       var polygons = voronoi.polygons(positions);        svg.append("g").selectAll("path").data(polygons).enter()           .append("path")           .attr("d", polygonToPath)           .attr("fill", color) })  function polygonToPath(polygon, i) {   // strip the data attribute   var points = d3.range(0, polygon.length).map(function(i) {return polygon[i]});   points.push(points[0])    // ignore thie element, since it is rendered incorrectly  if (i === 3835) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required