2017-03-25 14 views
1

私はマップをd3.jsの中央に配置しようとしています。私はこのコードを見つけて少し変更しました。私はそれをすることができませんでした。私はこのエラーが発生しています。どうすれば修正できますか?私はJavaScriptに新しいです。エラー:<path>属性d:予想される番号 "MNaN、NaNLNaN、NaNL ..."。 d3.jsで

これは私のコードです。

<!DOCTYPE html> 
<meta charset="utf-8"> 

<!-- Set a style for our worldshape-data --> 
<style> 
    path { 
    stroke: red; 
    stroke-width: 0.5px; 
    fill: steelblue; 
    } 
</style> 

<body> 

    <!-- implementation of the hosted D3- and TopoJson js-libraries --> 
    <script src="https://d3js.org/d3.v3.min.js"></script> 
    <script src="https://d3js.org/topojson.v0.min.js"></script> 

    <!-- map creation --> 
    <script> 
    // canvas resolution 
    var width = 1000, 
     height = 600; 

    // defines "svg" as data type and "make canvas" command 
    var svg = d3.select("body").append("svg") 
     .attr("width", width) 
     .attr("height", height); 


    // shorten the svg.append command 
    var g = svg.append("g"); 

    // load data and display the map on the canvas with country geometries 
    d3.json("turkey.json", function(error, topology) { 

     var projection = d3.geo.mercator().scale(1).translate([0, 0]).precision(0); 
     var path = d3.geo.path().projection(projection); 
     var bounds = path.bounds(topology); 


     var scale = .95/Math.max((bounds[1][0] - bounds[0][0])/width, 
     (bounds[1][1] - bounds[0][1])/height); 
     var transl = [(width - scale * (bounds[1][0] + bounds[0][0]))/2, 
     (height - scale * (bounds[1][1] + bounds[0][1]))/2 
     ]; 
     projection.scale(scale).translate(transl); 

     g.selectAll("path") 
     .data(topojson.object(topology, topology.objects.turkeytopo) 
      .geometries) 
     .enter() 
     .append("path") 
     .attr("d", path) 
    }); 



    // zoom and pan functionality 
    var zoom = d3.behavior.zoom() 
     .on("zoom", function() { 
     g.attr("transform", "translate(" + 
      d3.event.translate.join(",") + ")scale(" + d3.event.scale + ")"); 
     g.selectAll("path") 
      .attr("d", path.projection(projection)); 
     }); 

    svg.call(zoom) 
    </script> 
</body> 

</html> 

これは私のjsonの国データの一部です。

{ 
    "type": "Topology", 
    "objects": { 
    "turkeytopo": { 
     "type": "GeometryCollection", 
     "bbox": [25.66652800000014, 
     35.820178, 
     44.83384, 
     42.106301 
     ], 
     "geometries": [{ 
     "type": "MultiPolygon", 
     "arcs": [ 
      [ 
      [0] 
      ], 
      [ 
      [1] 
      ], 
      [ 
      [2] 
      ], 
      [ 
      [3] 
      ], 
      [ 
      [4] 
      ], 
      [ 
      [5] 
      ], 
      [ 
      [6] 
      ], 
      [ 
      [7] 
      ], 
      [ 
      [8] 
      ], 
      [ 
      [9] 
      ], 
      [ 
      [10] 
      ], 
      [ 
      [11] 
      ] 
     ] 
     }] 
    } 
    }, 
    "arcs": [ 
    [ 
     [1726, 
     8274 
     ], 
     [2, -33], 
     [-22, 
     0 
     ], 
     [-7, -21], 
     [-43, -47], 
     [-16, -4], 
     [-12, 
     25 
     ], 
     [-17, 
     13 
     ], 
     [-36, -25], 
     [-33, -6], 
     [-11, 
     13 
     ], 
     [-1, 
     66 
     ], 
     [-17, 
     14 
     ], 
     [-12, -52], 
     [-58, 
     81 
     ], 
     [-28, 
     24 
     ], 
     [-62, 
     16 
     ], 
     [-14, 
     19 
     ] 
    ] 
    ], 
    "transform": { 
    "scale": [0.001916922892289215, 
     0.000628675167516752 
    ], 
    "translate": [25.66652800000014, 
     35.820178 
    ] 
    } 
} 

答えて

0

path.boundsは正しく使用されていません。 APIドキュメントpath.bounds

Returns the projected planar bounding box (typically in pixels) for the specified GeoJSON object. (v4)

These components use the GeoJSON format—a standard way of representing geographic features in JavaScript. (v3)

からあなたはpath.bounds topojsonオブジェクトを渡して、代わりに、してみてくださいされています

var bounds = path.bounds(topojson.feature(topology, topology.objects.turkeytopo)); 

にBBOXプロパティによって示されるように、あなたはいつも、心が同様に座標を使用することができますあなたのトポロジー

var projection = d3.geo.mercator() 
    .scale(2000) 
    .center([34.5,38.5]) 
    .translate([width/2,height/2]); 

スケールは、SVGのサイズに依存します:投影は次のようになります。