Newer
Older
interface Link {
source: string;
target: string;
type?: string;
}
export interface LinkData {
source: NodeData;
target: NodeData;
type?: string;
}
export interface NodeData {
id: string;
name: string;
description?: string;
icon?: string;
banner?: string;
references?: string[];
neighbors: NodeData[];
links: LinkData[];
}
export interface Coordinate {
x: number;
y: number;
z: number;
}
public nodes: NodeData[];
public links: LinkData[];
private idToNode: Map<string, NodeData>;
constructor(nodes: NodeData[], links: Link[]) {
this.nodes = nodes;
this.idToNode = new Map<string, NodeData>();
nodes.forEach((node) => {
this.idToNode.set(node.id, node);
});
this.links = links.map((link) => {
return {
source: this.idToNode.get(link.source),
target: this.idToNode.get(link.target),
type: link.type,
};
});
this.resetNodeData();
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
this.updateNodeData();
}
resetNodeData() {
for (const node of this.nodes) {
node.neighbors = [];
node.links = [];
}
}
/**
* Updates the graph data structure to contain additional values.
* Creates a 'neighbors' and 'links' array for each node object.
*/
updateNodeData() {
this.resetNodeData();
this.links.forEach((link) => {
const a = link.source;
const b = link.target;
a.neighbors.push(b);
b.neighbors.push(a);
a.links.push(link);
b.links.push(link);
});
}
node(id: string): NodeData {
return this.idToNode.get(id);
}
/**
* Returns an array containing the different edge types of the graph.
* @returns {*[]}
*/
getLinkClasses(): string[] {
const linkClasses: string[] = [];
this.links.forEach((link) => linkClasses.push(link.type));
return [...new Set(linkClasses)].map((c) => String(c));
}
getNodeClasses(): string[] {
const nodeClasses: string[] = [];
this.nodes.forEach((node) => nodeClasses.push(node.type));
return [...new Set(nodeClasses)].map((c) => String(c));
}
view(
nodeTypes: Map<string, boolean>,
linkTypes: Map<string, boolean>
): Graph {
const links = this.links.filter((l) => linkTypes.get(l.type));
return new Graph(
this.nodes.filter((l) => nodeTypes.get(l.type)),
links.map((link) => {
return {
source: link.source.id,
target: link.target.id,
type: link.type,
};
})