javascript 如何知道 D3.js 中的当前缩放级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15147136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to know the current Zoom level in D3.js
提问by Alban
I have a problem since a few days. I have a graph and when I use Zoom Behaviorit works, but I need to know when I reach maximum zoom to load new given
几天后我遇到了问题。我有一个图表,当我使用Zoom Behavior 时它可以工作,但我需要知道何时达到最大缩放以加载新的给定
// Specifies the zoom scale's allowed range, [min, max]
d3.behavior.zoom().x(x).scaleExtent([1,10]).on("zoom", draw)
See my code http://jsfiddle.net/albanlopez/D4MRP/
采纳答案by Alban
rect.call(zm=d3.behavior.zoom().x(x).scaleExtent([1,10]).on("zoom", draw));
After a new test i have the answer :
经过新的测试后,我有了答案:
var currentZoom = d3.event.scale;
But only available/readable in the draw()function called by .on("zoom", draw)
但仅在.on("zoom", draw)调用的draw()函数中可用/可读
rect.call( zm = d3.behavior.zoom().x(x).scaleExtent([1,10]).on("zoom", draw));
function draw() {
// trace l'axe X
svg.select("g.x.axis").call(xAxis);
// trace l'axe Y
svg.select("g.y.axis").call(yAxis);
// trace la courbe
svg.select("path.line").attr("d", line);
console.log(zm.scale(), zm.translate()); // , zm.translate[1] = Y
console.log(d3.event.scale, d3.event.translate[0]); // , d3.event.translate[1] = Y
}
回答by Superboggly
In the draw function the current event will have the zoom level (d3.event.scale as you mentioned). Also if you keep the behaviour around like:
在 draw 函数中,当前事件将具有缩放级别(如您提到的 d3.event.scale)。此外,如果您保持以下行为:
var zm = d3.behavior.zoom().x(x).scaleExtent([1,10]).on("zoom", draw);
Then you can find the current zoom level by calling:
然后你可以通过调用找到当前的缩放级别:
zm.scale();
回答by CodeOcelot
As of D3 v4 there are two documented ways of retrieving the zoom state. To quote d3/d3-zoom README (under Zoom Transforms):
从 D3 v4 开始,有两种已记录的检索缩放状态的方法。引用 d3/d3-zoom README(在缩放变换下):
To retrieve the zoom state, use event.transform on a current zoom event within a zoom event listener (see zoom.on), or use d3.zoomTransform for a given node.
要检索缩放状态,请对缩放事件侦听器中的当前缩放事件使用 event.transform(请参阅 zoom.on),或对给定节点使用 d3.zoomTransform。
https://github.com/d3/d3-zoom/blob/master/README.md#zoom-transforms
https://github.com/d3/d3-zoom/blob/master/README.md#zoom-transforms