我尝试使用以下代码来获取 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);
});
最佳答案
根据文档HERE ,node.position
不再是有效的属性。请改用 layout.getNodePosition(node.id)
。
引用文档(以防链接中断):
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;
});
关于javascript - 如何获取或设置节点的 x, y 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23302066/