看起来最大的计算难题实际上出现在 .on("tick", tickActions) 步骤中,当时代码正在计算如何绘制所有路径,甚至是那些不可见的路径。
对于任何感兴趣的人,我将tick actions 函数更改为在绘制边缘之前先检查type 变量:
function tickActions() {
// plot the curved links
link.attr("d", function(d) {
if (d.type != draw_type) return null;
// code to draw paths
});
}
通过更改draw_type 变量,您可以决定实际计算和绘制哪些边。
除此之外,您还需要忽略某些边缘的强度。我知道我希望我的图表仅基于直边进行间隔,因此我对 simulation.force('link', link_force) 属性执行了以下操作:
var link_force = d3.forceLink()
// code for .id and .distance attributes
// return 0 for all non
.strength(function(d) {
if (d.type != 'straight') return 0;
return 0.3;
});
通过将所有非直边的强度设置为0,力算法在间隔节点时基本上会忽略它们。
最后,我添加了一个更新图表的restart_network() 函数。可以使用此函数来更改图表所看到的实际链接数据,但我决定将其他更改也包括在内。
function restart_network() {
simulation.force("link", link_force);
simulation.alpha(1).restart();
}