【问题标题】:D3.js zooming on specific path after loading finished加载完成后D3.js在特定路径上缩放
【发布时间】:2015-01-21 18:02:16
【问题描述】:

我想用 D3.js 在路径上触发一个事件。这是一个工作示例:http://jsfiddle.net/kwoxer/kpL1uyy2/

那么我怎么能说一个特定的路径是由 zoomIntoArea 函数触发的。我的意思是最后我有一些路径,我想在启动时加载一个特定的而不单击它。我已经试过了:

zoomIntoArea(d3.select("lines"))

还有其他一些,但可以肯定的是,这并没有给我正确的元素。

【问题讨论】:

标签: javascript function d3.js startup topojson


【解决方案1】:

您不需要缩放行为来显式缩放到路径,例如:

var bounds = path.bounds(d),
        dx = bounds[1][0] - bounds[0][0],
        dy = bounds[1][1] - bounds[0][1],
        x = (bounds[0][0] + bounds[1][0]) / 2,
        y = (bounds[0][1] + bounds[1][1]) / 2,
        scale = .9 / Math.max(dx / width, dy / height),
        translate = [
            width / 2 - scale * x, 
            height / 2 - scale * y];

svg.transition()
    .duration(750)
    .attr("transform", "translate(" + 
        translate + ")scale(" + 
        scale + ")");

【讨论】:

  • 感谢您的回答,但这不是我想要的。好吧,我需要 d3.json 中的一些东西……说在第一条路径的中心。这需要在最后触发。或者您可以用您的代码更改 jsfiddle 示例并将其展示给我。也许我只是不明白你的方式?谢谢。
  • 好的,现在我知道把你的代码放在哪里了。是的,那确实可行。但是我怎样才能使用该功能 zoomIntoArea 。我的意思是为什么再次使用相同的代码。谢谢。
  • 现在好了 zoomIntoArea(featureCollection);正在工作中。但我以前确实是这样测试过的。这里发生了奇怪的事情。好的问题解决了。谢谢。
猜你喜欢
  • 2021-09-02
  • 2013-04-20
  • 2013-09-03
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 1970-01-01
相关资源
最近更新 更多