【问题标题】:Modelling a simple graph with nodes and edges.用节点和边建模一个简单的图。
【发布时间】:2012-04-23 10:49:52
【问题描述】:

我试图使用 d3.js 可视化图表。我尝试了两种布局,bundle-radial 和 force。

Bundle-radial 不起作用,因为每个节点都需要一个父节点,它不适合图形可视化。

对于强制布局,脚本挂起。可能是因为这个图中有很多节点和边。我也不需要力布局附带的模拟。

在 d3.js 中还有其他布局可以尝试使用吗?

【问题讨论】:

  • 我们在谈论多少个节点和边?

标签: javascript d3.js


【解决方案1】:

如果不需要模拟,可以静态使用强制布局。调用force.start 后,调用force.tick 一定次数,然后调用force.stop 以停止模拟:

// Run the layout a fixed number of times.
// The ideal number of times scales with graph complexity.
// Of course, don't run too long—you'll hang the page!
force.start();
for (var i = n; i > 0; --i) force.tick();
force.stop();

在某些情况下,确定性地初始化节点位置以鼓励模拟快速收敛到一个好的解决方案可能会有所帮助。如果您不初始化位置,则力布局将随机初始化它们,因此可能有点不可预测。比如这里我沿对角线初始化节点:

// Initialize the positions deterministically, for better results.
var n = nodes.length;
nodes.forEach(function(d, i) { d.x = d.y = width / n * i; });

最后,如果您使用的是静态布局,请考虑使用 fisheye distortion 以仍然允许交互式探索。

【讨论】:

    猜你喜欢
    • 2011-06-09
    • 2017-12-22
    • 2018-12-05
    • 2020-04-02
    • 1970-01-01
    • 2011-06-21
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多