How do I specify the initial zoom level in D3?

前端 未结 3 2033
臣服心动
臣服心动 2021-01-04 09:57

I have written a script to graphically display some row-oriented data, with bars, labels and connections between rows. As the dataset has grown, the resulting SVG element ha

相关标签:
3条回答
  • 2021-01-04 10:25

    As of D3 2.8.0, the new d3.behavior.zoom allows you to set the current scale, e.g.

    d3.behavior.zoom()
        .scale(currentScale);
    
    0 讨论(0)
  • 2021-01-04 10:39

    After setting the zoom value, you have to call the zoom event to make the changes appear on the screen. Here's how you do it:

    //set the min and max extent to which zooming can occur and define a mouse zoom function
    var zoom = d3.behavior.zoom().scaleExtent([0.3, 3]).on("zoom", zoomed);
    zoom.translate([50,50]).scale(2);//translate and scale to whatever value you wish
    zoom.event(yourSVGlayer.transition().duration(50));//does a zoom
    //this function handles the zoom behaviour when you zoom with the mouse
    function zoomed() { yourSVGlayer.attr("transform", "translate("+d3.event.translate+")scale(" + d3.event.scale + ")"); }
    
    0 讨论(0)
  • 2021-01-04 10:43

    In d3 v4, you can use:

    svg.call(zoom.transform, d3.zoomIdentity.scale(X_SCALE,Y_SCALE));

    Simple example:

    var zoom = d3.zoom()
            .on("zoom", zoomed);
    
    var zoomer = svg.append("rect")
            .call(zoom)
            .call(zoom.transform, d3.zoomIdentity.scale(2,2));
    
    function zoomed() {
            svg.attr("transform", d3.event.transform);
        }
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题