0

我正在寻找一个包含节点链接图的可视化项。我有一系列点,我不想改变它们的位置,除非图表上存在碰撞(一个节点在另一个节点上)。如果发生碰撞节点,我想将它们隔开,以免它们重叠。我的JS代码如下

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;
  };
}

如您所见,我已尝试实现此处提到的碰撞检测逻辑。但是有些我无法使那部分工作。

还附上我到目前为止管理的输出

4

2 回答 2

2

请注意,在您的jsonString声明中initialize(),每个节点都被赋予了一个r属性。但是,在 内部collide(),您正在执行以下操作:

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

确保您的节点具有radius附加到它们的属性。如果不是,则应进行以下更改:

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

您可以在 Mike Bostock 脚本的第 30 行看到,他的节点最初是使用radius属性声明的,而不是您的r属性。

var nodes = d3.range(200).map(function() { return {radius: Math.random() * 12 + 4}; }),
于 2013-10-30T02:39:02.583 回答
1

更新

更改node.radiusnode.r和。quad.point.radius_ quad.point.r它应该工作。看起来这只是一个NaN问题。

于 2013-10-30T01:41:36.470 回答