Search code examples
javascriptgraphvis.js-networkvis.js

Change physics based on group - visjs


I would like to make some groups of nodes in my Graph behave differently than the other nodes.
For example I have a group called "properties" that I dynamicly add to the graph. I need to change the gravity constants or weights for this group so they are closer to the parent then the rest of the nodes.

example

I hope this example helps visualize the problem. (Had to black out the labels because of sensitive info)

How would I accomplish this in vis.js?


Solution

  • //

    Hi Json.

    see the attached code snippet. hope it helps you.

    // create an array with nodes
      var nodes = new vis.DataSet([
        {id: 1, label: 'Node 1', group: 1},
        {id: 2, label: 'Node 2', group: 2},
        {id: 3, label: 'Node 3', group: 1},
        {id: 4, label: 'Node 4', group: 2},
        {id: 5, label: 'Node 5', group: 2}
      ]);
    
      // create an array with edges
      var edges = new vis.DataSet([
        {from: 1, to: 3},
        {from: 2, to: 4},
        {from: 2, to: 5}
      ]);
    
      // create a network
      var container = document.getElementById('mynetwork');
      var data = {
        nodes: nodes,
        edges: edges
      };
      var options = {
        groups: {
          1: { color: 'red', mass: 500 },// try to change this value
          2: { color: 'blue', mass: 5 }
        },
        physics: true
      };
      var network = new vis.Network(container, data, options);
      
    #mynetwork {
          width: 600px;
          height: 400px;
          border: 1px solid lightgray;
        }
    
        p {
          max-width: 600px;
        }
    <!doctype html>
    <html>
    <head>
      <title>Network | Basic usage</title>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.20.0/vis.min.js"></script>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.20.0/vis.min.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
    <div id="mynetwork"></div>
    
    
    
    </body>
    </html>