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
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);
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 + ")"); }
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.