Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add d3.forceExtent #89

Open
gka opened this issue Mar 10, 2017 · 2 comments · May be fixed by #163
Open

add d3.forceExtent #89

gka opened this issue Mar 10, 2017 · 2 comments · May be fixed by #163

Comments

@gka
Copy link

gka commented Mar 10, 2017

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;
};
@just-boris
Copy link

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

Fil added a commit that referenced this issue Jun 25, 2020
fixes #89

note that the extent's definition in this PR is [[*xmin*, *ymin*], [*xmax*, *ymax*]], more in line with, for example zoom.extent. Defaults to [[0,0], [960,500]].
@Fil Fil linked a pull request Jun 25, 2020 that will close this issue
@Fil
Copy link
Member

Fil commented Jun 25, 2020

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]].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants