Search code examples
vis.jsvis.js-network

Always display network nodes in a ring in Vis.js


I tried reading the documentation but couldn't find what I was looking for. This is what I want, how can I do it? Thank you.

enter image description here


Solution

  • You can use the initRedraw event to calculate and to set the node coordinates for a circular layout:

    var radius = 150
    network.on('initRedraw', function () {
      var ids = data.nodes.getIds()
      var d = 2 * Math.PI / ids.length // Angular pitch
      ids.forEach(function(id, i) {
        var x = radius * Math.cos(d * i)
        var y = radius * Math.sin(d * i)
        network.moveNode(id, x, y)
      })
    })
    

    https://jsfiddle.net/L6s6hjwz/