Skip to content

Commit 065337c

Browse files
authored
Fix the topology layout for there are multiple independent network relationships (#397)
1 parent 21fe455 commit 065337c

File tree

1 file changed

+21
-11
lines changed
  • src/views/dashboard/related/topology/components/utils

1 file changed

+21
-11
lines changed

src/views/dashboard/related/topology/components/utils/layout.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ function findMostFrequent(arr: Call[]) {
141141

142142
return maxItem;
143143
}
144-
export function computeLevels(calls: Call[], nodeList: Node[], levels: any[]) {
144+
export function computeLevels(calls: Call[], nodeList: Node[], arr: Node[][]) {
145+
const levels: Node[][] = [];
145146
const node = findMostFrequent(calls);
146-
const nodes = JSON.parse(JSON.stringify(nodeList)).sort((a: Node, b: Node) => {
147+
let nodes = JSON.parse(JSON.stringify(nodeList)).sort((a: Node, b: Node) => {
147148
if (a.name.toLowerCase() < b.name.toLowerCase()) {
148149
return -1;
149150
}
@@ -158,23 +159,23 @@ export function computeLevels(calls: Call[], nodeList: Node[], levels: any[]) {
158159
key = nodes.findIndex((n: Node) => n.id === node.id);
159160
}
160161
levels.push([nodes[key]]);
161-
nodes.splice(key, 1);
162+
nodes = nodes.filter((_: unknown, index: number) => index !== key);
162163
for (const level of levels) {
163164
const a = [];
164165
for (const l of level) {
165166
for (const n of calls) {
166167
if (n.target === l.id) {
167168
const i = nodes.findIndex((d: Node) => d.id === n.source);
168-
if (i > -1) {
169+
if (i > -1 && nodes[i]) {
169170
a.push(nodes[i]);
170-
nodes.splice(i, 1);
171+
nodes = nodes.filter((_: unknown, index: number) => index !== i);
171172
}
172173
}
173174
if (n.source === l.id) {
174175
const i = nodes.findIndex((d: Node) => d.id === n.target);
175-
if (i > -1) {
176+
if (i > -1 && nodes[i]) {
176177
a.push(nodes[i]);
177-
nodes.splice(i, 1);
178+
nodes = nodes.filter((_: unknown, index: number) => index !== i);
178179
}
179180
}
180181
}
@@ -183,13 +184,22 @@ export function computeLevels(calls: Call[], nodeList: Node[], levels: any[]) {
183184
levels.push(a);
184185
}
185186
}
187+
const list = levels.length > arr.length ? levels : arr;
188+
const subList = levels.length > arr.length ? arr : levels;
189+
arr = list.map((subArray: Node[], index: number) => {
190+
if (subList[index]) {
191+
return subArray.concat(subList[index]);
192+
} else {
193+
return subArray;
194+
}
195+
});
196+
186197
if (nodes.length) {
187198
const ids = nodes.map((d: Node) => d.id);
188199
const links = calls.filter((item: Call) => ids.includes(item.source) || ids.includes(item.target));
189-
const list = computeLevels(links, nodes, []);
190-
levels = list.map((subArrayA, index) => subArrayA.concat(levels[index]));
200+
arr = computeLevels(links, nodes, arr);
191201
}
192-
return levels;
202+
return arr;
193203
}
194204
export function changeNode(d: { x: number; y: number }, currentNode: Nullable<Node>, layout: any, radius: number) {
195205
if (!currentNode) {
@@ -229,7 +239,7 @@ export function changeNode(d: { x: number; y: number }, currentNode: Nullable<No
229239
}
230240
export function hierarchy(levels: Node[][], calls: Call[], radius: number) {
231241
// precompute level depth
232-
levels.forEach((l: Node[], i: number) => l.forEach((n: any) => n && (n.level = i)));
242+
levels.forEach((l: Node[], i: number) => l.forEach((n: Node) => n && (n.level = i)));
233243

234244
const nodes: Node[] = levels.reduce((a, x) => a.concat(x), []);
235245
// layout

0 commit comments

Comments
 (0)