Anand Sainath
Anand Sainath

Reputation: 1807

D3 Force Graph - Fixed Nodes that Don't Overlap

I am looking to develop a viz that consists of a node link graph. I have a series of points whose position I don't want to change unless there is a collision (one node on another) on the graph. In case of collided nodes, I want to space them so that they don't overlap. My JS code is as below

var chartWidth = 200;
var chartHeight = 200;
var widthPadding = 40;
var heightPadding = 40;

var link, node;

$(function(){
    initialize();
});


function initialize() {
    var jsonString  = '{"nodes":[{"x":40,"y":64,"r":6,"fixed":true},{"x":40,"y":63,"r":6,"fixed":true},{"x":119,"y":53,"r":6,"fixed":true},{"x":119,"y":73,"r":6,"fixed":true},{"x":137,"y":73,"r":6,"fixed":true},{"x":140,"y":140,"r":6,"fixed":true},{"x":68,"y":57,"r":6,"fixed":true},{"x":70,"y":75,"r":6,"fixed":true},{"x":51,"y":59,"r":6,"fixed":true},{"x":51,"y":54,"r":6,"fixed":true},{"x":137,"y":40,"r":6,"fixed":true}],"links":[{"source":0,"target":1},{"source":1,"target":2},{"source":2,"target":3},{"source":3,"target":4},{"source":4,"target":5},{"source":0,"target":1},{"source":1,"target":6},{"source":6,"target":7},{"source":7,"target":4},{"source":4,"target":5},{"source":0,"target":1},{"source":1,"target":8},{"source":8,"target":9},{"source":9,"target":10},{"source":10,"target":5}]}';
    drawForceDirectedNodeLink($.parseJSON(jsonString));
}


function drawForceDirectedNodeLink(graph){
var width = chartWidth + (2*widthPadding);
var height = chartHeight + (2*heightPadding);

var q = d3.geom.quadtree(graph.nodes),
  i = 0,
  n = graph.nodes.length;

while (++i < n) {
    q.visit(collide(graph.nodes[i]));
}

var force = d3.layout.force()
    .size([width, height])
    .gravity(0.05)
    .on("tick", function(){
        link.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; });

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

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

var link = svg.selectAll(".link"),
    node = svg.selectAll(".node");

force
  .nodes(graph.nodes)
  .links(graph.links)
  .start();

link = link.data(graph.links)
    .enter().append("line")
    .attr("class", "link");

node = node.data(graph.nodes)
    .enter().append("circle")
    .attr("class", "node");
}


function collide(node) {
  var r = node.radius + 16,
      nx1 = node.x - r,
      nx2 = node.x + r,
      ny1 = node.y - r,
      ny2 = node.y + r;
  return function(quad, x1, y1, x2, y2) {
        if (quad.point && (quad.point !== node)) {
          var x = node.x - quad.point.x,
              y = node.y - quad.point.y,
              l = Math.sqrt(x * x + y * y),
              r = node.radius + quad.point.radius;
          if (l < r) {
            l = (l - r) / l * .5;
            node.x -= x *= l;
            node.y -= y *= l;
            quad.point.x += x;
            quad.point.y += y;
          }
        }
        return x1 > nx2
            || x2 < nx1
            || y1 > ny2
            || y2 < ny1;
  };
}

As you can see, I have tried to implement the collision detection logic mentioned here. But some how I have not been able to get that part work.

Also attaching the output that I've managed until now

Upvotes: 0

Views: 1729

Answers (2)

Walter Roman
Walter Roman

Reputation: 4779

Notice that within your jsonString declaration inside of initialize(), each node is being given an r property. However, further down within collide(), you're doing the following:

.attr("r", function(d) { return d.radius - 2; })

Make sure your nodes have a radius property attached to them. If not, the following change should do it:

.attr("r", function(d) { return d.r - 2; })

You can see on line 30 of Mike Bostock's script that his nodes are initially declared with a radius property, as opposed to your r property.

var nodes = d3.range(200).map(function() { return {radius: Math.random() * 12 + 4}; }),

Upvotes: 2

redmallard
redmallard

Reputation: 1138

UPDATE

Change node.radius to node.r and quad.point.radius to quad.point.r. And it should work. Looks like it was just a NaN problem.

Upvotes: 1

Related Questions