如何运行Mike Bostock的D3示例?

agc*_*nti 5 javascript json d3.js

我一直试图运行Mike Bostock的See-Through Globe示例,但如果您尝试在本地重现它,则对其json文件的引用是不正确的.

问题来自这行代码:

d3.json("/mbostock/raw/4090846/world-110m.json", function(error, topo) {
      var land = topojson.feature(topo, topo.objects.land),
          grid = graticule();
});
Run Code Online (Sandbox Code Playgroud)

我遵循了这个例子: d3:我们在topojson格式文件中的状态太大了

并尝试将URL更改为这些变体,以便更好地为外部用户引用这些文件;

"https://raw.github.com/mbostock/topojson/master/examples/world-110m.json"
"https://render.github.com/mbostock/topojson/master/examples/world-110m.json"
"http://bl.ocks.org/mbostock/raw/4090846/world-110m.json"
Run Code Online (Sandbox Code Playgroud)

但我永远不允许访问.有关如何正确引用json文件的任何想法?

如果他的其他例子也尝试了一些,并且每次都遇到同样的问题.还有其他人在复制这些例子方面取得了成功吗

问题转载于一个小提琴:http://jsfiddle.net/agconti/wBfLd/

链接到图库示例

链接到github要点

Ste*_*mas 6

由于原始策略相同,您无法访问远程json文件.并且您将无法使用file:protocol检索JSON对象.除非您想通过直接嵌入JSON来对代码进行手术,否则您将不得不运行本地服务器.

运行本地Web服务器的简便方法是执行:

python -m SimpleHTTPServer 8888 &
Run Code Online (Sandbox Code Playgroud)

从您网站的"根"目录,然后通过它访问它 http://localhost:8888


t.8*_*888 5

您可以在此处直接访问数据:http://bl.ocks.org/mbostock/raw/4090846/world-110m.json

要使其工作,您需要在脚本中创建一个新变量并直接将json分配给它.

您提供的小提琴代码:

var topo = // Paste data from provided link here. It will be one big object literal.

var width = 960,
    height = 960,
    speed = -1e-2,
    start = Date.now();

var sphere = {type: "Sphere"};

var projection = d3.geo.orthographic()
    .scale(width / 2.1)
    .translate([width / 2, height / 2])
    .precision(.5);

var graticule = d3.geo.graticule();

var canvas = d3.select("body").append("canvas")
    .attr("width", width)
    .attr("height", height);

var context = canvas.node().getContext("2d");

var path = d3.geo.path()
    .projection(projection)
    .context(context);

// d3.json("https://render.github.com/mbostock/topojson/master/examples/world-110m.json", function(error, topo) {
  var land = topojson.feature(topo, topo.objects.land), // topo var is now provided by pasted-in data instead of fetched json.
      grid = graticule();

  d3.timer(function() {
    context.clearRect(0, 0, width, height);

    projection.rotate([speed * (Date.now() - start), -15]).clipAngle(90);

    context.beginPath();
    path(sphere);
    context.lineWidth = 3;
    context.strokeStyle = "#000";
    context.stroke();
    context.fillStyle = "#fff";
    context.fill();

    projection.clipAngle(180);

    context.beginPath();
    path(land);
    context.fillStyle = "#dadac4";
    context.fill();

    context.beginPath();
    path(grid);
    context.lineWidth = .5;
    context.strokeStyle = "rgba(119,119,119,.5)";
    context.stroke();

    projection.clipAngle(90);

    context.beginPath();
    path(land);
    context.fillStyle = "#737368";
    context.fill();
    context.lineWidth = .5;
    context.strokeStyle = "#000";
    context.stroke();
  });
// });

d3.select(self.frameElement).style("height", height + "px");
Run Code Online (Sandbox Code Playgroud)

我本来可以直接修改小提琴,但json数据文件足够大,当我尝试粘贴它时,jsfiddle chokes.

希望这可以帮助.