【发布时间】:2015-05-26 02:45:44
【问题描述】:
我正在尝试在 ubuntu 的 Meteor 中重现 this d3 world tour,但似乎 d3 没有正确加载 json 或其他东西。我尝试了几个不同位置的 json 和 tsv 文件:https://github.com/KoGor/Maps.GeoInfo、https://github.com/mapmeld/flightmap,并尝试在文件上执行 fromdos。一切似乎都在工作,除了当我尝试运行它时它给出错误'SyntaxError:Unexpected Token
我的js文件:
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
if (Meteor.isClient) {
Template.map.rendered = function() {
var width = 960,
height = 500;
var projection = d3.geo.orthographic()
.scale(248)
.clipAngle(90);
var canvas = d3.select("body").append("canvas")
.attr("width", width)
.attr("height", height);
var c = canvas.node().getContext("2d");
var path = d3.geo.path()
.projection(projection)
.context(c);
var title = d3.select("h1");
queue()
.defer(d3.json, "/geo/world-110m.json")
.defer(d3.tsv, "/geo/world-country-names.tsv")
.await(ready);
function ready(error, world, names) {
if (error) return console.warn(error);
var globe = {type: "Sphere"},
land = topojson.feature(world, world.objects.land),
countries = topojson.feature(world, world.objects.countries).features,
borders = topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }),
i = -1,
n = countries.length;
countries = countries.filter(function(d) {
return names.some(function(n) {
if (d.id == n.id) return d.name = n.name;
});
}).sort(function(a, b) {
return a.name.localeCompare(b.name);
});
(function transition() {
d3.transition()
.duration(1250)
.each("start", function() {
title.text(countries[i = (i + 1) % n].name);
})
.tween("rotate", function() {
var p = d3.geo.centroid(countries[i]),
r = d3.interpolate(projection.rotate(), [-p[0], -p[1]]);
return function(t) {
projection.rotate(r(t));
c.clearRect(0, 0, width, height);
c.fillStyle = "#bbb", c.beginPath(), path(land), c.fill();
c.fillStyle = "#f00", c.beginPath(), path(countries[i]), c.fill();
c.strokeStyle = "#fff", c.lineWidth = .5, c.beginPath(), path(borders), c.stroke();
c.strokeStyle = "#000", c.lineWidth = 2, c.beginPath(), path(globe), c.stroke();
};
})
.transition()
.each("end", transition);
})();
}
}
}
如何让 json 文件正确加载,或让示例在流星中工作?
【问题讨论】:
标签: javascript json d3.js meteor