d3-force icon indicating copy to clipboard operation
d3-force copied to clipboard

add d3.forceExtent

Open gka opened this issue 9 years ago • 2 comments

I find this useful:

d3.forceExtent = function(extent) {
    var nodes;

    if (extent == null) extent=[[0,800], [0,500]];

    function force() {
        var i,
            n = nodes.length,
            node,
            r = 0;

        for (i = 0; i < n; ++i) {
            node = nodes[i];
            r = (node.radius || 0);
            node.x = Math.max(Math.min(node.x, extent[0][1]-r), extent[0][0]+r);
            node.y = Math.max(Math.min(node.y, extent[1][1]-r), extent[1][0]+r);
        }
    }

    force.initialize = function(_) { nodes = _; };

    force.extent = function(_) {
        return arguments.length ? (extent = _, force) : extent;
    };

    return force;
};

gka avatar Mar 10 '17 16:03 gka

Thank you, @gka. Your code saved me some time!

+1 for adding this. This kind of force helped me to capture orphan nodes and prevent them to fly away out of screen bounds

just-boris avatar Oct 29 '17 21:10 just-boris

This would certainly be useful, I've made PR #163 from your proposal, however changing the way the extent is defined and its defaults to [[xmin, ymin], [xmax, ymax]] = [[0,0], [960, 500]].

Fil avatar Jun 25 '20 16:06 Fil