diff --git a/src/common/graph/graph.ts b/src/common/graph/graph.ts index 49e147de574996b2d376d28cfc7c225c28aa8742..855cfe14c89585ef6aa66a858aa256f0fcf67a96 100644 --- a/src/common/graph/graph.ts +++ b/src/common/graph/graph.ts @@ -44,6 +44,10 @@ export class Graph private idToNode: Map<number, Node>; private idToLink: Map<number, Link>; + private nextNodeId = 0; + private nextLinkId = 0; + private nextObjectGroupId = 0; + /** * Creates a new Graph object. * Make sure the nodes and links are connected to the correct objects before calling this method! @@ -71,20 +75,31 @@ export class Graph }); this.connectElementsToGraph(); + this.updateNodeData(); + this.initializeIdGeneration(); } protected reset() { this.nodes = []; this.links = []; + this.objectGroups = []; this.nameToObjectGroup = new Map<string, NodeType>(); this.idToNode = new Map<number, Node>(); this.idToLink = new Map<number, Link>(); } + private initializeIdGeneration() { + this.nextNodeId = Math.max(...this.nodes.map((node) => node.id)) + 1; + this.nextLinkId = Math.max(...this.links.map((link) => link.id)) + 1; + this.nextObjectGroupId = Math.max( + ...this.objectGroups.map((group) => group.id) + ); + } + /** * Sets the correct graph object for all the graph elements in data. */ - connectElementsToGraph() { + private connectElementsToGraph() { this.nodes.forEach((n) => (n.graph = this)); this.links.forEach((l) => { l.graph = this; @@ -158,6 +173,11 @@ export class Graph * Creates a 'neighbors' and 'links' array for each node object. */ private updateNodeData(): Link[] { + this.nodes.forEach((node) => { + node.links = []; + node.neighbors = []; + }); + this.links.forEach((link) => { const a = link.source; const b = link.target; @@ -178,7 +198,9 @@ export class Graph } private addNode(node: Node) { - node.id = this.nodes.length; + if (this.idToNode.has(node.id)) { + node.id = this.nextNodeId++; + } this.nodes.push(node); this.idToNode.set(node.id, node); } @@ -223,13 +245,18 @@ export class Graph } private addObjectGroup(group: NodeType) { - group.id = this.objectGroups.length; + // if (this.nameToObjectGroup.has(group.id)) { + // + // } + group.id = this.nextObjectGroupId++; this.objectGroups.push(group); this.nameToObjectGroup.set(group.name, group); // TODO: Replace with id } private addLink(link: Link) { - link.id = this.links.length; + if (this.idToLink.has(link.id)) { + link.id = this.nextLinkId++; + } this.links.push(link); this.idToLink.set(link.id, link); } diff --git a/src/display/display.tsx b/src/display/display.tsx index 740a0642cae60e63bff3153289a80c483f46c5bd..fa38740bd2196867d17d4d35a479bcd8a29949c9 100644 --- a/src/display/display.tsx +++ b/src/display/display.tsx @@ -88,6 +88,7 @@ class Display extends React.Component< handleNodeFilter(visibility: Map<string, boolean>) { const graph = this.graph.view(visibility); + graph.removeFloatingNodes(); this.setState({ graph: graph }); }