4 changed files with 139 additions and 146 deletions
@ -0,0 +1,111 @@
@@ -0,0 +1,111 @@
|
||||
import type { EdgeData, NodeData } from "@antv/g6"; |
||||
import { type Device, DeviceType,VIRTUAL_NODE } from "./index"; |
||||
|
||||
export default class GenerateGraphData { |
||||
private readonly devices: Device[] = []; |
||||
private nodes: NodeData[] = []; |
||||
private edges: EdgeData[] = []; |
||||
|
||||
constructor(devices: Device[]) { |
||||
this.devices = devices; |
||||
this.generateNodeData() |
||||
this.generateEdgeData(); |
||||
} |
||||
|
||||
private generateNodeData() { |
||||
for (const device of this.devices) { |
||||
const deviceChild = this.devices.filter((item: Device) => device.id === item.parentId) |
||||
const isNeedPoint = device.type === DeviceType.Ems || device.type === DeviceType['Em-Measure'] || device.type === DeviceType.Em |
||||
this.nodes.push({ |
||||
id: String(device.id), |
||||
type: 'user-card-node', |
||||
label: device.name, |
||||
data: device, |
||||
style: Object.assign( |
||||
{ |
||||
// labelText: device.name,
|
||||
opacity: .3, |
||||
deviceName: (d: any) => d.data.name, |
||||
}) |
||||
}) |
||||
|
||||
if (!!deviceChild.length) { |
||||
// 在每个有子节点的设备下插入一个虚拟节点
|
||||
this.nodes.push({ |
||||
id: `${device.id}-virtual`, |
||||
label: '', |
||||
type: '', |
||||
style: { opacity: 0 }, |
||||
size: 0, |
||||
anchorPoints: [], |
||||
data: { |
||||
type: VIRTUAL_NODE, |
||||
parentId: String(device.id), |
||||
depth: device.depth + 1, |
||||
}, |
||||
}) |
||||
|
||||
} |
||||
} |
||||
} |
||||
|
||||
private generateEdgeData() { |
||||
for (const node of this.nodes) { |
||||
const customData: Device = node.data |
||||
const virtualId = `${customData.parentId}-virtual`; |
||||
const findParentNode: Device = this.nodes.some((n: NodeData) => n.id === String(customData.parentId)); |
||||
|
||||
if (this.isVirtualNode(customData.type)) { |
||||
this.edges.push({ |
||||
source: customData.parentId, |
||||
target: String(node.id), |
||||
type: 'my-line-edge', |
||||
data: customData, |
||||
}) |
||||
continue; |
||||
} |
||||
//
|
||||
|
||||
if (findParentNode) { |
||||
if (this.isEms(customData.type)) continue; |
||||
if (this.isEm(customData.type)) { |
||||
// 如果是 电表 类型的节点 target 指向父节点Ems
|
||||
this.edges.push({ |
||||
target: String(customData.parentId), |
||||
source: String(node.id), |
||||
type: 'em-line-edge', |
||||
data: customData, |
||||
}) |
||||
continue; |
||||
} |
||||
|
||||
this.edges.push({ |
||||
source: virtualId, |
||||
target: String(node.id), |
||||
type: 'my-line-edge', |
||||
data: customData, |
||||
}) |
||||
} |
||||
} |
||||
} |
||||
|
||||
isEm(type: number): boolean { |
||||
return type == DeviceType["Em-Measure"] || type == DeviceType.Em |
||||
} |
||||
|
||||
isEms(type: number): boolean { |
||||
return type == DeviceType.Ems |
||||
} |
||||
|
||||
isVirtualNode(type: number | string): boolean { |
||||
return type === VIRTUAL_NODE; |
||||
} |
||||
|
||||
getNodes(): NodeData[] { |
||||
return this.nodes; |
||||
} |
||||
|
||||
getEdges(): EdgeData[] { |
||||
return this.edges; |
||||
} |
||||
} |
Loading…
Reference in new issue