Attributes

Graph

#.getAttribute

Returns the desired graph's attribute or undefined if not found.

Example

graph.setAttribute('name', 'My Beautiful Graph');

const name = graph.getAttribute('name');

console.log(name);
>>> 'My Beautiful Graph'

Arguments

  • attribute string: name of the attribute to retrieve.

#.getAttributes

Returns the desired graph's attributes.

Example

graph.setAttribute('name', 'My Beautiful Graph');
graph.setAttribute('color', 'blue');

graph.getAttributes();
>>> {
  name: 'My Beautiful Graph',
  color: 'blue'
}

#.hasAttribute

Returns whether the desired graph's attribute is set.

Example

graph.setAttribute('name', 'My Beautiful Graph');

graph.hasAttribute('name');
>>> true

graph.hasNodeAttribute('color');
>>> false

#.setAttribute

Set the attribute of the graph to the given value.

Example

graph.setAttribute('name', 'My Beautiful Graph');

graph.getAttribute('name');
>>> 'My Beautiful Graph'

Arguments

  • attribute string: name of the attribute to set.
  • value any: value to set.

#.updateAttribute

Update the attribute of the graph using the provided function.

This method is very useful when performing tasks such as incrementing an attribute so you don't have to first fetch the former value to compute the next one.

Note that if the attribute is not yet setted, the passed value will be undefined.

Example

graph.setAttribute('relevance', 10);

graph.updateAttribute('relevance', x => x + 1);

graph.getAttribute('relevance');
>>> 11

Arguments

  • attribute string: name of the attribute to update.
  • updater function: function used to perform the update.

#.removeAttribute

Remove the given graph's attribute altogether.

Example

graph.setAttribute('name', 'My Beautiful Graph');

graph.removeAttribute('name');

graph.hasAttribute('name');
>>> false

#.replaceAttributes

Completely replace one graph's attributes by the provided object.

Example

graph.setAttribute('name', 'My Beautiful Graph');

graph.replaceAttributes({
  name: 'My Different Graph',
  color: 'blue'
});

Arguments

  • attributes object: the new attributes.

#.mergeAttributes

Merge the current attributes of the graph with the provided object.

Example

graph.setAttribute('name', 'My Beautiful Graph');

graph.mergeAttributes({
  name: 'My Different Graph',
  color: 'blue'
});

Arguments

  • data object: data to merge.

Nodes

#.getNodeAttribute

Returns the desired node's attribute or undefined if not found..

Example

graph.addNode('Martha', {age: 34});

const age = graph.getNodeAttribute('Martha', 'age');

console.log(age);
>>> 34

Arguments

  • node any: the target node.
  • attribute string: name of the attribute to retrieve.

#.getNodeAttributes

Returns the desired node's attributes.

Example

graph.addNode('Martha', {age: 34, eyes: 'blue'});

graph.getNodeAttributes('Martha');
>>> {
  age: 34,
  eyes: 'blue'
}

#.hasNodeAttribute

Returns whether the desired node's attribute is set.

Example

graph.addNode('Martha', {eyes: 'blue'});

graph.hasNodeAttribute('Martha', 'eyes');
>>> true

graph.hasNodeAttribute('Martha', 'age');
>>> false

#.setNodeAttribute

Set the attribute of a node to the given value.

Example

graph.addNode('Martha', {age: 36, eyes: 'blue'});

graph.setNodeAttribute('Martha', 'age', 34);

Arguments

  • node any: the node to update.
  • attribute string: name of the attribute to set.
  • value any: value to set.

#.updateNodeAttribute

Update the attribute of a node using the provided function.

This method is very useful when performing tasks such as incrementing an attribute so you don't have to first fetch the former value to compute the next one.

Note that if the attribute is not yet setted, the passed value will be undefined.

Example

graph.addNode('Martha', {occurrences: 1});

graph.updateNodeAttribute('Martha', 'occurrences', n => n + 1);

Arguments

  • node any: the node to update.
  • attribute string: name of the attribute to update.
  • updater function: function used to perform the update.

#.removeNodeAttribute

Remove the given node's attribute altogether.

Example

graph.addNode('Martha', {age: 34});

graph.removeNodeAttribute('Martha', 'age');

graph.hasNodeAttribute('Martha', 'age');
>>> false

#.replaceNodeAttributes

Completely replace one node's attributes by the provided object.

Example

graph.addNode('Martha', {age: 36, eyes: 'blue'});

graph.replaceNodeAttributes('Martha', {
  age: 34,
  eyes: 'green'
});

Arguments

  • node any: the node to update.
  • attributes object: the new attributes.

#.mergeNodeAttributes

Merge the current attributes of a node with the provided object.

Example

graph.addNode('Martha', {age: 36, eyes: 'blue'});

graph.mergeNodeAttributes('Martha', {age: 34, hair: 'brown'});

Arguments

  • node any: the node to update.
  • data object: data to merge.

Performance tip: while this method is convenient, you'll be faster by using the #.setNodeAttribute method etc. in most cases and you'll enable renderers reacting to the graph's changes to act more efficiently, for instance.

Edges

#.getEdgeAttribute

Returns the desired edge's attribute or undefined if not found.

Example

graph.addNode('Martha');
graph.addNode('Catherine');
const edge = graph.addEdge('Martha', 'Catherine', {type: 'KNOWS'});

// Using the edge's key:
graph.getEdgeAttribute(edge, 'type');
>>> 'KNOWS'

// Using the edge's source & target:
graph.getEdgeAttribute('Martha', 'Catherine', 'type');
>>> 'KNOWS'

