如何在d3的力布局图中获得相同的节点位置 [英] How to get the same node positions in d3's force layout graph

查看:158
本文介绍了如何在d3的力布局图中获得相同的节点位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个力向图,但是我需要它在每次生成时都保持不变(使用相同的数据). 有什么办法可以使用d3.js做到这一点?

I would like to create a force directed graph, but i need it to stay the same every time it is generated (with the same data). Is there any way to do this using d3.js?

更新:

我发现了工作解决方案,该解决方案基于使用

I found working solution, which is based on using seeded random number generator

// set the random seed
Math.seedrandom('mySeed');

推荐答案

您可以通过修改D3的力布局,或基于该布局创建自己的布局.至少在3个位置中将随机性(Math.Random)用于节点的定位(考虑到力布局引用其他代码,可能会更多).您必须消除所有随机性,才能使图形每次都以相同的方式显示:

You could by modifying D3's force layout, or by creating your own layout based on it. There are at least 3 places where randomness (Math.Random) is used in the positioning of nodes (there may be more, given the force layout references other code). You would have to eliminate all randomness in order to get the graph to display in the same way each time:

https://github.com/mbostock/d3/blob/master/src/layout/force.js

但是,这会妨碍布局的工作方式-它使用随机性将自身快速分类为清晰的图表.如果您的节点数很少,那么可能就不会有问题,但是大量节点可能会导致纠结.

However, this would hamper how the layout works – it's using randomness to sort itself out into a legible diagram quickly. If your number of nodes is small, then it probably wouldn't be an issue, but a large number of nodes could just end up a tangle.

这篇关于如何在d3的力布局图中获得相同的节点位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