"Pinning" nodes in a D3 force-directed graph

Tom Auger

I have constructed a regular "grid" that places nodes at evenly-spaced points across the grid. Then, by randomizing the linkDistance, I'm able to "stir up" the grid so it's less regular.

I'd like to "pin" all the edge points such that they don't move - leaving only the inner points to be affected by the force layout.

My insight was that since this is a regular quad grid, any points with a weight less than 4 would be an "edge" point, and thus should be pinned.

I figure that weight is only calculated after the nodes and links have been added to the force layout, so I'm forEaching through the nodes array, after adding it to the force layout, and conditionally setting the fixed property based on weight.

Then, I'm reapplying the nodes property and starting the simulation.

No good. In the example I'm attaching ALL the points move.

force = d3.layout.force()
                .size( [w, h ] )
                .nodes( nodes )
                .links( links )
                .linkDistance( function( d ){ return Math.random() * GRID_SPACING; } )
                .linkStrength( 1 )
                .charge( 0 )
                .gravity( 0 )
                .friction( .5 )
                .on( "tick", function() {
                    d3links.attr("x1", function(d) { return d.source.x; })
                        .attr("y1", function(d) { return d.source.y; })
                        .attr("x2", function(d) { return d.target.x; })
                        .attr("y2", function(d) { return d.target.y; });

                    d3nodes.attr("cx", function(d) { return d.x; })
                        .attr("cy", function(d) { return d.y; });
                } );

            // "Pin" all the edge nodes.
            nodes.forEach( function( node ){
                if ( node.weight < 4 ){
                    node.fixed = true;
                }
            } );

            force.nodes( nodes ).start();
Cool Blue

Your insight is a good one! But timing is everything...
The "start" event is triggered after the weights have been initialised so this should work...

force = d3.layout.force()
    .size([w, h])
    .nodes(nodes)
    .links(links)
    .linkDistance(function (d) { return Math.random() * GRID_SPACING; })
    .linkStrength(1)
    .charge(0)
    .gravity(0)
    .friction(.5)
    .on("tick", function () {
        d3links.attr("x1", function (d) { return d.source.x; })
                .attr("y1", function (d) { return d.source.y; })
                .attr("x2", function (d) { return d.target.x; })
                .attr("y2", function (d) { return d.target.y; });

        d3nodes.attr("cx", function (d) { return d.x; })
                .attr("cy", function (d) { return d.y; });
    })
    .on("start", function () {
        // "Pin" all the edge nodes.
        nodes.forEach(function (node) {
            if (node.weight < 4) {
                node.fixed = true;
            }
        });
    })


force.nodes(nodes).start();

(If you want to include drag behavior then you would need to re-fix after dragend also.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically adding nodes to d3.js Force directed graph

From Dev

3d.js - Updating nodes on a force directed graph

From Dev

Dynamically adding nodes to d3.js Force directed graph

From Dev

3d.js - Updating nodes on a force directed graph

From Dev

D3 force directed graph direction

From Dev

The links between the nodes in Force-Directed Graph in D3 Javascript

From Dev

How to restrict number of nodes initially in d3 Force directed graph

From Dev

How to make it so D3 Force Directed Graph generates nodes in same position each time

From Dev

d3 force directed graph nodes stay at fixed position after filter

From Dev

Introducing Arrow(directed), in Force Directed Graph d3

From Dev

d3 force directed graph downward force simulation

From Dev

D3 force directed graph: Performance issue in a complex graph

From Dev

D3 Force directed layout + All nodes to original positions

From Dev

Adding label to the links in D3 force directed graph

From Dev

d3 force directed graph remove text cursor

From Dev

d3 force directed graph don't select text

From Dev

semantic zooming of the force directed graph in d3

From Dev

modifying the d3 force-directed graph example

From Dev

d3 force directed graph, links not being drawn

From Dev

D3 force directed graph moving text

From Dev

D3 Force Directed Graph ajax update

From Dev

D3 force-directed graph: update node position

From Dev

modifying the d3 force-directed graph example

From Dev

Load JSON object into D3 Force Directed Graph

From Dev

Dynamically change color of D3 Node (force directed graph)

From Dev

D3js - Force directed graph - advanced highlighting of neigbour nodes and links, is it possible?

From Dev

D3.js network graph using force-directed layout and rectangles for nodes

From Dev

D3v4 force directed graph - localStorage disconnects links and nodes

From Dev

D3.js network graph using force-directed layout and rectangles for nodes

Related Related

  1. 1

    Dynamically adding nodes to d3.js Force directed graph

  2. 2

    3d.js - Updating nodes on a force directed graph

  3. 3

    Dynamically adding nodes to d3.js Force directed graph

  4. 4

    3d.js - Updating nodes on a force directed graph

  5. 5

    D3 force directed graph direction

  6. 6

    The links between the nodes in Force-Directed Graph in D3 Javascript

  7. 7

    How to restrict number of nodes initially in d3 Force directed graph

  8. 8

    How to make it so D3 Force Directed Graph generates nodes in same position each time

  9. 9

    d3 force directed graph nodes stay at fixed position after filter

  10. 10

    Introducing Arrow(directed), in Force Directed Graph d3

  11. 11

    d3 force directed graph downward force simulation

  12. 12

    D3 force directed graph: Performance issue in a complex graph

  13. 13

    D3 Force directed layout + All nodes to original positions

  14. 14

    Adding label to the links in D3 force directed graph

  15. 15

    d3 force directed graph remove text cursor

  16. 16

    d3 force directed graph don't select text

  17. 17

    semantic zooming of the force directed graph in d3

  18. 18

    modifying the d3 force-directed graph example

  19. 19

    d3 force directed graph, links not being drawn

  20. 20

    D3 force directed graph moving text

  21. 21

    D3 Force Directed Graph ajax update

  22. 22

    D3 force-directed graph: update node position

  23. 23

    modifying the d3 force-directed graph example

  24. 24

    Load JSON object into D3 Force Directed Graph

  25. 25

    Dynamically change color of D3 Node (force directed graph)

  26. 26

    D3js - Force directed graph - advanced highlighting of neigbour nodes and links, is it possible?

  27. 27

    D3.js network graph using force-directed layout and rectangles for nodes

  28. 28

    D3v4 force directed graph - localStorage disconnects links and nodes

  29. 29

    D3.js network graph using force-directed layout and rectangles for nodes

HotTag

Archive