在我的数据中,我有与国家相关的值。我为每个国家创建了缩放圈子,现在想用cx和cy定位它们在每个国家的中心位置。查找topoJSON路径的质心并用它在D3中定位一个圆圈
我已经使用topoJSON生成了一个具有国家代码'ids'的地图,并且在我的数据(cd)中有匹配的国家/地区代码。
{"type": "Polygon",
"id": 604,
"arcs": [
[133, -473, -448, -378, -374, -413]
]
},
使用D3的path.centroid(feature),我怎么能找到每个topoJSON路径的重心?
g.selectAll("circle")
.data(cd)
.enter()
.append("circle")
.attr("class", "bubble")
.attr("cx", 50)
.attr("cy", 50)
.attr("r", function(d) {
return r(+d.Value)
})
g.selectAll("path")
.data(topojson.object(topology, topology.objects.countries)
.geometries)
.enter()
.append("path")
.attr("d", path)
这里充分Plunker
谢谢,使用翻译作品太棒了!我如何使用cx和cy定位来做到这一点? (我想用这些尺寸来定位我的div工具提示和文本标签) – user3821345
@ user3821345,查看更新的答案。 – Mark
这就是我想要的,非常感谢! – user3821345