0

我尝试了以下代码来获取 Vivagraph.js 中节点的位置。不知道为什么它返回未定义。

当我 console.dir(node) 我确实看到正在设置一个位置,但由于某种原因我无法访问它。

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

1 回答 1

2

根据文档HEREnode.position不再是有效属性。改为使用layout.getNodePosition(node.id)

引用文档(以防链接中断):

位置属性从节点对象移出到布局提供者中。

为什么?共享节点位置使得两个不同的布局器无法渲染相同的图形。

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; 
});
于 2014-04-27T04:37:38.817 回答