将d3.nest()与geojson文件一起使用

时间:2014-09-23 16:56:30

标签: d3.js

d3.nest()如何与geojson文件一起使用?

我的geojson数据格式如下:

"features": [
{ "type": "Feature", "properties": { "neighborhood": "Allerton", "boroughCode": "2", "borough": "Bronx", "@id": "http:\/\/nyc.pediacities.com\/Resource\/Neighborhood\/Allerton" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -73.848597000000183, 40.871670000000115 ], [ -73.845822536836778, 40.870239076236174 ], [ -73.854559184633743, 40.859953835764252 ], [ -73.854665433068263, 40.859585694988056 ], [ -73.856388703358959, 40.857593635304482 ], [ -73.868881809153407, 40.857223150158326 ], [ -73.868317552728243, 40.857862062258313 ], [ -73.869553714672321, 40.857784095600181 ], [ -73.871024857620654, 40.857309948816905 ], [ -73.870480549987164, 40.865413584098484 ], [ -73.87055489856489, 40.869702798589863 ], [ -73.86721594442561, 40.869689663636713 ], [ -73.85745, 40.869533000000182 ], [ -73.855550000000108, 40.871813000000145 ], [ -73.853597967576576, 40.873288368674203 ], [ -73.848597000000183, 40.871670000000115 ] ] ] } }

但我的巢命令:

var nested_data = d3.nest()
    .key(function(d, i) { console.log(d); return d.features.properties.neighborhood; })
    .entries(map);

返回一个空数组。

我想嵌套我的数据以更轻松地过滤它。这是建议吗?

1 个答案:

答案 0 :(得分:2)

假设您的geojson如下所示

var map = {
  type: "FeatureCollection",
  "features": [
  { 
    "type": "Feature",
    "properties": {
      "neighborhood": "Allerton",
      "boroughCode": "2", 
      "borough": "Bronx", 
      "@id": "http:\/\/nyc.pediacities.com\/Resource\/Neighborhood\/Allerton"
    },
    "geometry": { /* various coordinates, etc */ }
  ]
}

所以,你想要做的是:

d3.nest()
  .key(function(d, i) {
    return d.properties.neighborhood;
  })
  .entries(map.features);

您希望传递map.features,因为那是您的数组。