我正在尝试重现 this d3 world tour在 ubuntu 中的 Meteor 中,但似乎 d3 没有正确加载 json 或者其他东西。我已经尝试了来自几个不同位置的 json 和 tsv 文件:https://github.com/KoGor/Maps.GeoInfo , https://github.com/mapmeld/flightmap ,并尝试对文件执行 fromdos
。一切似乎都正常,除了当我尝试运行它时出现错误“SyntaxError:意外的 token <”。 tsv 和 json 文件位于/public/geo
我的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 文件,或者如何让示例在 Meteor 中工作?
最佳答案
请注意,您必须将 .json(和 .tsv)放入 Meteor 项目的 public 文件夹中,因为它们是静态 Assets 。
关于javascript - 为什么这个 json 文件在 Meteor [ubuntu] 中无法加载 d3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30429837/