Skip to content
Advertisement

getConnectedNodes direction parameter

I have a small issue with the parameter direction of the function getConnectedNodes() based on the Vis.js documentation (search for “getConnectedNodes” in the link)

Any idea to get the direction of the edges using the parameter (i don’t know how to)?

JSON Example

[ 
  { "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 ] } 
]

Here part of the code

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

Before hand, thanks a lot!

Advertisement

Answer

index is the index of the array like 0, 1, 2,,,. The start index is 0. On the other hand, elem is the object like {x: ###, y: ###, id: ###}. From these situation, I thought that index of getConnectedNodes(index) might be elem.id. So how about the following modification?

From:

elem.connections = network.getConnectedNodes(index);

To:

elem.connections = network.getConnectedNodes(elem.id, "from");
  • From the document, if you want to retrieve “parent”, you can retrieve it by adding from to the argument.

    • 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.
  • When you want to retrieve “child”, please add to to the argument instead of from.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement