为什么在Meteor [ubuntu]中用d3加载这个json文件?

时间:2015-05-25 00:38:44

标签: javascript json d3.js meteor

我试图在ubuntu中重现Meteor中的this d3 world tour,但似乎d3并没有正确加载json或其他东西。我已尝试过几个不同位置的json和tsv文件:https://github.com/KoGor/Maps.GeoInfohttps://github.com/mapmeld/flightmap,并尝试对文件执行fromdos。一切似乎都在工作,除了它给出了错误' SyntaxError:Unexpected 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中工作?

1 个答案:

答案 0 :(得分:1)

请注意,您必须将.json(和.tsv)放在Meteor项目的公共文件夹中,因为它们是静态资产。