2016-04-13 35 views
0

Haritaya d3 ve topojson'a teşekkür ederim. Sonra bu kodu ile tek her ülkeyi tek çizmek: ... d3 ve topojson ile harita çiz

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); 
    } 
}); 

kod iyi çalışıyor olsa

, ben böyle harita çizmek için bir döngü daha zarif yolu arıyorum

cevap

1

Bir bunu yapmanın yolu ilk önce, veri dizisi hesaplamak kodunuzu tam olarak denk olmalıdır 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); 

Bu yollarda eşlemek etmektir.