@antv/graphlib / Exports / Graph
| Name | Type |
|---|---|
N |
extends PlainObject |
E |
extends PlainObject |
-
default↳
Graph
- addEdge
- addEdges
- getEdge
- getEdgeDetail
- mergeEdgeData
- removeEdge
- removeEdges
- updateEdgeData
- updateEdgeSource
- updateEdgeTarget
- addNode
- addNodes
- areNeighbors
- getDegree
- getNeighbors
- getNode
- getRelatedEdges
- hasEdge
- hasNode
- removeNode
- removeNodes
- updateNodeData
- batch
- bfs
- bfsTree
- checkEdgeExistence
- checkNodeExistence
- checkTreeExistence
- clone
- commit
- createView
- dfs
- dfsTree
- doAddEdge
- doAddNode
- doRemoveEdge
- doRemoveNode
- emit
- getAllEdges
- getAllNodes
- getAncestors
- getEvents
- getPredecessors
- getSuccessors
- hasTreeStructure
- mergeNodeData
- off
- on
- once
- reduceChanges
- toJSON
- updateEdgeDataProperty
- updateNodeDataProperty
• new Graph<N, E>(options?)
Create a new Graph instance.
| Name | Type |
|---|---|
N |
extends PlainObject |
E |
extends PlainObject |
| Name | Type | Description |
|---|---|---|
options? |
GraphOptions<N, E> |
The options to initialize a graph. See GraphOptions. ts 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); } }); |
EventEmitter.constructor
• Private batchCount: number = 0
• Private bothEdgesMap: Map<ID, Set<Edge<E>>>
• Private changes: GraphChange<N, E>[] = []
• Private edgeMap: Map<ID, Edge<E>>
• Private inEdgesMap: Map<ID, Set<Edge<E>>>
• Private nodeMap: Map<ID, Node<N>>
• onChanged: (event: GraphChangedEvent<N, E>) => void
▸ (event): void
This function is called with a GraphChangedEvent each time a graph change happened.
event.changes contains all the graph changes in order since last onChanged.
| Name | Type |
|---|---|
event |
GraphChangedEvent<N, E> |
void
• Private outEdgesMap: Map<ID, Set<Edge<E>>>
• Private treeIndices: TreeIndices<Node<N>>
▸ addEdge(edge): void
Add 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.
| Name | Type |
|---|---|
edge |
Edge<E> |
void
▸ addEdges(edges): void
Add all edges of the given iterable(an array, a set, etc.) into the graph.
| Name | Type |
|---|---|
edges |
Iterable<Edge<E>> |
void
▸ getEdge(id): Edge<E>
Get the edge data with given ID.
| Name | Type |
|---|---|
id |
ID |
Edge<E>
▸ getEdgeDetail(id): Object
Get the edge, the source node, and the target node by an edge ID.
| Name | Type |
|---|---|
id |
ID |
Object
| Name | Type |
|---|---|
edge |
Edge<E> |
source |
Node<N> |
target |
Node<N> |
▸ mergeEdgeData(id, patch): void
| Name | Type |
|---|---|
id |
ID |
patch |
Partial<E> |
void
▸ removeEdge(id): void
Remove a single edge of the given id.
| Name | Type |
|---|---|
id |
ID |
void
▸ removeEdges(idList): void
Remove edges whose id was included in the given id list.
| Name | Type |
|---|---|
idList |
ID[] |
void
▸ updateEdgeData(id, data): void
Update edge data. This will replace the whole data object.
updateEdgeData(id, data); // Works like `edge.data = data`| Name | Type |
|---|---|
id |
ID |
data |
E |
void
▸ updateEdgeData<P>(id, propertyName, value): void
Update a single property on the edge data.
To update multiple properties, you could batch several updates or use mergeNodeData.
updateEdgeData(id, key, value); // Works like `edge.data[key] = value`| Name | Type |
|---|---|
P |
extends string | number | symbol |
| Name | Type |
|---|---|
id |
ID |
propertyName |
P |
value |
E[P] |
void
▸ updateEdgeData(id, update): void
Update edge data by a update function.
updateEdgeData(id, oldData => newData);| Name | Type |
|---|---|
id |
ID |
update |
(data: E) => E |
void
▸ updateEdgeSource(id, source): void
Change the source of an edge. The source must be found in current graph.
| Name | Type |
|---|---|
id |
ID |
source |
ID |
void
▸ updateEdgeTarget(id, target): void
Change the target of an edge. The target must be found in current graph.
| Name | Type |
|---|---|
id |
ID |
target |
ID |
void
▸ addNode(node): void
Add a single node into the graph.
| Name | Type |
|---|---|
node |
Node<N> |
void
▸ addNodes(nodes): void
Add all nodes of the given array, or iterable, into the graph.
| Name | Type |
|---|---|
nodes |
Iterable<Node<N>> |
void
▸ areNeighbors(firstNodeId, secondNodeId): boolean
Tell if two nodes are neighbors.
| Name | Type |
|---|---|
firstNodeId |
ID |
secondNodeId |
ID |
boolean
▸ getDegree(id, direction?): number
Get the degree of the given node.
| Name | Type |
|---|---|
id |
ID |
direction? |
"in" | "out" | "both" |
number
▸ getNeighbors(id): Node<N>[]
Given a node ID, find its neighbors.
| Name | Type | Description |
|---|---|---|
id |
ID |
ID of the node |
Node<N>[]
▸ getNode(id): Node<N>
Get the node data with given ID.
| Name | Type |
|---|---|
id |
ID |
Node<N>
▸ getRelatedEdges(id, direction?): Edge<E>[]
Given a node ID, find all edges of the node.
| Name | Type | Description |
|---|---|---|
id |
ID |
ID of the node |
direction? |
"in" | "out" | "both" |
Edge direction, defaults to 'both'. |
Edge<E>[]
▸ hasEdge(id): boolean
Check if an edge exists in the graph.
| Name | Type |
|---|---|
id |
ID |
boolean
▸ hasNode(id): boolean
Check if a node exists in the graph.
| Name | Type |
|---|---|
id |
ID |
boolean
▸ removeNode(id): void
Remove a single node and its attached edges from the graph.
| Name | Type |
|---|---|
id |
ID |
void
▸ removeNodes(idList): void
Remove nodes and their attached edges from the graph.
| Name | Type |
|---|---|
idList |
ID[] |
void
▸ updateNodeData(id, data): void
Update node data. This will replace the whole data object.
updateNodeData(id, data); // Works like `node.data = data`| Name | Type |
|---|---|
id |
ID |
data |
N |
void
▸ updateNodeData<P>(id, propertyName, value): void
Update a single property on the node data.
To update multiple properties, you could batch several updates or use mergeNodeData.
updateNodeData(id, key, value); // Works like `node.data[key] = value`| Name | Type |
|---|---|
P |
extends string | number | symbol |
| Name | Type |
|---|---|
id |
ID |
propertyName |
P |
value |
N[P] |
void
▸ updateNodeData(id, update): void
Update node data by a update function.
updateNodeData(id, oldData => newData);| Name | Type |
|---|---|
id |
ID |
update |
(data: N) => N |
void
▸ addTree(tree, treeKey?): void
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(); // []| Name | Type |
|---|---|
tree |
TreeData<N> | TreeData<N>[] |
treeKey? |
string |
void
▸ attachTreeStructure(treeKey?): void
Attach a new tree structure representing the hierarchy of all nodes in the graph.
| Name | Type | Description |
|---|---|---|
treeKey? |
string |
A unique key of the tree structure. You can attach multiple tree structures with different keys. ts 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] |
void
▸ detachTreeStructure(treeKey?): void
Detach the tree structure of the given tree key from the graph.
graph.detachTreeStructure('Inheritance');
graph.getRoots('Inheritance'); // Error!| Name | Type |
|---|---|
treeKey? |
string |
void
▸ getChildren(id, treeKey?): Node<N>[]
Given a node ID and an optional tree key, get the children of the node in the specified tree structure.
| Name | Type |
|---|---|
id |
ID |
treeKey? |
string |
Node<N>[]
▸ getParent(id, treeKey?): null | Node<N>
Given a node ID and an optional tree key, get the parent of the node in the specified tree structure. If the given node is one of the tree roots, this returns null.
| Name | Type |
|---|---|
id |
ID |
treeKey? |
string |
null | Node<N>
▸ getRoots(treeKey?): Node<N>[]
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
| Name | Type |
|---|---|
treeKey? |
string |
Node<N>[]
▸ setParent(id, parent, treeKey?): void
Set node parent. If this operation causes a circle, it fails with an error.
| Name | Type | Description |
|---|---|---|
id |
ID |
ID of the child node. |
parent |
ID |
ID of the parent node. |
treeKey? |
string |
Which tree structure the relation is applied to. |
void
▸ batch(fn): void
Batch 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([]);
});
});| Name | Type |
|---|---|
fn |
() => void |
void
▸ bfs(id, fn, direction?): boolean
| Name | Type | Default value |
|---|---|---|
id |
ID |
undefined |
fn |
(node: Node<N>) => boolean | void |
undefined |
direction |
"in" | "out" | "both" |
'out' |
boolean
▸ bfsTree(id, fn, treeKey?): boolean
| Name | Type |
|---|---|
id |
ID |
fn |
(node: Node<N>) => boolean | void |
treeKey? |
string |
boolean
▸ Private checkEdgeExistence(id): void
| Name | Type |
|---|---|
id |
ID |
void
▸ Private checkNodeExistence(id): void
| Name | Type |
|---|---|
id |
ID |
void
▸ Private checkTreeExistence(treeKey): void
| Name | Type |
|---|---|
treeKey |
undefined | string |
void
▸ clone(): Graph<N, E>
Graph<N, E>
▸ Private commit(): void
Reset changes and dispatch a ChangedEvent.
void
▸ createView(options): GraphView<N, E>
| Name | Type |
|---|---|
options |
Omit<GraphViewOptions<N, E>, "graph"> |
GraphView<N, E>
▸ dfs(id, fn, direction?): boolean
| Name | Type | Default value |
|---|---|---|
id |
ID |
undefined |
fn |
(node: Node<N>) => boolean | void |
undefined |
direction |
"in" | "out" | "both" |
'out' |
boolean
▸ dfsTree(id, fn, treeKey?): boolean
| Name | Type |
|---|---|
id |
ID |
fn |
(node: Node<N>) => boolean | void |
treeKey? |
string |
boolean
▸ Private doAddEdge(edge): void
| Name | Type |
|---|---|
edge |
Edge<E> |
void
▸ Private doAddNode(node): void
| Name | Type |
|---|---|
node |
Node<N> |
void
▸ Private doRemoveEdge(id): void
| Name | Type |
|---|---|
id |
ID |
void
▸ Private doRemoveNode(id): void
| Name | Type |
|---|---|
id |
ID |
void
▸ emit(evt, ...args): void
触发一个事件
| Name | Type |
|---|---|
evt |
string |
...args |
any[] |
void
EventEmitter.emit
node_modules/_@antv_event-emitter@0.1.3@@antv/event-emitter/lib/index.d.ts:25
▸ getAllEdges(): Edge<E>[]
Get all edges in the graph as an array.
Edge<E>[]
▸ getAllNodes(): Node<N>[]
Get all nodes in the graph as an array.
Node<N>[]
▸ getAncestors(id, treeKey?): Node<N>[]
Returns an array of all the ancestor nodes, staring from the parent to the root.
| Name | Type |
|---|---|
id |
ID |
treeKey? |
string |
Node<N>[]
▸ getEvents(): Record<string, EventType[]>
Record<string, EventType[]>
EventEmitter.getEvents
node_modules/_@antv_event-emitter@0.1.3@@antv/event-emitter/lib/index.d.ts:32
▸ getPredecessors(id): Node<N>[]
Get all predecessors of the given node.
| Name | Type |
|---|---|
id |
ID |
Node<N>[]
▸ getSuccessors(id): Node<N>[]
Get all successors of the given node.
| Name | Type |
|---|---|
id |
ID |
Node<N>[]
▸ hasTreeStructure(treeKey): boolean
| Name | Type |
|---|---|
treeKey |
undefined | string |
boolean
▸ mergeNodeData(id, patch): void
Like Object.assign, merge all properties of path to the node data.
| Name | Type | Description |
|---|---|---|
id |
ID |
Node ID. |
patch |
Partial<N> |
A data object to merge. |
void
▸ off(evt?, callback?): Graph<N, E>
取消监听一个事件,或者一个channel
| Name | Type |
|---|---|
evt? |
string |
callback? |
Function |
Graph<N, E>
EventEmitter.off
node_modules/_@antv_event-emitter@0.1.3@@antv/event-emitter/lib/index.d.ts:31
▸ on(evt, callback, once?): Graph<N, E>
监听一个事件
| Name | Type |
|---|---|
evt |
string |
callback |
Function |
once? |
boolean |
Graph<N, E>
EventEmitter.on
node_modules/_@antv_event-emitter@0.1.3@@antv/event-emitter/lib/index.d.ts:13
▸ once(evt, callback): Graph<N, E>
监听一个事件一次
| Name | Type |
|---|---|
evt |
string |
callback |
Function |
Graph<N, E>
EventEmitter.once
node_modules/_@antv_event-emitter@0.1.3@@antv/event-emitter/lib/index.d.ts:19
▸ reduceChanges(changes): GraphChange<N, E>[]
Reduce the number of ordered graph changes by dropping or merging unnecessary changes.
For example, if we update a node and remove it in a batch:
graph.batch(() => {
graph.updateNodeData('A', 'foo', 2);
graph.removeNode('A');
});We get 2 atomic graph changes like
[
{ type: 'NodeDataUpdated', id: 'A', propertyName: 'foo', oldValue: 1, newValue: 2 },
{ type: 'NodeRemoved', value: { id: 'A', data: { foo: 2 } },
]Since node 'A' has been removed, we actually have no need to handle with NodeDataUpdated change.
reduceChanges() here helps us remove such changes.
| Name | Type |
|---|---|
changes |
GraphChange<N, E>[] |
GraphChange<N, E>[]
▸ toJSON(): string
string
▸ Private updateEdgeDataProperty<P>(id, propertyName, value): void
| Name | Type |
|---|---|
P |
extends string | number | symbol |
| Name | Type |
|---|---|
id |
ID |
propertyName |
P |
value |
E[P] |
void
▸ Private updateNodeDataProperty<P>(id, propertyName, value): void
| Name | Type |
|---|---|
P |
extends string | number | symbol |
| Name | Type |
|---|---|
id |
ID |
propertyName |
P |
value |
N[P] |
void