Voronoi Map d3

46 views
Skip to first unread message

pedro ribeiro

unread,
Nov 26, 2016, 12:37:51 PM11/26/16
to d3-js
hey, 
I have a usa map and behind i have a voronoi map to divide the usa in areas. But i don't want the voronoi to get out of the limits of the map. 


this is the code that i currently have. Ty for the help

var projectedPoints = [];
            for (var i = 0; i < data.length; i++) {
                projectedPoints.push(projection(data[i].location));
            }

            svg.selectAll("path")
                .data(voronoi.polygons(projectedPoints))
                .enter()
                .append("path")
                .attr("class", "teams-cells")
                .attr("id", function (d, i) { return data[i].abbreviation; })
                .attr("d", function (d) { return "M" + d.join("L") + "Z"; })
                .attr("fill", function (d, i) { return data[i].color; })
                .on('mouseover', function (d, i) {
                    tooltip.transition()
                        .duration(200)
                        .style("opacity", .9);
                    tooltip.html(data[i].team + "<br/>")
                        .style("left", (d3.event.pageX) + "px")
                        .style("top", (d3.event.pageY - 28) + "px");
                })
                .on('mouseout', function () {
                    tooltip.transition()
                        .duration(500)
                        .style("opacity", 0);
                })
                .on("click",function (d,i) {
                    verify(data[i]);
                });

            svg.append("path")
                .datum(topojson.feature(topology, topology.objects.land))
                .attr("d", path);
Screen Shot 2016-11-26 at 16.36.20.png

Philipp Litzenberger

unread,
Nov 27, 2016, 6:05:52 AM11/27/16
to d3...@googlegroups.com
Hi,


Best Philipp

--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<Screen Shot 2016-11-26 at 16.36.20.png>

Alex Macy

unread,
Nov 27, 2016, 12:49:41 PM11/27/16
to d3-js
Clipping worked well for me here: United States of Voronoi

That said, the polygons extend beyond the clip-path, so you'd run into trouble if you're looking to do things like calculate area for the polygons. For that, take a look at what Jason Davies did here: Areas of Clipped Voronoi Regions

- Alex
Reply all
Reply to author
Forward
0 new messages