Arguments

  1. Using the key:

    • edge any: the target edge.
    • attribute string: name of the attribute to retrieve.
  2. Using the source & target:

    • source any: source of the edge.
    • target any: target of the edge.
    • attribute string: name of the attribute to retrieve.

#.getEdgeAttributes

Returns the desired edge's attributes.

Example

graph.addNode('Martha');
graph.addNode('Catherine');
const edge = graph.addEdge('Martha', 'Catherine', {type: 'KNOWS', weight: 2});

// Using the edge's key:
const attributes = graph.getEdgeAttributes(edge);

// Using the edge's source & target:
graph.getEdgeAttributes('Martha', 'Catherine');
>>> {
  type: 'KNOWS',
  weight: 2
}

Arguments

  1. Using the key:

    • edge any: the target edge.
  2. Using the source & target:

    • source any: source of the edge.
    • target any: target of the edge.

#.hasEdgeAttribute

Returns whether the desired edge's attribute is set.

Example

graph.addNode('Martha');
graph.addNode('Catherine');
const edge = graph.addEdge('Martha', 'Catherine', {type: 'KNOWS'});

// Using the edge's key:
const type = graph.hasEdgeAttribute(edge, 'type');
>>> true

// Using the edge's source & target:
graph.hasEdgeAttribute('Martha', 'Catherine', 'type');
>>> 'KNOWS'

Arguments

  1. Using the key:

    • edge any: the target edge.
    • attribute string: name of the attribute to poll.
  2. Using the source & target:

    • source any: source of the edge.
    • target any: target of the edge.
    • attribute string: name of the attribute to poll.

#.setEdgeAttribute

Set the attribute of an edge to the given value.

Example

graph.addNode('Martha');
graph.addNode('Jack');
const edge = graph.addEdge('Martha', 'Jack', {type: 'KNOWS'});

// Using the edge's key:
graph.setEdgeAttribute(edge, 'type', 'LIKES');

// Using the edge's source & target:
graph.setEdgeAttribute('Martha', 'Jack', 'type', 'LIKES');

Arguments

  1. Using the key:

    • edge any: the edge to update.
    • attribute string: name of the attribute to set.
    • value any: value to set.
  2. Using the source & target:

    • source any: source of the edge.
    • target any: target of the edge.
    • attribute string: name of the attribute to set.
    • value any: value to set.

#.updateEdgeAttribute

Update the attribute of an edge using the provided function.

This method is very useful when performing tasks such as incrementing an attribute so you don't have to first fetch the former value to compute the next one.

Note that if the attribute is not yet setted, the passed value will be undefined.

Example

graph.addNode('Martha');
graph.addNode('Jack');
const edge = graph.addEdge('Martha', 'Jack', {weight: 1});

// Using the edge's key:
graph.updateEdgeAttribute(edge, 'weight', n => n + 1);

// Using the edge's source & target:
graph.updateEdgeAttribute('Martha', 'Jack', 'weight', n => n + 1);

Arguments

  1. Using the key:

    • edge any: the edge to update.
    • attribute string: name of the attribute to update.
    • updater function: function used to perform the update.
  2. Using the source & target:

    • source any: source of the edge.
    • target any: target of the edge.
    • attribute string: name of the attribute to update.
    • updater function: function used to perform the update.

#.removeEdgeAttribute

Remove the given edge's attribute altogether.

Example

graph.addNode('Martha');
graph.addNode('Jack');
const edge = graph.addEdge('Martha', 'Jack', {type: 'KNOWS'});

// Using the edge's key:
graph.removeEdgeAttribute(edge, 'type');

// Using the edge's source & target:
graph.removeEdgeAttribute('Martha', 'Jack', 'type');

graph.hasEdgeAttribute(edge, 'type');
>>> false

Arguments

  1. Using the key:

    • edge any: the edge to update.
    • attribute string: name of the attribute to set.
  2. Using the source & target:

    • source any: source of the edge.
    • target any: target of the edge.
    • attribute string: name of the attribute to set.

#.replaceEdgeAttributes

Completely replace one edge's attributes by the provided object.

Example

graph.addNode('Martha');
graph.addNode('Jack');
const edge = graph.addEdge('Martha', 'Jack', {type: 'KNOWS', weight: 1});

// Using the edge's key:
graph.replaceEdgeAttributes(edge, {type: 'LIKES', weight: 3});

// Using the edge's source & target:
graph.replaceEdgeAttributes('Martha', 'Jack', {type: 'LIKES', weight: 3}));

Arguments

  1. Using the key:

    • edge any: the edge to update.
    • attributes object: the new attributes.
  2. Using the source & target:

    • source any: source of the edge.
    • target any: target of the edge.
    • attributes object: the new attributes.

#.mergeEdgeAttributes

Merge the current attributes of an edge with the provided object.

Example

graph.addNode('Martha');
graph.addNode('Jack');
const edge = graph.addEdge('Martha', 'Jack', {type: 'KNOWS'});

// Using the edge's key:
graph.mergeEdgeAttributes(edge, {type: 'LIKES', weight: 3});

// Using the edge's source & target:
graph.mergeEdgeAttributes('Martha', 'Jack', {type: 'LIKES', weight: 3}));

Arguments

  1. Using the key:

    • edge any: the edge to update.
    • data object: data to merge.
  2. Using the source & target:

    • source any: source of the edge.
    • target any: target of the edge.
    • data object: data to merge.

Performance tip: while this method is convenient, you'll be faster by using the #.setEdgeAttribute method etc. in most cases and you'll enable renderers reacting to the graph's changes to act more efficiently, for instance.