访问 vis.js 单击处理程序中的节点数据

Accessing node data in vis.js click handler

我有一个节点和边的网络图,我想在节点被点击后获取节点数据。例如,

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

但这只是 returns 一些内部 ID [105]。有没有办法获取与节点关联的实际数据?

您在属性中获得的节点 ID 不是 "some internal id",但这些是您自己定义的节点的 ID。您可以简单地从您自己的 DataSet 中读取节点的数据,其中的节点如下:

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