d3.js地图未显示

桑德拉·W

我一直在尝试使用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]],[[

关于我可能做错了什么的任何指导将不胜感激。

gus27

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

您帖子中的代码与http://bl.ocks.org/mbostock/4180634上的代码几行不同

例如,您使用参数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 ,它运行正常并显示了国家。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档