Skip to content Skip to sidebar Skip to footer

How Do I Get Or Set X, Y Position Of Node

I tried the following code for getting the position of a node in Vivagraph.js. Not sure why it is returning undefined. When I console.dir(node) I do see that a position is being s

Solution 1:

According to the docs HERE, node.position is no longer a valid property. Use layout.getNodePosition(node.id) instead.

To quote the docs (in case that link breaks):

position attribute is moved out from the node object into layout provider.

Why? Having shared node position makes impossible rendering of the same graph by two different layouters.

v.0.4.*

// each node object has "position" on it:
graph.forEachNode(function (node) {
  var position = node.position;
  position.x += 1; // move by one pixel
});

v.0.5.*

// "position" is now part of layouter:
graph.forEachNode(function (node) {
  // layout here is instance of Viva.Graph.Layout.forceDirected or Viva.Graph.Layout.constant:var position = layout.getNodePosition(node.id);
  position.x += 1; 
});

Post a Comment for "How Do I Get Or Set X, Y Position Of Node"