我们如何通过单击特征来获取 D3 中的所有特征属性?例如,当点击一个省时,我们如何获得这张地图上所有省份的"面积"和"周长":哥伦比亚的GeoJson地图?
下面的代码不起作用。我看到"属性"未定义。
function clicked(d) {
var polys= effectLayer.selectAll("path");
var x;
var txt;
for (x in polys) {
//show this:
txt += polys[x].properties.AREA + " ";
}
alert(txt );
}
您将所选内容误认为基准面。您无法访问如下选择:
polys[x].properties.AREA
因此,由于您想要的是基准面,请使用each
:
var txt = "";
var polys= mapLayer.selectAll("path").each(function(e){
txt += e.properties.AREA + " "
});
这是更新的bl.ocks,请检查控制台:https://bl.ocks.org/GerardoFurtado/bb29d8d5b984da7fc8079c94cce9423c/e9963b96e01b2ecab5215ecf31c7d821c6b54daf