简体   繁体   English

使用D3放大

[英]Zooming in using D3

How would I zoom in to a certain point on an svg at a certain point and then zoom out to the whole svg. 我如何在某个点上放大到svg上的某个点,然后再缩小到整个svg。 There are only two states, I don't require mouse scroll zoom or a zoom in, zoom out button. 只有两种状态,我不需要鼠标滚动缩放或放大,缩小按钮。

I've looked up and found .zoom in d3 documentation, but am unsure of how to apply in my circumstances. 我在d3文档中查找并找到了.zoom,但是不确定如何在我的情况下应用。 I've only seen scroll zooms that are too complicated. 我只看到滚动缩放过于复杂。

Checkout how mouse zooming is typically implemented: 查看通常如何实现鼠标缩放:

var svg = d3.select("#svg")
    .call(d3.behavior.zoom().on("zoom", redraw))

function redraw() {
  console.log("translate: ", d3.event.translate, "scale:", d3.event.scale);
  svg.attr("transform",
      "translate(" + d3.event.translate + ")"
      + " scale(" + d3.event.scale + ")");
}

Scrolling will spit out something like: 滚动将吐出类似以下内容的内容:

translate:  [-66.3438, -36.359] scale: 1.1809

To avoid implementing a scroll zoom, you can directly modify the transformation attribute of your svg just like the redraw function does. 为了避免实现滚动缩放,可以像重绘功能一样直接修改svg的transform属性。 Just use your own values instead of d3.event.translate. 只需使用您自己的值而不是d3.event.translate。 For example: 例如:

svg.attr("transform",
      "translate(" + [0,0] + ")"
      + " scale(" + 2 + ")");

will zoom 2x to the center of the svg. 将缩放到svg的中心2倍。 Using 5 instead of 2 would zoom in 5x. 使用5而不是2将放大5倍。

The click to zoom map example has good demonstration of how to do this 单击缩放地图示例很好地演示了如何执行此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM