getConnectedNodes方向参数

getConnectedNodes direction parameter

我对基于 Vis.js documentation 的函数 getConnectedNodes() 的参数 direction 有一个小问题(在 link 中搜索 "getConnectedNodes")

有没有使用参数获取边缘方向的想法(我不知道如何)?

JSON 例子

[ 
  { "x": 0, "y": 0, "id": "0", "connections": [ 2 ] // i think here should be a from?},
  { "x": 200, "y": 0, "id": "1", "connections": [ 3, 2 ] },
  { "x": 500, "y": 500, "id": "2", "connections": [ 0, 1 ] },
  { "x": 300, "y": -200, "id": "3", "connections": [ 1 ] } 
]

这里是部分代码

google.script.run.withSuccessHandler(([nodes, edges]) => new vis.Network(container, {nodes: nodes, edges: edges}, options)).sample();

let network;

function init() {
  container = document.getElementById('mynetwork');
  exportArea = document.getElementById('input_output');
  network = google.script.run.withSuccessHandler(([nodes, edges]) => {network = new vis.Network(container, {nodes: nodes, edges: edges}, options);}).sample();
};

function addConnections(elem, index) {
  elem.connections = network.getConnectedNodes(index);               < I THINK THE PROBLEM IS HERE
}

function exportNetwork() {
  var nodes = objectToArray(network.getPositions());
  nodes.forEach(addConnections);
  var exportValue = JSON.stringify(nodes, undefined, 2);
  exportArea.innerHTML = exportValue;
}

function objectToArray(obj) {
  return Object.keys(obj).map(function(key) {
    obj[key].id = key;
    return obj[key];
  });
}

手前,非常感谢!

index0, 1, 2,,,一样是数组的索引。起始索引为 0。另一方面,elem 是类似于 {x: ###, y: ###, id: ###} 的对象。从这些情况来看,我认为 getConnectedNodes(index)index 可能是 elem.id。那么下面的修改呢?

发件人:

elem.connections = network.getConnectedNodes(index);

收件人:

elem.connections = network.getConnectedNodes(elem.id, "from");
  • 来自the document,如果要检索“parent”,可以通过在参数中添加from来检索。

    • For a node id, returns an array with the id's of the connected nodes.
    • If optional parameter direction is set to string 'from', only parent nodes are returned.
    • If direction is set to 'to', only child nodes are returned.
    • Any other value or undefined returns both parent and child nodes.
  • 当您要检索“child”时,请在参数中添加to而不是from