I am using d3.js with force layout to visualize a large number of nodes. I would like to apply a restriction to the zoom pan option. JSFiddle: https://jsfiddle.net/40z5tw8h/24/
The above script contains a simple version of what I'm working on. Since I would potentially have to visualize a very large dataset, I use the function to scale the group holding element ("g") after the forces are completed. Thus, I always have a complete visualization, visible afterwards.
I would like to limit the panning - when the graph is fully visible so that it can only be moved in the viewport. If the layout is enlarged, I would like to limit the pan as follows:
The group hold item must not go away:
- down more than 20 pixels from the top of svg.
- on the right is more than 20 px on the left side of svg.
- above 20 pixels below svg.
- more than 20 pixels left on the right side of svg.
I think the whole implementation should be in the scaling function, which at the moment:
function zoomed(){
if (d3.event.sourceEvent == null){
g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
else{
var gElementBounds = g.node().getBoundingClientRect();
var g_bottom = gElementBounds.bottom;
var g_top = gElementBounds.top;
var g_left = gElementBounds.left;
var g_right = gElementBounds.right;
var g_height = gElementBounds.height;
var g_width = gElementBounds.width;
var svg = g.node().parentElement;
var svgElementBounds = svg.getBoundingClientRect();
var svg_bottom = svgElementBounds.bottom;
var svg_top = svgElementBounds.top;
var svg_left = svgElementBounds.left;
var svg_right = svgElementBounds.right;
var svg_height = svgElementBounds.height;
var svg_width = svgElementBounds.width;
var t = d3.event.translate;
var margin = 20;
if(d3.event.sourceEvent.type == 'wheel'){
g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
else{
g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
}
}
The restrictions that I tried to implement are commented out because they do not work properly.
Anyone have a solution?
source
share