【发布时间】:2017-03-22 13:37:37
【问题描述】:
我正在尝试使用D3.js 和TopoJSON 渲染瑞士地图。底层 JSON 可以在 here 找到。首先,我尝试关注this tutorial,在无法渲染任何看起来像地图的远程内容后,我发现this question on here 带有指向工作示例的链接。从我获取此代码的地方,我目前正在使用:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<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 Switzerland</h1>
<script>
var width = window.innerWidth,
height = window.innerHeight;
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
var projection = d3.geo.mercator()
.scale(500)
.translate([-900, 0]);
var path = d3.geo.path()
.projection(projection);
d3.json("ch-cantons.json", function(error, topology) {
if (error) throw error;
svg.selectAll("path")
.data(topojson.feature(topology, topology.objects.cantons).features).enter().append("path").attr("d", path);
});
</script>
</body>
</html>
由于JSON 看起来不错(在复制/粘贴后正确呈现它的一些在线平台上进行了检查)并且只有很少的代码可能出错,我认为错误出在投影参数中。稍微折腾一下就不行了。因此,我们将不胜感激任何帮助!
【问题讨论】:
标签: javascript json d3.js topojson