You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
50 lines
1.2 KiB
|
2 months ago
|
import type { EdgeData, NodeData } from "@antv/g6";
|
||
|
|
import type { Device, MyNodeData } from "@/types/device";
|
||
|
|
|
||
|
|
export class GenerateGraphData {
|
||
|
|
private readonly devices: Device[] = [];
|
||
|
|
private nodes: MyNodeData[] = [];
|
||
|
|
private edges: EdgeData[] = [];
|
||
|
|
|
||
|
|
constructor(devices: Device[]) {
|
||
|
|
this.devices = devices;
|
||
|
|
this.generateNodeData()
|
||
|
|
this.generateEdgeData();
|
||
|
|
}
|
||
|
|
|
||
|
|
private generateNodeData() {
|
||
|
|
for (const device of this.devices) {
|
||
|
|
this.nodes.push({
|
||
|
|
id: String(device.name),
|
||
|
|
label: device.name,
|
||
|
|
data: Object.assign(device, {
|
||
|
|
// typeString: DeviceType[device.type],
|
||
|
|
}),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private generateEdgeData() {
|
||
|
|
for (const node of this.nodes) {
|
||
|
|
const customData: Device = node.data
|
||
|
|
const findParentNode: Device = this.nodes.some((n: MyNodeData) => n.id === String(customData.parentName));
|
||
|
|
|
||
|
|
if (findParentNode) {
|
||
|
|
this.edges.push({
|
||
|
|
source: String(customData.parentName),
|
||
|
|
target: String(node.id),
|
||
|
|
data: customData,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
getNodes(): NodeData[] {
|
||
|
|
return this.nodes as NodeData[];
|
||
|
|
}
|
||
|
|
|
||
|
|
getEdges(): EdgeData[] {
|
||
|
|
return this.edges;
|
||
|
|
}
|
||
|
|
}
|