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 trigger:
>>> '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 trigger:
>>> '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 trigger:
>>> 'Thomas'

Payload

  • key any: the added node.
  • 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 trigger:
>>> 'T->R', 'Thomas', 'Richard'

Payload

  • edge 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.

cleared

Emitted whenever the graph is cleared when using the #.clear or the #.dropNodes (without arguments) method. Note that when using this method, the nodeDropped & the edgeDropped won't be emitted.

Example

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

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

attributesUpdated

Emitted whenever the attributes of the graph are updated.

Example

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

graph.setAttribute('name', 'My Beautiful Graph');
// Will trigger:
'name', 'set', {value: 'My Beautiful Graph'}

Payload

  • type string: type of the update, one of set, replace or merge.
  • meta object: additional information related to the update.

    • set

      • name string: edited attribute's name.
      • value string: new value.
    • remove

      • name string: removed attribute's name.
    • replace

      • before object: precedent attributes.
      • after object: current attributes.
    • merge

      • data object: Merged data.

nodeAttributesUpdated

Emitted whenever the attributes of the node are updated.

Example

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

graph.setNodeAttribute('Thomas', 'age', 54);
// Will trigger:
'Thomas', 'set', {key: 'age', value: 54}

Payload

  • key any: the updated node.
  • type string: type of the update, one of set, replace or merge.
  • meta object: additional information related to the update.

    • set

      • name string: edited attribute's name.
      • value string: new value.
    • remove

      • name string: removed attribute's name.
    • replace

      • before object: precedent attributes.
      • after object: current attributes.
    • merge

      • data object: Merged data.

edgeAttributesUpdated

Emitted whenever the attributes of the edge are updated.

Example

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

graph.setEdgeAttribute('T->R', 'type', 'KNOWS');
// Will trigger:
'Thomas', 'set', {key: 'type', value: 'KNOWS'}

Payload

  • key any: the updated edge.
  • type string: type of the update, one of set, replace or merge.
  • meta object: additional information related to the update.

    • set

      • name string: edited attribute's name.
      • value string: new value.
    • remove

      • name string: removed attribute's name.
    • replace

      • before object: precedent attributes.
      • after object: current attributes.
    • merge

      • data object: Merged data.