Hi,
I am absolutely new to D3js and got stucked at one translate and scale problem.
I want to scale my D3 force layout to e.g. 400% and then make it center on the screen, if this has been displaced after scaling. So I first scale it and then check the Bounding Box of the <g> which contains the entire force layout. If the group is out screen area then transition(0,0). (I also tried center(0,0). The problem is - when <g> is transitioned (or centered) the scale gets reset to 100%. Here is my code.
var vis = d3.select("#graph").append("svg:svg")
.attr("width", 960)
.attr("height", 450)
.append('g')
.attr('id', 'mygroup');
/*
More code goes here to draw ~15 nodes, text, and images
*/
function zoom(scaleValue){
//this is working fine, to scale vis at xx% whatever comes in scaleValue.
vis.attr("transform", "scale(" + scaleValue + ")");
var bbox = document.getElementById("mygroup").getBBox();
var newX = 0;
var newY = 0;
if (bbox.y < 0) {
newY = -(bbox.y);
}
if (bbox.x < 0) {
newX = -(bbox.x);
}
//animate vis to visible area
vis.transition()
.duration(2000)
.attr("transform", "scale(" + scaleValue + ")" + "translate(" + newX + "," + newY + ")");
/************also tried center(0,0) *****************/
/*vis.transition()
.duration(2000)
.attr("transform", "scale(" + scaleValue + ")" + "center(0,0)");*/
}
Why is scale is getting reset to 100% when I translate it second time.
thanks,
Komal