简体   繁体   English

如何在D3.js中禁用缩放功能?

[英]How do I disable my zoom functionality in D3.js?

I have a zoom functionality made in D3, but I'd like to make it optional, so I'd like a way to turn it off. 我在D3中具有缩放功能,但是我想将其设为可选,所以我想将其关闭。 Here's my code: 这是我的代码:

//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);});
    }
}

As you can see I tried using an if-statement to prevent the zoom functionality from working when a checkbox isn't ticked. 如您所见,我尝试使用if语句来防止未选中复选框时缩放功能的工作。 This prevents users from scrolling on the page when the mouse is inside of the SVG frame. 这可以防止用户将鼠标置于SVG框架内时在页面上滚动。

I'd like the correct way to do this. 我想要正确的方法。 Thanks very much in advance! 首先十分感谢!

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

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

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

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