我有一个可渲染的 cytoscape.js 图表。我对利用预设布局来放置节点感兴趣。 cytoscape.js 文档显示了以下预设布局:
var options = {
name: 'preset',
positions: undefined, // map of (node id) => (position obj); or function(node){ return somPos; }
zoom: undefined, // the zoom level to set (prob want fit = false if set)
pan: undefined, // the pan level to set (prob want fit = false if set)
fit: true, // whether to fit to viewport
padding: 30, // padding on fit
animate: false, // whether to transition the node positions
animationDuration: 500, // duration of animation in ms if enabled
animationEasing: undefined, // easing of animation if enabled
ready: undefined, // callback on layoutready
stop: undefined // callback on layoutstop
};
有人可以帮助我理解或举例说明文档中的以下内容的含义
// map of (node id) => (position obj); or function(node){ return somPos; }
我将所有节点存储在具有以下列的 MySQL 数据库表中
id、出发地、目的地、x 位置、y 位置
cytoscape.js 位置是否采用如下所示的字典:
{'id': 1, {'x':10, 'y':45}}, {'id': 2, {'x':21, 'y':32}}
等等?
最佳答案
这意味着当positions
设置为undefined
时,您需要创建一个函数来获取每个节点的位置。
例如:
cy.nodes().forEach(function(n){
var x = n.data("x");
var y = n.data("y");
});
这应该返回您的节点位置。
编辑
创建节点时可以设置节点位置。例如:
var elements;
elements: [{"data":{"id":"yourID","name":"name"},"position"{"x":"0.0","y":"0.0"}}];
有关更多信息,请参阅this Cytoscape js 网站上的演示。在这里,他们手动设置位置。
关于javascript - Cytoscape.js 预设布局文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42841293/