2016-07-12 57 views
1

我有一个JSON文件,表明d3地图不是正在渲染我创建的澳大利亚地形图文件。D3地图无法渲染澳大利亚地图文件

相同的代码呈现美国地图就好了。浏览器检查器中没有错误,并且这两个地图都可以在像geojson.io这样的在线可视化网站上呈现出来。

我已经提供了JSON的链接。

<html> 
 

 
<head> 
 
    <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> 
 
    <script src="http://d3js.org/topojson.v1.min.js"></script> 
 
    <style> 
 
    path { 
 
     fill: #ccc; 
 
    } 
 
    </style> 
 
</head> 
 

 
<body> 
 
    <h1>topojson simplified Australia</h1> 
 
    <script> 
 
    var width = window.innerWidth, 
 
     height = window.innerHeight; 
 

 
    var path = d3.geo.path(); 
 

 
    var svg = d3.select("body").append("svg") 
 
     .attr("width", width) 
 
     .attr("height", height); 
 

 
    d3.json("topo-australia-simplified.json", function(error, topology) { 
 
     if (error) throw error; 
 

 
     svg.selectAll("path") 
 
     .data(topojson.feature(topology, topology.objects.australia).features) 
 
     .enter().append("path") 
 
     .attr("d", path); 
 
    }); 
 
    </script> 
 

 

 
</body> 
 

 
</html>

回答

0

的问题是,你正在使用:

var path = d3.geo.path(); 

您还没有给出预测:

var projection = d3.geo.mercator() 
    .scale(500)//scale it up as per your choice. 
    .translate([-900,0]);//translate as it was scaled up. 

var path = d3.geo.path() 
    .projection(projection); 

工作代码here

+0

谢谢!你知道为什么我可以渲染美国地图,但没有投影吗? – Dawid

+0

原因是因为当你做'd3.geo.path();'投影是空的。 (对于美国的情况)功能值是这样的,你不需要缩放,所以不需要翻译它。我也编辑了我的答案,希望它解释得更好。 – Cyril