Optional options: GraphOptions<N, E>The options to initialize a graph. See GraphOptions.
const graph = new Graph({
// Optional, initial nodes.
nodes: [
// Each node has a unique ID.
{ id: 'A', foo: 1 },
{ id: 'B', foo: 1 },
],
// Optional, initial edges.
edges: [
{ id: 'C', source: 'B', target: 'B', weight: 1 },
],
// Optional, called with a GraphChangedEvent.
onChanged: (event) => {
console.log(event);
}
});
Private batchPrivate changesPrivate edgePrivate inPrivate nodeThis function is called with a GraphChangedEvent each time a graph change happened.
event.changes contains all the graph changes in order since last onChanged.
Private outPrivate treeAdd a single edge pointing from source to target into the graph.
graph.addNode({ id: 'NodeA' });
graph.addNode({ id: 'NodeB' });
graph.addEdge({ id: 'EdgeA', source: 'NodeA', target: 'NodeB' });
If source or target were not found in the current graph, it throws an Error.
Traverse the given tree data, add each node into the graph, then attach the tree structure.
graph.addTree({
id: 1,
children: [
{ id: 2 },
{ id: 3 },
],
}, 'Inheritance');
graph.getRoots('Inheritance'); // [1]
graph.getChildren(1, 'Inheritance'); // [2, 3]
graph.getAllNodes(); // [1, 2, 3]
graph.getAllEdges(); // []
Attach a new tree structure representing the hierarchy of all nodes in the graph.
Optional treeKey: stringA unique key of the tree structure. You can attach multiple tree structures with different keys.
const graph = new Graph({
nodes: [{ id: 1 }, { id: 2 }, { id: 3 }],
});
graph.attachTreeStructure('Inheritance');
graph.setParent(2, 1, 'Inheritance');
graph.setParent(3, 1, 'Inheritance');
graph.getRoots('Inheritance'); // [1]
graph.getChildren(1, 'Inheritance'); // [2,3]
Get the root nodes of an attached tree structure.
Consider a graph with the following tree structure attached:
Tree structure:
O 3
/ \ |
1 2 4
graph.getRoots() takes all nodes without a parent, therefore [0, 3] was returned.
Newly added nodes are also unparented. So they are counted as roots.
graph.addNode({ id: 5 });
graph.getRoots(); // [0, 3, 5]
Here is how the tree structure looks like:
Tree structure:
O 3 5
/ \ |
1 2 4
By setting a parent, a root node no more be a root.
graph.setParent(5, 2);
graph.getRoots(); // [0, 3]
The tree structure now becomes:
Tree structure:
O 3
/ \ |
1 2 4
|
5
Removing a node forces its children to be unparented, or roots.
graph.removeNode(0);
graph.getRoots(); // [1, 2, 3]
You might draw the the structure as follow:
Tree structure:
1 2 3
| |
5 4
Optional treeKey: stringBatch several graph changes into one.
Make several changes, but dispatch only one ChangedEvent at the end of batch:
graph.batch(() => {
graph.addNodes([]);
graph.addEdges([]);
});
Batches can be nested. Only the outermost batch will dispatch a ChangedEvent:
graph.batch(() => {
graph.addNodes([]);
graph.batch(() => {
graph.addEdges([]);
});
});
Private checkPrivate checkPrivate checkPrivate commitPrivate doPrivate doPrivate doPrivate doMerge ordered atomic changes to a single GraphDiff.
Generated using TypeDoc
Create a new Graph instance.