【问题标题】:Can't call force.tick() from a force graph with a tree layout无法从具有树形布局的力图中调用 force.tick()
【发布时间】:2014-07-21 08:55:19
【问题描述】:

我有一个基于force-collapsable example 的可视化,我想加快或跳过初始节点移动以尽快使图表达到平衡。

我遵循了answer 中的建议,并从我的代码中调用了 tick(),但它在 force-collabsable 示例中不起作用,因为 force 对象上没有 tick 或 alpha 方法。

这是我的代码在修改后的样子,我删除了 on tick 调用并在最后添加了 force.tick() 调用:

var force = d3.layout.force()  // line 38 in the example
    //.on("tick", tick)        // commented out as I call tick instead
    .charge(function(d) { return d._children ? -d.size / 100 : -30; })
    .linkDistance(function(d) { return d.target._children ? 80 : 30; })
    .size([w, h - 160]);

function update() {            // line 56 in the example
  var nodes = flatten(root),
      links = d3.layout.tree().links(nodes);

  force                        // line 60 in the example
    .nodes(nodes)
    .links(links)
    .start();

  force.tick()                 // added by me to cause a tick and the point of error

但调用失败并显示“未捕获类型错误:未定义不是函数”,因为该方法不存在。

当我调用 force 对象时(取自 Chrome 调试器),它具有以下值:

charge: function (x) { distance: function (x) { drag: function () { friction: function (x) { gravity: function (x) { linkDistance: function (x) { linkStrength: function (x) { links: function (x) { nodes: function (x) { on: function (type, listener) { resume: function () { size: function (x) { start: function () { stop: function () { theta: function (x) { proto: Object

为什么这个例子中的力对象与其他不使用树形布局的力布局例子不同?如何直接调用tick()?

【问题讨论】:

  • 没有答案的链接。
  • 能贴出相关代码吗? JS错误的行号也会有帮助,所以我们知道什么是未定义的。

标签: javascript svg d3.js force-layout


【解决方案1】:

我发现如果我删除了对 d3.geom.js 和 d3.layout.js 的引用,我可以调用 tick() 并且在没有这些文件的情况下不会对我的布局产生负面影响。

【讨论】:

    猜你喜欢
    • 2019-09-20
    • 2017-02-10
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2013-04-21
    • 2012-04-19
    相关资源
    最近更新 更多