Andre driem
Andre driem

Reputation: 15

With the directed sortMethod, is it possible to determine relative position of nodes in the same hierarchy?

For exemple I have a simple network:

var options = {
  style = {
    hierarchical = {
      sortMethod = 'directed'
    }
  }               
}    
var nodes = new vis.DataSet([
  {id: 1, label: 'Node 1'},
  {id: 2, label: 'Node 2'},
  {id: 3, label: 'Node 3'},
  {id: 4, label: 'Node 4'},
  {id: 5, label: 'Node 5'}
]);

var edges = new vis.DataSet([
  {from: 1, to: 2},
  {from: 2, to: 3},
  {from: 4, to: 3},
  {from: 5, to: 3}
]);

Is there a way to indicate that I want the node 2 to be rendered between node 4 and 5?

Upvotes: 1

Views: 70

Answers (1)

YakovL
YakovL

Reputation: 8316

I'm afraid the only way is to set coordinates of the nodes explicitly:

var nodes = new vis.DataSet([
  {id: 1, label: 'Node 1', x: 0, y: -100},
  {id: 2, label: 'Node 2', x: 0, y:    0},
  {id: 3, label: 'Node 3', x: 0, y:  100},
  {id: 4, label: 'Node 4', x:  100, y: 0},
  {id: 5, label: 'Node 5', x: -100, y: 0}
]);

The point is, physics works only with existing nodes and edges. Still, you can try one of the following tricks:

  • add hidden edges between 2 and 4 and 2 and 5 – this way, you may hope that because nodes in those pairs are attracted to each other, node 2 ends up at least closer to both 4 and 5 than 4 to 5;
  • or instead of setting coordinates explicitly, use some pre-defined layout to put nodes in the desired order; however, as far as I know, no existing layout really garantees that this will be so (see these, these issues: this is vis.js-version-dependent and not reliable)

Upvotes: 1

Related Questions