Link Search Menu Expand Document

Events

The Graph instance is a node.js-like event emitter. As such, you can listen to its events in order to be able to react. This is particularly useful if you need to render your graph or maintain external indexes.

Documentation about node.js event emitters can be found here.

Note that the emitted payload is always an object with various keys.

nodeAdded

Emitted whenever a node is added to the graph.

Example

graph.on('nodeAdded', function({key}) {
  console.log(key);
})

graph.addNode('Thomas');
// Will print:
>>> 'Thomas'

Payload

  • key any: the added node.
  • attributes object: the node’s attributes.

edgeAdded

Emitted whenever an edge is added to the graph.

Example

graph.on('edgeAdded', function({key, source, target}) {
  console.log(key, source, target);
})

graph.addEdgeWithKey('T->R', 'Thomas', 'Richard');
// Will print:
>>> 'T->R', 'Thomas', 'Richard'

Payload

  • key any: the added edge.
  • source any: the added edge’s source.
  • target any: the added edge’s target.
  • attributes object: the edge’s attributes.
  • undirected boolean: whether the edge is undirected.

nodeDropped

Emitted whenever a node is dropped from the graph.

Example

graph.on('nodeDropped', function({key}) {
  console.log(key);
})

graph.dropNode('Thomas');
// Will print:
>>> 'Thomas'

Payload

  • key any: the dropped node’s key.
  • attributes object: the node’s attributes.

edgeDropped

Emitted whenever an edge is dropped from the graph.

Example

graph.on('edgeDropped', function({key, source, target}) {
  console.log(key, source, target);
})

graph.addEdgeWithKey('T->R', 'Thomas', 'Richard');
graph.dropEdge('T->R');
// Will print:
>>> 'T->R', 'Thomas', 'Richard'

Payload

  • key any: the dropped edge’s key.
  • source any: the dropped edge’s source.
  • target any: the dropped edge’s target.
  • attributes object: the edge’s attributes.
  • undirected boolean: whether the edge is undirected.

cleared

Emitted whenever the graph is cleared when using the #.clear method. Note that when using this method, the nodeDropped & the edgeDropped events won’t be emitted.

Example

graph.on('cleared', function() {
  console.log(graph.order, graph.size);
});

graph.clear();
// Will print:
>>> 0, 0

edgesCleared

Emitted whenever the graph is cleared of its edges when using the #.clearEdges method. Note that when using this method the edgeDropped events won’t be emitted.

Example

graph.on('edgesCleared', function() {
  console.log(graph.order, graph.size);
});

graph.clearEdges();
// Will print:
>>> 45, 0

attributesUpdated

Emitted whenever the attributes of the graph are updated.

Example

graph.on('attributesUpdated', function({type}) {
  console.log(type);
});

graph.setAttribute('name', 'My Beautiful Graph');
// Will print:
'set'

Payload

  • type string: type of the update, one of set, remove, replace, merge or update.
  • attributes object: the graph’s attributes.
  • name [string]: the name of the edited attributes if type is set or remove.
  • data [object]: merged data in case the type is merge.

nodeAttributesUpdated

Emitted whenever the attributes of the node are updated.

Example

graph.on('nodeAttributesUpdated', function({key, type}) {
  console.log(key, type);
});

graph.setNodeAttribute('Thomas', 'age', 54);
// Will print:
'Thomas', 'set'

Payload

  • type string: type of the update, one of set, remove, replace, merge or update.
  • key string: the affected node’s key.
  • attributes object: the node’s attributes.
  • name [string]: the name of the edited attributes if type is set or remove.
  • data [object]: merged data in case the type is merge.

edgeAttributesUpdated

Emitted whenever the attributes of the edge are updated.

Example

graph.on('edgeAttributesUpdated', function({key, type}) {
  console.log(key, type);
});

graph.setEdgeAttribute('T->R', 'type', 'KNOWS');
// Will print:
'Thomas', 'set'

Payload

  • type string: type of the update, one of set, remove, replace, merge or update.
  • key string: the affected edge’s key.
  • attributes object: the edge’s attributes.
  • name [string]: the name of the edited attributes if type is set or remove.
  • data [object]: merged data in case the type is merge.

eachNodeAttributesUpdated

Emitted whenever the #.updateEachNodeAttributes is called.

Payload

  • hints [object]: hints (only if they were provided by user, else null):
    • attributes [array]: the list of updated attribute names.

eachEdgeAttributesUpdated

Emitted whenever the #.updateEachEdgeAttributes is called.

Payload

  • hints [object]: hints (only if they were provided by user, else null):
    • attributes [array]: the list of updated attribute names.