【问题标题】:Is it possible to skip the inital transitions in D3 force layout?是否可以跳过 D3 强制布局中的初始过渡?
【发布时间】:2016-07-08 11:21:43
【问题描述】:

我在这里使用了 D3 示例强制布局数据:https://bl.ocks.org/mbostock/4062045

并将其转移到此示例:https://bl.ocks.org/mbostock/1804919

结果 JSFiddle : https://jsfiddle.net/thatOneGuy/q2shr6ne/6/

这很好用,但我需要它才能与数千个节点一起工作。我已经用大约 3000 对它进行了测试,它需要几分钟才能解决。此处示例:https://jsfiddle.net/thatOneGuy/q2shr6ne/7/

我发现了这个例子以及更多:

How to speed up the force layout animation in d3.js

但我需要有他们的结束位置,因为我不希望在开始时有任何过渡。这可能吗?

它们如何定位的代码:

function tick(e) {

  circle
      .each(gravity(0.2*e.alpha))
      .each(collide(.5))
      .attr("cx", function(d) { return d.x; })
      .attr("cy", function(d) { return d.y; });
}

// Move nodes toward cluster focus.
function gravity(alpha) {
  return function(d) { 
    d.y += (d.cy - d.y) * alpha;
    d.x += (d.cx - d.x) * alpha;
  };
}

// Resolve collisions between nodes.
function collide(alpha) {
  var quadtree = d3.geom.quadtree(data.nodes);
  return function(d) {
 // console.log(d.radius)
    var r = d.radius + maxRadius + padding,
        nx1 = d.x - r,
        nx2 = d.x + r,
        ny1 = d.y - r,
        ny2 = d.y + r;
    quadtree.visit(function(quad, x1, y1, x2, y2) {
      if (quad.point && (quad.point !== d)) {
        var x = d.x - quad.point.x,
            y = d.y - quad.point.y,
            l = Math.sqrt(x * x + y * y),
            r = d.radius + quad.point.radius + (d.color !== quad.point.color) * padding;
        if (l < r) {
          l = (l - r) / l * alpha;
          d.x -= x *= l;
          d.y -= y *= l;
          quad.point.x += x;
          quad.point.y += y;
        }
      }
      return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
    });
  };
}

我找到了这个例子:http://bl.ocks.org/mbostock/1667139

但是实施起来很困难......

【问题讨论】:

    标签: javascript d3.js


    【解决方案1】:

    你的例子对我来说很好:

      setTimeout(function() {
    
          force.start();
          for (var i = n * n; i > 0; --i) force.tick();
          force.stop();
      }, 10);
    

    并在force 定义后注释start()

    见:https://jsfiddle.net/q2shr6ne/8/

    【讨论】:

    • 我认为 force.tick 正在调用默认的刻度函数而不是记住我将其设置为 .on('tick',tick),这让我自己的事情变得复杂了。感谢您指出这一点:)
    猜你喜欢
    • 2013-06-14
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 2016-07-17
    相关资源
    最近更新 更多