获取d3中的所有功能属性 [英] get all features properties in d3
问题描述
如何通过单击功能在 D3 中获得所有功能属性?
例如,如何获取此地图所有省份的 AREA和 PERIMETER:哥伦比亚的杰森地图,当单击省份时?
How we can get all features properties in D3 with clicking on a feature? For example, how we can get 'AREA' and 'PERIMETER' of all provinces of this map: GeoJson map of Colombia, when clicking on a province?
下面的代码不起作用。我看到属性未定义。
The code below not work. I see 'properties' undefined.
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 );
}
推荐答案
您误选了为基准。您无法访问像这样的选择:
You're mistaking a selection for a datum. You cannot access a selection like this:
polys[x].properties.AREA
因此,由于您想要的是基准面,因此请使用每个
:
So, since what you want is the datum, use an each
:
var txt = "";
var polys= mapLayer.selectAll("path").each(function(e){
txt += e.properties.AREA + " "
});
这是更新的bl.ocks,请检查控制台: https://bl.ocks.org/GerardoFurtado/bb29d8d5b984da7fc8079c94cce9423c/e9963b96e01b2ecab5215ecf31c7d821c6b54daf
Here is the updated bl.ocks, check the console: https://bl.ocks.org/GerardoFurtado/bb29d8d5b984da7fc8079c94cce9423c/e9963b96e01b2ecab5215ecf31c7d821c6b54daf
这篇关于获取d3中的所有功能属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!