javascriptvivagraphjs

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 set, but for some reason I cannot access it.

var layout = Viva.Graph.Layout.forceDirected(graph, {
   springLength : 20,
   springCoeff : 0.0008,
   dragCoeff : 0.1,
   gravity : -10,
   theta : 0.5
});

var graphics = Viva.Graph.View.webglGraphics();
var nodeSize = 10;

graphics.setNodeProgram(Viva.Graph.View.webglImageNodeProgram());

graphics.node(function(node) {
    //url location on your server
    var ui = Viva.Graph.View.webglImage(nodeSize, 'https://lh4.googleusercontent.com/' + node.data);
    console.dir(ui);
    return ui;
}).link(function(link) {
       return Viva.Graph.View.webglLine(colors[(Math.random() * colors.length) << 0]);
   });

graph.forEachNode(function(node) { 
    console.log('pos ' + node.position); 
});

Solution

  • 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; 
    });