d3:放大到国家而不是国家的边界​​框II

时间:2016-05-03 17:14:01

标签: d3.js topojson

我试图让d3 Zoom to Bounding Box II示例(请参阅此处:https://bl.ocks.org/mbostock/raw/9656675/)按照示例为世界地图而不是美国州地图工作。不幸的是,缩放是不稳定的。对于一些国家,它运作良好;对于其他人来说,它会缩小到太平洋。

由于我无法从远程位置加载topojson文件,因此我可以编写代码。我在上面的例子中编辑的相关功能如下:

var projection = d3.geo.mercator() // instead of albersUsa
    .scale(100)
    .translate([width / 2, height / 2]);

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

  g.selectAll("path")
    .data(topojson.feature(world, world.objects.countries).features) //traverse world objects instead of state objects
    .enter().append("path")
  .attr("d", path)
  .attr("class", "feature")
  .on("click", clicked);

  g.append("path")
      .datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; })) // append world objects not states
      .attr("class", "mesh")
      .attr("d", path);
});

任何帮助表示感谢。

注意:我刚刚意识到某些州的变焦也不稳定。例如,尝试点击VT或NH。我将在GitHub上提交一个错误。同时有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为回答我自己的问题(即使我不是那个解决它的人)也是犹太人。

迈克博斯托克说我正在运行一个稍微过时的d3版本。至少运行D3 3.5.7,然后更新边界var:

scale = Math.max(1, Math.min(8, 0.9 / Math.max(dx / width, dy / height)))

他也在他的网站上修改了这些例子。