javascript - 如何禁用 D3.js 中的缩放功能?

标签 javascript d3.js scroll zooming

我在 D3 中提供了缩放功能,但我希望将其设为可选,因此我想要一种将其关闭的方法。这是我的代码:

//Zoom command ...
var zoom = d3.behavior.zoom()
    .x(xScale)
    .y(yScale)
    .scaleExtent([1,10])
    .on("zoom", zoomTargets);

var SVGbody = SVG.append("g")
    .attr("clip-path", "url(#clip)")
    .call(zoom);

/

/The function handleling the zoom. Nothing is zoomed automatically, every elemnt must me defined here.
function zoomTargets() {
    if($("#enableZoom").is(':checked')){    
        var translate = zoom.translate(),
        scale = zoom.scale();

        tx = Math.min(0, Math.max(width * (1 - scale), translate[0]));
        ty = Math.min(0, Math.max(height * (1 - scale), translate[1]));

        //This line applies the tx and ty which prevents the graphs from moving out of the limits. This means it can't be moved until zoomed in first.
        zoom.translate([tx, ty]);

        SVG.select(".x.axis").call(xAxis)
            .selectAll("text")
                .style("text-anchor", "end")
                .style("font-size", AXIS_FONTSIZE)
                .attr("transform", function(d) {
                return "rotate(-30)" 
                });

        SVG.select(".y.axis").call(yAxis)
            .selectAll("text")
                .style("font-size", AXIS_FONTSIZE);

        SVG.selectAll("circle")
        .attr("cx", function(d, i){ return xScale(graphDataX[i]);})
        .attr("cy",function(d){return yScale(d);});

        SVGMedian.selectAll("ellipse")
            .attr("cx", function(d, i){ return xScale((i*100)+100);})
            .attr("cy", function(d){ return yScale(d-0.01);});
    }
}

正如您所看到的,我尝试使用 if 语句来防止在未选中复选框时缩放功能发挥作用。当鼠标位于 SVG 框架内时,这可以防止用户在页面上滚动。

我想要正确的方法来做到这一点。预先非常感谢!

最佳答案

这是禁用 d3 缩放行为的一种方法

SVGbody.select("g").call(d3.behavior.zoom().on("zoom", null));

关于javascript - 如何禁用 D3.js 中的缩放功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23597184/

相关文章:

javascript - D3 V4 - 尝试绘制简单的折线图

javascript - D3 - 堆积条形图显示图表中的错误数据

css - 使用溢出滚动的文本制作一个div

javascript - 在 javascript 中寻找正确的正则表达式以进行模式匹配

php - JSON 数据未在 POST 中发送?

jquery - D3.js 缩放初始缩放级别

javascript - ScrollFire Materialize 显示列表

html - 固定在窗口 "Back To Top"按钮中,例如,当 HTML 页面滚动 200 像素时,该按钮可见

javascript - 内容脚本模式匹配

javascript - react ,无法从 onKeyPress 输入事件获取 event.key