D3 如何找到合并多边形的质心

D3 how to find Centroid of merged ploygons

我使用 D3 绘制状态图,我可以使用 path.Centroid() 函数绘制每个状态的质心,但是一旦我合并状态(多边形)使其成为大区域 path.Centroid () 不起作用。

如何找到合并的 Ploygons 的质心?

参考:here is the link to merged states example

注意:我有所有州质心的 lat/long 列表。

要在 'Merging States' 示例中找到质心:

 var mergeTopo = topojson.merge(us, us.objects.states.geometries.filter(function(d) { return selected.has(d.id); }));
 var mergeCentroid = [path.centroid(mergeTopo)];

 svg.selectAll(".mergedcentroid").data(mergeCentroid)
      .enter().append("circle") 
      .attr("class", "mergedcentroid")
      .attr("fill", "black")
      .attr("stroke", "purple")
      .attr("stroke-width", 5)
      .attr("r", 15)
      .attr("cx", function (d){ return d[0]; })
      .attr("cy", function (d){ return d[1]; });

在创建圆之前将质心坐标包裹在一个数组中。