Javascript d3.js力定向图保持恒定的链接距离

Javascript d3.js力定向图保持恒定的链接距离,javascript,d3.js,Javascript,D3.js,有人知道如何在排斥节点的同时保持恒定的链路距离吗 这是一个问题的示例,这是标准FDG示例,但节点较少 var graph = { "nodes":[ {"name":"a","group":1}, {"name":"a","group":1}, {"name":"a","group":1}, {"name":"a","group":1}, {"name":"b","group":8} ], "links":[ {"sourc

有人知道如何在排斥节点的同时保持恒定的链路距离吗

这是一个问题的示例,这是标准FDG示例,但节点较少

var graph = {
  "nodes":[
    {"name":"a","group":1},
      {"name":"a","group":1},
      {"name":"a","group":1},
      {"name":"a","group":1},
    {"name":"b","group":8}
  ],
  "links":[
    {"source":1,"target":0,"value":1},
      {"source":2,"target":0,"value":1},
      {"source":3,"target":0,"value":1},
      {"source":4,"target":0,"value":1}
  ]
};
var width = 300,
    height = 300;

var color = d3.scale.category20();

var force = d3.layout.force()
    .charge(-120)
    .linkDistance(30)
    .size([width, height]);

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

var drawGraph = function(graph) {
  force
      .nodes(graph.nodes)
      .links(graph.links)
      .start();

  var link = svg.selectAll(".link")
      .data(graph.links)
    .enter().append("line")
      .attr("class", "link")
      .style("stroke-width", function(d) { return Math.sqrt(d.value); });

  var gnodes = svg.selectAll('g.gnode')
  .data(graph.nodes)
  .enter()
  .append('g')
  .classed('gnode', true)
  .call(force.drag);

  var node = gnodes.append("circle")
      .attr("class", "node")
      .attr("r", 5)
      .style("fill", function(d) { return color(d.group); });

  node.append("title")
      .text(function(d) { return d.name; });

   var labels = gnodes.append("text")
              .text(function(d) { return d.name; })
              .attr('text-anchor', 'middle')
              .attr('font-size', 8.0)
              .attr('font-weight', 'bold')
              .attr('y', 2.5)
              .attr('fill', d3.rgb(50,50,50))
              .attr('class', 'node-label')
              .append("svg:title")
              .text(function(d) { return d.name; });

  force.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; })
    .each(function(d) { console.log(Math.sqrt((d.source.x - d.target.x) * (d.source.x - d.target.x) + (d.source.y - d.target.y) * (d.source.y - d.target.y))); });

                  gnodes.attr("transform", function(d) {
                      return 'translate(' + [d.x, d.y] + ')';
                  });
  });
};

drawGraph(graph);
有一个中心节点和四个连接节点。所有链环的长度都应该为30,但由于斥力的作用,链环的长度固定为35。有没有一种方法可以抵消这一点,使链接长度收敛到其期望值30,同时保持非连接节点之间的排斥

这类似于使连接力比斥力强得多。然而,增加这一点会导致非常不稳定的行为

提出这个问题的另一种方式是,是否有一种方法可以在保持所需链路长度的同时将节点彼此分散得尽可能远?

是的,使用.chargeDistance30。.chargeDistance设置确定应用电荷时的最大距离,默认情况下为无限。设置为30将您的费用设置为仅适用于30px范围内的节点,并应提供您想要的行为


这样做的缺点是,在大型图形上,您将不再看到可以更快地展开图形的附加效果,布局将具有更本地化的外观。为了达到这样的效果,我建议尝试与力算法的alpha参数相关的动态电荷距离,冷却时间从无穷大开始,然后随着图形冷却,向30或其他方向移动。

谢谢提示。我之前试过,但正是由于缺乏全局展开,使得这种方法次优。回想起来,也许这是一个愚蠢的问题,因为如果我们有两个节点和两条边,一条长度为3,另一条长度为4,会发生什么?它们需要收敛到某个平衡距离。