d3.js地图未显示

时间:2016-11-11 14:54:55

标签: javascript json d3.js

我一直在尝试使用d3.js创建一个基本地图,其中包含我下载的世界shapefile(ne_50m_admin_0_countries)。我已经尝试了几个小时的各种不同的代码来尝试让它显示,但我总是得到一个空白的HTML页面。我是d3.js的新手,非常业余。以下是我一直试图开始工作的代码,它来自http://bl.ocks.org/mbostock/4180634

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
  background: #fcfcfa;
}

    .stroke {
      fill: none;
  stroke: #000;
  stroke-width: 3px;
}

.fill {
  fill: #fff;
}

.graticule {
  fill: none;
  stroke: #777;
  stroke-width: .5px;
  stroke-opacity: .5;
}

.land {
  fill: #222;
}

.boundary {
  fill: none;
  stroke: #fff;
  stroke-width: .5px;
}

</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/d3.geo.projection.v0.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>

var width = 960,
height = 580;

var color = d3.scale.category10();

var projection = d3.geo.kavrayskiy7()
    .scale(170)
    .translate([width / 2, height / 2])
    .precision(.1);

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

var graticule = d3.geo.graticule();

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

svg.append("defs").append("path")
    .datum({type: "Sphere"})
    .attr("id", "sphere")
    .attr("d", path);

svg.append("use")
    .attr("class", "stroke")
    .attr("xlink:href", "#sphere");

svg.append("use")
    .attr("class", "fill")
    .attr("xlink:href", "#sphere");

svg.append("path")
    .datum(graticule)
    .attr("class", "graticule")
    .attr("d", path);

d3.json("world-110.json", function(error, name) {
  if (error) throw error;

  var countries = topojson.feature(name, world.objects.countries).features,
      neighbors = topojson.neighbors(world.objects.countries.geometries);

  svg.selectAll(".country")
      .data(name)
    .enter().insert("path", ".graticule")
      .attr("class", "country")
      .attr("d", path)
      .style("fill", function(d, i) { return color(d.color = d3.max(neighbors[i], function(n) { return countries[n].color; }) + 1 | 0); });

  svg.insert("path", ".graticule")
      .datum(topojson.mesh(name, world.objects.countries, function(a, b) { return a !== b; }))
      .attr("class", "boundary")
      .attr("d", path);
});

d3.select(self.frameElement).style("height", height + "px");

</script> 
</body>
</html>

似乎我的JSON不能用于任何一个,但我通过QGIS正确保存了它。以下是摘录:

{"type":"Topology","transform":{"scale":[0.03600360036003601,0.017366249624962495],"translate":[-180,-90]},"objects":{"land":{"type":"MultiPolygon","arcs":[[[0]],[[1]],[[2]],[[3]],[[4]],[[5]],[[6]],[[

非常感谢任何有关我可能做错的指导。

1 个答案:

答案 0 :(得分:0)

尝试从原始网站再次复制代码。

您帖子中的代码与http://bl.ocks.org/mbostock/4180634

上的代码有几行不同

E.g。您使用的是参数name而不是world,但仍在尝试访问必须未定义的world变量:

// YOUR CODE:
d3.json("world-110.json", function(error, name) {
    if (error) throw error;

    var countries = topojson.feature(name, world.objects.countries).features, // world is UNDEFINED here
        neighbors = topojson.neighbors(world.objects.countries.geometries);
-----------------------------------------------------
// ORIGINAL:
d3.json("./world-50m.json", function(error, world) {
    if (error) throw error;

    var countries = topojson.feature(world, world.objects.countries).features,
        neighbors = topojson.neighbors(world.objects.countries.geometries);

我刚刚从网站和http://bl.ocks.org/mbostock/raw/4090846/world-50m.json的相应json中复制了代码,它运行正常并向各个国家展示。