D3 force directed graph: Performance issue in a complex graph

Kop4lyf

I created a D3 force directed graph having reactangles as nodes and links joining them at the end of the perimeter. Since I need to align the divs according to the rectaengles, I have created two svgs and links in svg1 and acc to z-index, above it are divs and above divs is second svg containing rectangles/nodes( having opacity 0 so that divs are visible). The issue I am having is that the graph is slow in IPAD and it's browser minimizes about 80% times on opening of the page on which the graph was drawn. On debugging, I saw that the problem is with my tick function, whose code is here,

force.start();
q = d3.geom.quadtree(nodes),count=0;
// define what to do one each tick of the animation
force.on("tick", function() {
    i = 0;
    //applying collision detection to avoid overlapping by default
    if(!mapCreated){
       while (++i < nodes.length) q.visit(collide(nodes[i]));
    }

    //this checks if node tries to move out (limitToCorners does that)
    node.attr("x", function(d) { return d.x = limitToCorners(d.x,"x"); })
    .attr("y", function(d) { return d.y = limitToCorners(d.y,"y"); });

    //this puts the link attribute to link directly to center of rectangle
    link.attr("x1", function(d) { return d.source.x+nodeModel.width/2; })
    .attr("y1", function(d) { return d.source.y+nodeModel.height/2; })
    .attr("x2", function(d) { return d.target.x+nodeModel.width/2; })
    .attr("y2", function(d) { return d.target.y+nodeModel.height/2; });

    //changing the CSS so that divs are in same place as the nodes
    changeGoalsPosition(refList);

    // changing the attributes of lines on svg so that it comes to the end of rectange (calculateLinkEndPoints does that)
   for(i=0;i<lines.length;i++){
      var obj = {};
      obj.source = {
            x: parseInt(linksRefList[i].attr("x1")),
            y: parseInt(linksRefList[i].attr("y1"))
      };
      obj.target = {
        x: parseInt(linksRefList[i].attr("x2")),
        y: parseInt(linksRefList[i].attr("y2"))
      };
      $(lines[i]).attr("x1", function(d) { return calculateLinkEndPoints(obj,"sx"); })
      .attr("y1", function(d) { return calculateLinkEndPoints(obj,"sy"); })
      .attr("x2", function(d) { return calculateLinkEndPoints(obj,"tx"); })
      .attr("y2", function(d) { return calculateLinkEndPoints(obj,"ty"); });
   }
 });    
Lars Kotthoff

The processing you're doing in the tick function is quite expensive. You basically have 2 options.

  • Reduce the number of nodes to be processed.
  • Reduce the number of processings of tick events.

Whether you can do the former will depend on your application. For the latter, you could do something like this to skip every other event.

var process = 1;
force.on("tick", function() {
    if(process) {
        // do processing
    }
    process = 1 - process;
});

You could of course skip more events in a similar fashion. At some point, you might notice that the layout becomes "jumpier" because of the skipped events. You can mitigate this problem by moving the elements to their new positions using a transition instead of simply setting them.

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 direction

From Dev

Introducing Arrow(directed), in Force Directed Graph d3

From Dev

d3 force directed graph downward force simulation

From Dev

"Pinning" nodes in a D3 force-directed graph

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

JUNG force directed graph

From Dev

d3 Directed Graph Editor additions

From Dev

parameterized D3js force directed graph node positioning

From Dev

Dynamically adding nodes to d3.js Force directed graph

From Dev

D3.js Titles on Collapsible 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

Search node in force directed graph using d3.js

From Dev

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

From Dev

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

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

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

Related Related

HotTag

Archive