Accessing node data in vis.js click handler
Asked Answered
S

2

18

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?

Summand answered 10/3, 2016 at 2:27 Comment(0)
C
33

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);
});
Czechoslovak answered 10/3, 2016 at 15:18 Comment(1)
This show all nodes. How can i get the selected node only ?Aubreir
C
0

How to get the clicked Node and retrieve Node-Information from that node:

network.on( 'click', function(properties) {
    var nodeId = network.getNodeAt({x:properties.event.srcEvent.offsetX, y:properties.event.srcEvent.offsetY});
    var node = nodes.get(nodeId);
});
Cerebrum answered 11/8, 2023 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.