Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing node data in vis.js click handler

I have a network graph of nodes and edges and would like to get the node data once it gets clicked. e.g,

var network = new vis.Network(container, data, options);
network.on( 'click', function(properties) {
    console.log('clicked node ' + properties.nodes);
});

But this just returns some internal id [105]. Is there a way to get the actual data that is associated with the node?

like image 821
Vishnu Avatar asked Mar 10 '16 02:03

Vishnu


1 Answers

The node ids that you get in the properties is not "some internal id", but these are the id's of the nodes that you defined yourself. You can simply read the node's data from your own DataSet with nodes like:

var nodes = new vis.DataSet([...]);
var edges = new vis.DataSet([...]);
var data = {nodes: nodes, edges: edges};

var network = new vis.Network(container, data, options);
network.on( 'click', function(properties) {
    var ids = properties.nodes;
    var clickedNodes = nodes.get(ids);
    console.log('clicked nodes:', clickedNodes);
});
like image 172
Jos de Jong Avatar answered Nov 18 '22 21:11

Jos de Jong