2016-04-13 23 views
0

d3とtopojsonに感謝してマップを描こうとしています。それから私は、このコードを1で、各国1を描く:d3とtopojsonで地図を描く

d3.json("datamaps-0.5.0/src/js/data/world.topo.json", function(error, map) { 
    console.log(map); 
    for (i=0; i<map.objects.world.geometries.length; i++) 
    { 
    svg.append("path") 
      .attr("class", "state") 
     .datum(topojson.feature(map, map.objects.world.geometries[i])) 
     .attr("d", path); 
    } 
}); 

コードが適切に機能しますが、私は、このようなマップを描画するためのループよりエレガントな方法を探しています...

答えて

1

ワンそれを行うための方法は、まず、あなたのデータ配列を計算し、あなたのコードとまったく同じである必要がありd3

var features= map.objects.world.geometries 
        .map(//.map: create a new array by applying the function below to each element of the orignal array 
         function(g) { //take the geometry 
          return topojson.feature(map, g) //and return the corresponding feature. 
         } 
        ); 
svg.selectAll(".state") 
    .data(features) 
    .enter() 
    .append("path") 
    .attr("class", "state") 
    .attr("d", path); 

これでパスにマップすることです。

関連する問題