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

user3014093

When I call d3.start() on a d3.js force directed I generate a graph with nodes and edges in particular locations. However, when I call start again with the same data, the positioning of the nodes is different than previously. Is there a way to make it so that nodes are positioned in the same positions each time?

Do I absolutely set the first nodes position, and then somehow position relatively to that first nodes position each time?Thanks.

My code for building the d3-force graph is boilerplate.

And following my collection of data from my internal json and creating the node and link objects I call

setTimeout(function(){
    var n = node.size();
    node.forEach(function(d, i) {
      d.x = d.y = width / n * i;
    });
    console.log(node);
    setTimeout(function(){
      force.start();
      for (var i = 0; i < n; ++i) force.tick();
      force.stop();
    },300);
},100);

where node is the array of circle objects I use for nodes...Also it does not seem like the nodes are initializing along the diagonal for when I first call start. Any thoughts? Thanks!

Naor Biton

According to the d3 documentation, aligning your nodes along a diagonal line, will make the force directed algorithm balance the nodes positions with the least number of iterations in most cases.

I use it this way and I get consistent positions every time I use the same data set.

The number of iterations depends on the graph size and complexity. The choice of initial positions can also have a dramatic impact on how quickly the graph converges on a good solution. For example, here the nodes are arranged along the diagonal:

var n = nodes.length;
nodes.forEach(function(d, i) {
  d.x = d.y = width / n * i;
});

From: https://github.com/mbostock/d3/wiki/Force-Layout

This means that setting your nodes' positions is redundant. Let the force directed sort all the positions for you, by calling .start() after you've done something similar to the code above.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

d3 force directed graph nodes stay at fixed position after filter

From Dev

"Pinning" nodes in a D3 force-directed graph

From Dev

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

From Dev

D3 force-directed graph: update node position

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

Introducing Arrow(directed), in Force Directed Graph d3

From Dev

How to append images as nodes in D3 force layout graph?

From Dev

d3.js force directed graph: How to make node size depends on the value of the links?

From Dev

d3 force directed graph downward force simulation

From Dev

D3 force directed graph: Performance issue in a complex graph

From Dev

How to add a dynamic legend to a D3 force directed graph in Apex?

From Dev

How to draw a simple Force-directed graph in D3 Javascript

From Dev

How to render links as elbow connectors in d3 force directed graph

From Dev

How to choose which JSON file to use in your D3 force directed graph

From Dev

How do I zoom in on my force-directed graph in d3?

From Dev

D3 Force directed layout + All nodes to original positions

From Dev

how to adjust size of force directed graph in d3.js?

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

Related Related

  1. 1

    d3 force directed graph nodes stay at fixed position after filter

  2. 2

    "Pinning" nodes in a D3 force-directed graph

  3. 3

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

  4. 4

    D3 force-directed graph: update node position

  5. 5

    Dynamically adding nodes to d3.js Force directed graph

  6. 6

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

  7. 7

    Dynamically adding nodes to d3.js Force directed graph

  8. 8

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

  9. 9

    D3 force directed graph direction

  10. 10

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

  11. 11

    Introducing Arrow(directed), in Force Directed Graph d3

  12. 12

    How to append images as nodes in D3 force layout graph?

  13. 13

    d3.js force directed graph: How to make node size depends on the value of the links?

  14. 14

    d3 force directed graph downward force simulation

  15. 15

    D3 force directed graph: Performance issue in a complex graph

  16. 16

    How to add a dynamic legend to a D3 force directed graph in Apex?

  17. 17

    How to draw a simple Force-directed graph in D3 Javascript

  18. 18

    How to render links as elbow connectors in d3 force directed graph

  19. 19

    How to choose which JSON file to use in your D3 force directed graph

  20. 20

    How do I zoom in on my force-directed graph in d3?

  21. 21

    D3 Force directed layout + All nodes to original positions

  22. 22

    how to adjust size of force directed graph in d3.js?

  23. 23

    Adding label to the links in D3 force directed graph

  24. 24

    d3 force directed graph remove text cursor

  25. 25

    d3 force directed graph don't select text

  26. 26

    semantic zooming of the force directed graph in d3

  27. 27

    modifying the d3 force-directed graph example

  28. 28

    d3 force directed graph, links not being drawn

  29. 29

    D3 force directed graph moving text

HotTag

Archive