简体   繁体   中英

Single node bug in Arbor.JS

When creating a trivial Arbor JS graph with a single node, the node jitters around all over the place and no further nodes can be added.

The problem is also reported here by another user:

https://github.com/samizdatco/arbor/issues/12

Would appreciate your help with a fix / workaround

使用d3.js库而不是Arbor解决了我的问题:-)

We use a workaround solution for this. Its probably more of a hack than a complete solution, but it's easy to implement and works fine for most cases.

What we do is that we determine the number of particles in the system every time a particle is added or removed. If this count is equal to one, we add a new particle in the system, with its colour set to the canvas background colour. Since the node's colour is same as its background, it's not visible.

So at no point there is a single node in the graph. Whenever that happens due to an addition or deletion, we add this hidden balancing node. You may take a look at our website to see a live example for the above: http://www.graphthinker.com . As you add nodes, you may see that the graph continues to be responsive even when it has just a single (visible) node.

This hidden balancing node could be removed when there is no longer a need for it, say when another node gets added, or when the only visible node is removed.

It's not really a fix but I count the number of nodes and if less than on I set the friction to 1.0

if (nodeCount == 1) {
   //Stop single nodes bouncing all over the place
   sys.parameters({ friction: '1.0' });
}

Another option is to replace the physics.js file with this one. It has several fixes to compensate for the issues around having a single node (including problems adding the second one).

try this

 if (nodeCount == 1) { sys.parameters({ repulsion: 10, gravity: false, dt: 0.035 }) } else if (nodeCount > 1 && nodeCount < 30) { sys.parameters({ repulsion: 1000, gravity: false, dt: 0.35 }) } else { sys.parameters({ friction: .1, stiffness: 0.1, repulsion: 1, gravity: false, dt: 0.035 }) } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM