Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make node spacing change dynamically in vis.js

I am trying to visualize some graphs. Unfortunately I often get situation when one node overlaps another, as shown below:

Setting nodeSpacing option doesn't fit me because some node labels are extremely long and some are short. Is there any option setting dynamical positioning? Here are my options:

    var options = {
         layout:{
             hierarchical: {
                 enabled:true,
                 parentCentralization: true,
                 direction: 'DU',
                 sortMethod: 'directed'
                }
         },
         edges:{
             arrows: 'from'
         }
 }
like image 339
Maroth Avatar asked Jul 29 '26 19:07

Maroth


1 Answers

In the Physics options there is an option for hierarchicalRepulsion which you can play with - it all depends on your graph, I guess. I took their basic example of a hierarchical layout and added really long label names and used this config:

physics: {
    enabled: true,
    hierarchicalRepulsion: {
        centralGravity: 0.0,
        springLength: 500,
        springConstant: 0.01,
        nodeDistance: 400,
        damping: 0.09
    },
    solver: 'hierarchicalRepulsion'
}

I played around with large values for springLength and nodeDistance and this is the sort of thing you can get back:

enter image description here

Zoomed in a bit more, so you can see the captions:

enter image description here

There's still a bit of overlap lower down the hierarchy though - but it gives you a starting point to play with.

Also, consider the usability of really long captions - at some point you might want to start using tool-tips and other work-arounds. Once again - all depends on your network.

Good luck - it's an awesome library!

like image 125
Robin Mackenzie Avatar answered Aug 01 '26 10:08

Robin Mackenzie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!