【发布时间】:2017-02-01 16:07:18
【问题描述】:
我在这里有与 d3.js 一起使用的 topojson 数据: http://mazolosite.orionhub.org:8000/ZAF_adm3.json
我还创建了这个小提琴来演示我的问题,但是它似乎没有加载我的 topojson 数据: https://jsfiddle.net/gh583j5a/
这是我的代码,可以在本地解决缩放和正确居中问题。示例中的美国 topojson 似乎使用默认投影渲染得很好。
由于缺乏映射 API 的背景,我发现自己在缩放和投影方面遇到了困难。
var width = 960,
height = 500;
var path = d3.geo.path();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
debugger;
//var url = "https://gist.githubusercontent.com/mbostock/4090846/raw/d534aba169207548a8a3d670c9c2cc719ff05c47/us.json"
var url = "http://mazolosite.orionhub.org:8000/ZAF_adm3.json";
d3.json(url, function(error, topology) {
if (error) throw error;
//var featureCollection = topojson.feature(topology, topology.objects.counties);
var featureCollection = topojson.feature(topology, topology.objects.collection);
var bounds = d3.geo.bounds(featureCollection);
var centerX = d3.sum(bounds, function(d) {
return d[0];
}) / 2,
centerY = d3.sum(bounds, function(d) {
return d[1];
}) / 2;
var projection = d3.geo.mercator()
.scale(300)
.center([0,90]);//centerX, centerY
path.projection(projection);
svg.selectAll("path")
.data(featureCollection.features)
.enter().append("path")
.attr("d", path);
});
我的问题是理解 d3.js 的这一部分(缩放和中心):
var 投影 = d3.geo.mercator() .scale(300) .center([0,90]);//centerX,centerY
请帮忙。
【问题讨论】:
标签: javascript jquery d3.js