vue3-network-graph 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +733 -728
- package/README.md +733 -728
- package/dist/index.d.ts +12 -0
- package/dist/index.mjs +1217 -847
- package/dist/index.umd.cjs +5 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,728 +1,733 @@
|
|
|
1
|
-
# vue3-network-graph 使用说明
|
|
2
|
-
|
|
3
|
-
基于 **Vue 3 + SVG** 的轻量级网络拓扑图组件,无第三方图表库依赖。支持节点拖拽、画布无限平移、滚轮缩放、连线流向动画、三种连线模式、同节点对多连线防重叠、连线文字沿线直立等能力。
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## 目录
|
|
8
|
-
|
|
9
|
-
- [名称](#名称)
|
|
10
|
-
- [描述](#描述)
|
|
11
|
-
- [使用方法](#使用方法)
|
|
12
|
-
- [使用例子](#使用例子)
|
|
13
|
-
- [全局配置属性说明](#全局配置属性说明)
|
|
14
|
-
- [节点属性说明](#节点属性说明)
|
|
15
|
-
- [连线属性说明](#连线属性说明)
|
|
16
|
-
- [调用方法](#调用方法)
|
|
17
|
-
- [事件说明](#事件说明)
|
|
18
|
-
- [交互操作](#交互操作)
|
|
19
|
-
|
|
20
|
-
---
|
|
21
|
-
|
|
22
|
-
## 名称
|
|
23
|
-
|
|
24
|
-
**vue3-network-graph** - Vue3 网络拓扑图组件
|
|
25
|
-
|
|
26
|
-
---
|
|
27
|
-
|
|
28
|
-
## 描述
|
|
29
|
-
|
|
30
|
-
一个纯 SVG 实现的轻量级网络拓扑图组件,提供以下核心能力:
|
|
31
|
-
|
|
32
|
-
### 核心功能
|
|
33
|
-
|
|
34
|
-
- **多种连线模式**:直线(straight)/ 折线(polyline)/ 曲线(curve)
|
|
35
|
-
- **智能多线处理**:同一对节点的多条连线自动防重叠,任何模式下都保持平行
|
|
36
|
-
- **连线文字直立**:`fromText` / `centerText` / `toText` 沿路径切向旋转,超过±90° 自动翻转保持可读
|
|
37
|
-
- **丰富的节点类型**:8 种图标节点 + 文本节点 + 纯边框节点
|
|
38
|
-
- **图片节点**:照片 / 头像 / 位图节点,按真实比例等比适配(整图完整显示或铺满裁剪,永不拉伸)
|
|
39
|
-
- **2D/3D 视图**:支持 2D 平面和 2.5D 卡片风格
|
|
40
|
-
- **流向动画**:虚线流光 + 粒子效果,可单独控制每条连线的动画开关
|
|
41
|
-
- **连线不被节点吃掉**:节点分层绘制(底板在下、图标/标签在上),连线穿过节点或节点重叠时依然连续可见;选中链路自动提升到最上层
|
|
42
|
-
|
|
43
|
-
### 交互能力
|
|
44
|
-
|
|
45
|
-
- **节点操作**:拖拽移动、拖拽调整尺寸、多选整体拖动
|
|
46
|
-
- **画布操作**:无限平移(空白左键/中键)、滚轮缩放(0.2x–5x)
|
|
47
|
-
- **选择模式**:单击选中、Ctrl+ 点击多选、Ctrl+ 左键框选
|
|
48
|
-
- **连线创建**:从节点手柄拖拽到目标节点自动连线
|
|
49
|
-
- **图层管理**:Z 轴排序、右键菜单、快捷键操作(需启用 `enableZOrder`)
|
|
50
|
-
|
|
51
|
-
### 性能优化
|
|
52
|
-
|
|
53
|
-
- **自动降级**:装饰动效根据规模自动关闭(连线>200 关流光、>500 关发光、节点>300 关呼吸)
|
|
54
|
-
- **大图性能**:千级节点可流畅平移/拖拽,密集图形下节点优先响应点击
|
|
55
|
-
|
|
56
|
-
---
|
|
57
|
-
|
|
58
|
-
## 使用方法
|
|
59
|
-
|
|
60
|
-
### 1. 安装
|
|
61
|
-
|
|
62
|
-
```bash
|
|
63
|
-
npm install vue3-network-graph
|
|
64
|
-
# 或:pnpm add vue3-network-graph | yarn add vue3-network-graph
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
### 2. 引入组件
|
|
68
|
-
|
|
69
|
-
```js
|
|
70
|
-
// main.js
|
|
71
|
-
import { createApp } from "vue";
|
|
72
|
-
import { Topology } from "vue3-network-graph";
|
|
73
|
-
import "vue3-network-graph/style.css";
|
|
74
|
-
|
|
75
|
-
const app = createApp(App);
|
|
76
|
-
app.component("Topology", Topology);
|
|
77
|
-
app.mount("#app");
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
或局部引入:
|
|
81
|
-
|
|
82
|
-
```vue
|
|
83
|
-
<script setup>
|
|
84
|
-
import { Topology } from "vue3-network-graph";
|
|
85
|
-
import "vue3-network-graph/style.css";
|
|
86
|
-
</script>
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### 3. 基本使用
|
|
90
|
-
|
|
91
|
-
```vue
|
|
92
|
-
<template>
|
|
93
|
-
<Topology
|
|
94
|
-
ref="topoRef"
|
|
95
|
-
:editable="true"
|
|
96
|
-
v-model:nodes="myNodes"
|
|
97
|
-
v-model:edges="myEdges"
|
|
98
|
-
:config="config"
|
|
99
|
-
@node-click="handleNodeClick"
|
|
100
|
-
@selection-change="handleSelectionChange"
|
|
101
|
-
/>
|
|
102
|
-
</template>
|
|
103
|
-
|
|
104
|
-
<script setup>
|
|
105
|
-
import { ref } from "vue";
|
|
106
|
-
import { Topology } from "vue3-network-graph";
|
|
107
|
-
|
|
108
|
-
const myNodes = ref([
|
|
109
|
-
{ id: "node1", label: "节点 1", type: "core", state: "normal", x: 0, y: 0 },
|
|
110
|
-
{
|
|
111
|
-
id: "node2",
|
|
112
|
-
label: "节点 2",
|
|
113
|
-
type: "server",
|
|
114
|
-
state: "normal",
|
|
115
|
-
x: 200,
|
|
116
|
-
y: 100,
|
|
117
|
-
},
|
|
118
|
-
]);
|
|
119
|
-
|
|
120
|
-
const myEdges = ref([
|
|
121
|
-
{ id: "edge1", from: "node1", to: "node2", centerText: "连接" },
|
|
122
|
-
]);
|
|
123
|
-
|
|
124
|
-
const config = ref({
|
|
125
|
-
showGrid: true,
|
|
126
|
-
nodeDraggable: true,
|
|
127
|
-
nodeResizable: true,
|
|
128
|
-
});
|
|
129
|
-
</script>
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
---
|
|
133
|
-
|
|
134
|
-
## 使用例子
|
|
135
|
-
|
|
136
|
-
### 完整示例
|
|
137
|
-
|
|
138
|
-
```vue
|
|
139
|
-
<template>
|
|
140
|
-
<div class="topology-container">
|
|
141
|
-
<Topology
|
|
142
|
-
ref="topoRef"
|
|
143
|
-
:editable="true"
|
|
144
|
-
v-model:nodes="nodes"
|
|
145
|
-
v-model:edges="edges"
|
|
146
|
-
v-model:line-mode="lineMode"
|
|
147
|
-
v-model:view-mode="viewMode"
|
|
148
|
-
:groups="groups"
|
|
149
|
-
:config="config"
|
|
150
|
-
@node-click="onNodeClick"
|
|
151
|
-
@edge-select="onEdgeSelect"
|
|
152
|
-
@selection-change="onSelectionChange"
|
|
153
|
-
@data-change="onDataChange"
|
|
154
|
-
@edge-create="onEdgeCreate"
|
|
155
|
-
>
|
|
156
|
-
<!-- 自定义节点图标插槽 -->
|
|
157
|
-
<template #node-icon="{ node, color, viewMode }">
|
|
158
|
-
<circle r="16" :fill="color" opacity="0.9" />
|
|
159
|
-
</template>
|
|
160
|
-
</Topology>
|
|
161
|
-
</div>
|
|
162
|
-
</template>
|
|
163
|
-
|
|
164
|
-
<script setup>
|
|
165
|
-
import { ref, onMounted } from "vue";
|
|
166
|
-
import { Topology } from "vue3-network-graph";
|
|
167
|
-
import "vue3-network-graph/style.css";
|
|
168
|
-
|
|
169
|
-
// 节点数据
|
|
170
|
-
const nodes = ref([
|
|
171
|
-
{
|
|
172
|
-
id: "core",
|
|
173
|
-
label: "核心交换机",
|
|
174
|
-
nodeType: "core",
|
|
175
|
-
state: "normal",
|
|
176
|
-
x: 0,
|
|
177
|
-
y: 0,
|
|
178
|
-
width: 80,
|
|
179
|
-
height: 80,
|
|
180
|
-
},
|
|
181
|
-
{
|
|
182
|
-
id: "server1",
|
|
183
|
-
label: "Web 服务器",
|
|
184
|
-
nodeType: "server",
|
|
185
|
-
state: "normal",
|
|
186
|
-
x: 300,
|
|
187
|
-
y: -150,
|
|
188
|
-
},
|
|
189
|
-
{
|
|
190
|
-
id: "db1",
|
|
191
|
-
label: "数据库",
|
|
192
|
-
nodeType: "database",
|
|
193
|
-
state: "idle",
|
|
194
|
-
x: 300,
|
|
195
|
-
y: 150,
|
|
196
|
-
},
|
|
197
|
-
// 纯边框节点示例
|
|
198
|
-
{
|
|
199
|
-
id: "border-box",
|
|
200
|
-
label: "",
|
|
201
|
-
nodeType: "border-only",
|
|
202
|
-
state: "none",
|
|
203
|
-
x: -400,
|
|
204
|
-
y: -200,
|
|
205
|
-
width: 250,
|
|
206
|
-
height: 150,
|
|
207
|
-
borderWidth: 2,
|
|
208
|
-
borderColor: "#00f0ff",
|
|
209
|
-
},
|
|
210
|
-
]);
|
|
211
|
-
|
|
212
|
-
// 连线数据
|
|
213
|
-
const edges = ref([
|
|
214
|
-
{ id: "e1", from: "core", to: "server1", speed: 1.5, endMarker: "arrow" },
|
|
215
|
-
{
|
|
216
|
-
id: "e2",
|
|
217
|
-
from: "core",
|
|
218
|
-
to: "db1",
|
|
219
|
-
speed: 2,
|
|
220
|
-
centerText: "查询流量",
|
|
221
|
-
endMarker: "arrow",
|
|
222
|
-
},
|
|
223
|
-
]);
|
|
224
|
-
|
|
225
|
-
// 分组示例
|
|
226
|
-
const groups = ref([
|
|
227
|
-
{
|
|
228
|
-
id: "g1",
|
|
229
|
-
label: "应用区",
|
|
230
|
-
color: "#409eff",
|
|
231
|
-
nodeIds: ["server1", "db1"],
|
|
232
|
-
padding: 20,
|
|
233
|
-
},
|
|
234
|
-
]);
|
|
235
|
-
|
|
236
|
-
// 连线模式和视图模式
|
|
237
|
-
const lineMode = ref("curve"); // straight | polyline | curve
|
|
238
|
-
const viewMode = ref("2d"); // 2d | 3d
|
|
239
|
-
|
|
240
|
-
// 配置项
|
|
241
|
-
const config = ref({
|
|
242
|
-
selectedBorderColor: "#ffd400",
|
|
243
|
-
selectedLineColor: "#ffd400",
|
|
244
|
-
nodeDraggable: true,
|
|
245
|
-
nodeResizable: true,
|
|
246
|
-
enableZoom: true,
|
|
247
|
-
enablePan: true,
|
|
248
|
-
enableMarquee: true,
|
|
249
|
-
showGrid: true,
|
|
250
|
-
gridColor: "#1a2240",
|
|
251
|
-
gridStep: 40,
|
|
252
|
-
gridOpacity: 0.5,
|
|
253
|
-
radialGradient: {
|
|
254
|
-
startColor: "#0f1729",
|
|
255
|
-
endColor: "#060a14",
|
|
256
|
-
},
|
|
257
|
-
showStatusDot: true,
|
|
258
|
-
motionFx: true,
|
|
259
|
-
maxSelectNodes: "unlimited",
|
|
260
|
-
maxSelectEdges: "unlimited",
|
|
261
|
-
enableZOrder: false,
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
// 组件实例引用
|
|
265
|
-
const topoRef = ref(null);
|
|
266
|
-
|
|
267
|
-
// 事件处理函数
|
|
268
|
-
const onNodeClick = (node) => {
|
|
269
|
-
console.log("节点被点击:", node);
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
const onEdgeSelect = (edge) => {
|
|
273
|
-
console.log("连线被选中:", edge);
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
const onSelectionChange = (selection) => {
|
|
277
|
-
console.log("选中变化:", selection);
|
|
278
|
-
console.log("选中节点:", selection.nodes);
|
|
279
|
-
console.log("选中连线:", selection.edges);
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
const onDataChange = (payload) => {
|
|
283
|
-
console.log("数据变动:", payload);
|
|
284
|
-
console.log("变动摘要:", payload.summary);
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
const onEdgeCreate = (edge) => {
|
|
288
|
-
console.log("新建连线:", edge);
|
|
289
|
-
// 持久化新创建的连线
|
|
290
|
-
edges.value = [...edges.value, edge];
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
// 方法调用示例
|
|
294
|
-
const handleMethods = () => {
|
|
295
|
-
// 获取当前数据
|
|
296
|
-
const data = topoRef.value.getData();
|
|
297
|
-
|
|
298
|
-
// 添加节点
|
|
299
|
-
topoRef.value.addNodes([
|
|
300
|
-
{ label: "新节点", nodeType: "server", x: 500, y: 0 },
|
|
301
|
-
]);
|
|
302
|
-
|
|
303
|
-
// 添加连线
|
|
304
|
-
topoRef.value.addEdges([{ from: "core", to: "new-node-id" }]);
|
|
305
|
-
|
|
306
|
-
// 更新节点
|
|
307
|
-
topoRef.value.updateNodes([{ id: "core", label: "更新后的名称" }]);
|
|
308
|
-
|
|
309
|
-
// 删除选中元素
|
|
310
|
-
topoRef.value.removeSelected();
|
|
311
|
-
|
|
312
|
-
// 坐标转换
|
|
313
|
-
const svgPos = topoRef.value.clientToSvg(event);
|
|
314
|
-
const clientPos = topoRef.value.svgToClient(node.x, node.y);
|
|
315
|
-
|
|
316
|
-
// 图层管理(需启用 enableZOrder)
|
|
317
|
-
topoRef.value.moveToFront("node-id");
|
|
318
|
-
topoRef.value.moveToBack("node-id");
|
|
319
|
-
topoRef.value.moveLayerUp("node-id");
|
|
320
|
-
topoRef.value.moveLayerDown("node-id");
|
|
321
|
-
};
|
|
322
|
-
</script>
|
|
323
|
-
|
|
324
|
-
<style scoped>
|
|
325
|
-
.topology-container {
|
|
326
|
-
width: 100%;
|
|
327
|
-
height: 600px;
|
|
328
|
-
background: radial-gradient(ellipse at center, #0f1729 0%, #060a14 100%);
|
|
329
|
-
}
|
|
330
|
-
</style>
|
|
331
|
-
```
|
|
332
|
-
|
|
333
|
-
---
|
|
334
|
-
|
|
335
|
-
## 全局配置属性说明
|
|
336
|
-
|
|
337
|
-
| 配置项 | 类型 | 默认值 | 说明 |
|
|
338
|
-
| --------------------- | ----------------------- | --------- | ----------------------------------------------------------------------------------------- |
|
|
339
|
-
| `selectedBorderColor` | `String` | `#fbbf24` | 选中节点的描边颜色 |
|
|
340
|
-
| `selectedLineColor` | `String` | `#fbbf24` | 选中连线的颜色 |
|
|
341
|
-
| `nodeDraggable` | `Boolean` | `true` | 节点拖拽开关(需 `editable`) |
|
|
342
|
-
| `nodeResizable` | `Boolean` | `true` | 节点尺寸调整开关(需 `editable`) |
|
|
343
|
-
| `enableZoom` | `Boolean` | `true` | 滚轮缩放开关 |
|
|
344
|
-
| `enablePan` | `Boolean` | `true` | 画布拖拽平移开关 |
|
|
345
|
-
| `enableMarquee` | `Boolean` | `true` | Ctrl + 左键框选开关 |
|
|
346
|
-
| `maxSelectNodes` | `Number \| "unlimited"` | 不限制 | 最多选中节点数:`0`(禁止)/ `1` / `2` 或任意非负整数;不设或 `"unlimited"` 不限制 |
|
|
347
|
-
| `maxSelectEdges` | `Number \| "unlimited"` | 不限制 | 最多选中连线数(合并线按整组内全部连线计数) |
|
|
348
|
-
| `showGrid` | `Boolean` | `true` | 网格背景(设 `false` 关闭) |
|
|
349
|
-
| `gridColor` | `String` | `#1a2240` | 网格线颜色 |
|
|
350
|
-
| `gridStep` | `Number` | `40` | 网格间距(1x 缩放下的 px,随缩放联动) |
|
|
351
|
-
| `gridOpacity` | `Number` | `0.5` | 网格叠层透明度(0–1) |
|
|
352
|
-
| `radialGradient` | `Object` | 见说明 | 画布背景径向渐变 |
|
|
353
|
-
| `showCenterPoint` | `Boolean` | `false` | 画布中心十字标记 |
|
|
354
|
-
| `showEdgeTextBg` | `Boolean` | `false` | 连线文字底牌 |
|
|
355
|
-
| `edgeTextBgColor` | `String` | `#0a0e1a` | 连线文字底牌颜色 |
|
|
356
|
-
| `edgeCenterTextFollowLine` | `Boolean` | `true` | 中心文字是否跟随线方向旋转:`true` 随线倾斜(现状),`false` 始终保持水平 |
|
|
357
|
-
| `showNodeHandles` | `Boolean` | `false` | 节点手柄(开启后节点上下左右出现四个小圆圈,拖到另一节点松开自动连线) |
|
|
358
|
-
| `showStatusDot` | `Boolean` | `true` | 节点右上角状态圆点开关 |
|
|
359
|
-
| `labelGap` | `Number` | `18` | 节点标签与节点盒边缘的固定间距(1x 缩放下 px) |
|
|
360
|
-
| `watchDataChanges` | `Boolean` | `false` | 组件内数据变动检测(防抖 300ms 后触发 `data-change`) |
|
|
361
|
-
| `fx` | `Boolean` | 自动 | 装饰动效总开关:`false` 全关 / `true` 全开 / 不设 = 自动 |
|
|
362
|
-
| `motionFx` | `Boolean` | 自动 | 连接线动画(流光 + 粒子)单项覆盖 |
|
|
363
|
-
| `glowFx` | `Boolean` | 自动 | 连线发光单项覆盖 |
|
|
364
|
-
| `breatheFx` | `Boolean` | 自动 |
|
|
365
|
-
| `
|
|
366
|
-
| `
|
|
367
|
-
| `
|
|
368
|
-
| `
|
|
369
|
-
| `
|
|
370
|
-
| `
|
|
371
|
-
| `
|
|
372
|
-
| `
|
|
373
|
-
| `
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
radialGradient
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
|
406
|
-
|
|
|
407
|
-
| `
|
|
408
|
-
| `
|
|
409
|
-
| `
|
|
410
|
-
| `
|
|
411
|
-
| `
|
|
412
|
-
| `
|
|
413
|
-
| `
|
|
414
|
-
| `
|
|
415
|
-
| `
|
|
416
|
-
| `
|
|
417
|
-
| `
|
|
418
|
-
| `
|
|
419
|
-
| `
|
|
420
|
-
| `
|
|
421
|
-
| `
|
|
422
|
-
| `
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
|
427
|
-
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
|
432
|
-
|
|
|
433
|
-
| `"
|
|
434
|
-
| `"
|
|
435
|
-
| `"
|
|
436
|
-
| `"
|
|
437
|
-
| `"
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
|
465
|
-
|
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
|
470
|
-
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
|
495
|
-
|
|
|
496
|
-
| `"
|
|
497
|
-
| `"
|
|
498
|
-
| `"
|
|
499
|
-
| `"
|
|
500
|
-
| `"
|
|
501
|
-
| `"
|
|
502
|
-
| `"
|
|
503
|
-
| `"
|
|
504
|
-
| `"
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
|
516
|
-
|
|
|
517
|
-
| `
|
|
518
|
-
| `
|
|
519
|
-
| `
|
|
520
|
-
| `
|
|
521
|
-
| `
|
|
522
|
-
| `
|
|
523
|
-
| `
|
|
524
|
-
| `
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
//
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
//
|
|
588
|
-
topoRef.value.
|
|
589
|
-
|
|
590
|
-
//
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
//
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
const
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
|
624
|
-
|
|
|
625
|
-
| `
|
|
626
|
-
| `
|
|
627
|
-
| `
|
|
628
|
-
| `
|
|
629
|
-
| `
|
|
630
|
-
| `edge-
|
|
631
|
-
| `
|
|
632
|
-
| `
|
|
633
|
-
| `
|
|
634
|
-
| `
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
|
661
|
-
|
|
|
662
|
-
|
|
|
663
|
-
|
|
|
664
|
-
|
|
|
665
|
-
|
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
|
670
|
-
|
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
1
|
+
# vue3-network-graph 使用说明
|
|
2
|
+
|
|
3
|
+
基于 **Vue 3 + SVG** 的轻量级网络拓扑图组件,无第三方图表库依赖。支持节点拖拽、画布无限平移、滚轮缩放、连线流向动画、三种连线模式、同节点对多连线防重叠、连线文字沿线直立等能力。
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 目录
|
|
8
|
+
|
|
9
|
+
- [名称](#名称)
|
|
10
|
+
- [描述](#描述)
|
|
11
|
+
- [使用方法](#使用方法)
|
|
12
|
+
- [使用例子](#使用例子)
|
|
13
|
+
- [全局配置属性说明](#全局配置属性说明)
|
|
14
|
+
- [节点属性说明](#节点属性说明)
|
|
15
|
+
- [连线属性说明](#连线属性说明)
|
|
16
|
+
- [调用方法](#调用方法)
|
|
17
|
+
- [事件说明](#事件说明)
|
|
18
|
+
- [交互操作](#交互操作)
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 名称
|
|
23
|
+
|
|
24
|
+
**vue3-network-graph** - Vue3 网络拓扑图组件
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## 描述
|
|
29
|
+
|
|
30
|
+
一个纯 SVG 实现的轻量级网络拓扑图组件,提供以下核心能力:
|
|
31
|
+
|
|
32
|
+
### 核心功能
|
|
33
|
+
|
|
34
|
+
- **多种连线模式**:直线(straight)/ 折线(polyline)/ 曲线(curve)
|
|
35
|
+
- **智能多线处理**:同一对节点的多条连线自动防重叠,任何模式下都保持平行
|
|
36
|
+
- **连线文字直立**:`fromText` / `centerText` / `toText` 沿路径切向旋转,超过±90° 自动翻转保持可读
|
|
37
|
+
- **丰富的节点类型**:8 种图标节点 + 文本节点 + 纯边框节点
|
|
38
|
+
- **图片节点**:照片 / 头像 / 位图节点,按真实比例等比适配(整图完整显示或铺满裁剪,永不拉伸)
|
|
39
|
+
- **2D/3D 视图**:支持 2D 平面和 2.5D 卡片风格
|
|
40
|
+
- **流向动画**:虚线流光 + 粒子效果,可单独控制每条连线的动画开关
|
|
41
|
+
- **连线不被节点吃掉**:节点分层绘制(底板在下、图标/标签在上),连线穿过节点或节点重叠时依然连续可见;选中链路自动提升到最上层
|
|
42
|
+
|
|
43
|
+
### 交互能力
|
|
44
|
+
|
|
45
|
+
- **节点操作**:拖拽移动、拖拽调整尺寸、多选整体拖动
|
|
46
|
+
- **画布操作**:无限平移(空白左键/中键)、滚轮缩放(0.2x–5x)
|
|
47
|
+
- **选择模式**:单击选中、Ctrl+ 点击多选、Ctrl+ 左键框选
|
|
48
|
+
- **连线创建**:从节点手柄拖拽到目标节点自动连线
|
|
49
|
+
- **图层管理**:Z 轴排序、右键菜单、快捷键操作(需启用 `enableZOrder`)
|
|
50
|
+
|
|
51
|
+
### 性能优化
|
|
52
|
+
|
|
53
|
+
- **自动降级**:装饰动效根据规模自动关闭(连线>200 关流光、>500 关发光、节点>300 关呼吸)
|
|
54
|
+
- **大图性能**:千级节点可流畅平移/拖拽,密集图形下节点优先响应点击
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 使用方法
|
|
59
|
+
|
|
60
|
+
### 1. 安装
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npm install vue3-network-graph
|
|
64
|
+
# 或:pnpm add vue3-network-graph | yarn add vue3-network-graph
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 2. 引入组件
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
// main.js
|
|
71
|
+
import { createApp } from "vue";
|
|
72
|
+
import { Topology } from "vue3-network-graph";
|
|
73
|
+
import "vue3-network-graph/style.css";
|
|
74
|
+
|
|
75
|
+
const app = createApp(App);
|
|
76
|
+
app.component("Topology", Topology);
|
|
77
|
+
app.mount("#app");
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
或局部引入:
|
|
81
|
+
|
|
82
|
+
```vue
|
|
83
|
+
<script setup>
|
|
84
|
+
import { Topology } from "vue3-network-graph";
|
|
85
|
+
import "vue3-network-graph/style.css";
|
|
86
|
+
</script>
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 3. 基本使用
|
|
90
|
+
|
|
91
|
+
```vue
|
|
92
|
+
<template>
|
|
93
|
+
<Topology
|
|
94
|
+
ref="topoRef"
|
|
95
|
+
:editable="true"
|
|
96
|
+
v-model:nodes="myNodes"
|
|
97
|
+
v-model:edges="myEdges"
|
|
98
|
+
:config="config"
|
|
99
|
+
@node-click="handleNodeClick"
|
|
100
|
+
@selection-change="handleSelectionChange"
|
|
101
|
+
/>
|
|
102
|
+
</template>
|
|
103
|
+
|
|
104
|
+
<script setup>
|
|
105
|
+
import { ref } from "vue";
|
|
106
|
+
import { Topology } from "vue3-network-graph";
|
|
107
|
+
|
|
108
|
+
const myNodes = ref([
|
|
109
|
+
{ id: "node1", label: "节点 1", type: "core", state: "normal", x: 0, y: 0 },
|
|
110
|
+
{
|
|
111
|
+
id: "node2",
|
|
112
|
+
label: "节点 2",
|
|
113
|
+
type: "server",
|
|
114
|
+
state: "normal",
|
|
115
|
+
x: 200,
|
|
116
|
+
y: 100,
|
|
117
|
+
},
|
|
118
|
+
]);
|
|
119
|
+
|
|
120
|
+
const myEdges = ref([
|
|
121
|
+
{ id: "edge1", from: "node1", to: "node2", centerText: "连接" },
|
|
122
|
+
]);
|
|
123
|
+
|
|
124
|
+
const config = ref({
|
|
125
|
+
showGrid: true,
|
|
126
|
+
nodeDraggable: true,
|
|
127
|
+
nodeResizable: true,
|
|
128
|
+
});
|
|
129
|
+
</script>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## 使用例子
|
|
135
|
+
|
|
136
|
+
### 完整示例
|
|
137
|
+
|
|
138
|
+
```vue
|
|
139
|
+
<template>
|
|
140
|
+
<div class="topology-container">
|
|
141
|
+
<Topology
|
|
142
|
+
ref="topoRef"
|
|
143
|
+
:editable="true"
|
|
144
|
+
v-model:nodes="nodes"
|
|
145
|
+
v-model:edges="edges"
|
|
146
|
+
v-model:line-mode="lineMode"
|
|
147
|
+
v-model:view-mode="viewMode"
|
|
148
|
+
:groups="groups"
|
|
149
|
+
:config="config"
|
|
150
|
+
@node-click="onNodeClick"
|
|
151
|
+
@edge-select="onEdgeSelect"
|
|
152
|
+
@selection-change="onSelectionChange"
|
|
153
|
+
@data-change="onDataChange"
|
|
154
|
+
@edge-create="onEdgeCreate"
|
|
155
|
+
>
|
|
156
|
+
<!-- 自定义节点图标插槽 -->
|
|
157
|
+
<template #node-icon="{ node, color, viewMode }">
|
|
158
|
+
<circle r="16" :fill="color" opacity="0.9" />
|
|
159
|
+
</template>
|
|
160
|
+
</Topology>
|
|
161
|
+
</div>
|
|
162
|
+
</template>
|
|
163
|
+
|
|
164
|
+
<script setup>
|
|
165
|
+
import { ref, onMounted } from "vue";
|
|
166
|
+
import { Topology } from "vue3-network-graph";
|
|
167
|
+
import "vue3-network-graph/style.css";
|
|
168
|
+
|
|
169
|
+
// 节点数据
|
|
170
|
+
const nodes = ref([
|
|
171
|
+
{
|
|
172
|
+
id: "core",
|
|
173
|
+
label: "核心交换机",
|
|
174
|
+
nodeType: "core",
|
|
175
|
+
state: "normal",
|
|
176
|
+
x: 0,
|
|
177
|
+
y: 0,
|
|
178
|
+
width: 80,
|
|
179
|
+
height: 80,
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
id: "server1",
|
|
183
|
+
label: "Web 服务器",
|
|
184
|
+
nodeType: "server",
|
|
185
|
+
state: "normal",
|
|
186
|
+
x: 300,
|
|
187
|
+
y: -150,
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
id: "db1",
|
|
191
|
+
label: "数据库",
|
|
192
|
+
nodeType: "database",
|
|
193
|
+
state: "idle",
|
|
194
|
+
x: 300,
|
|
195
|
+
y: 150,
|
|
196
|
+
},
|
|
197
|
+
// 纯边框节点示例
|
|
198
|
+
{
|
|
199
|
+
id: "border-box",
|
|
200
|
+
label: "",
|
|
201
|
+
nodeType: "border-only",
|
|
202
|
+
state: "none",
|
|
203
|
+
x: -400,
|
|
204
|
+
y: -200,
|
|
205
|
+
width: 250,
|
|
206
|
+
height: 150,
|
|
207
|
+
borderWidth: 2,
|
|
208
|
+
borderColor: "#00f0ff",
|
|
209
|
+
},
|
|
210
|
+
]);
|
|
211
|
+
|
|
212
|
+
// 连线数据
|
|
213
|
+
const edges = ref([
|
|
214
|
+
{ id: "e1", from: "core", to: "server1", speed: 1.5, endMarker: "arrow" },
|
|
215
|
+
{
|
|
216
|
+
id: "e2",
|
|
217
|
+
from: "core",
|
|
218
|
+
to: "db1",
|
|
219
|
+
speed: 2,
|
|
220
|
+
centerText: "查询流量",
|
|
221
|
+
endMarker: "arrow",
|
|
222
|
+
},
|
|
223
|
+
]);
|
|
224
|
+
|
|
225
|
+
// 分组示例
|
|
226
|
+
const groups = ref([
|
|
227
|
+
{
|
|
228
|
+
id: "g1",
|
|
229
|
+
label: "应用区",
|
|
230
|
+
color: "#409eff",
|
|
231
|
+
nodeIds: ["server1", "db1"],
|
|
232
|
+
padding: 20,
|
|
233
|
+
},
|
|
234
|
+
]);
|
|
235
|
+
|
|
236
|
+
// 连线模式和视图模式
|
|
237
|
+
const lineMode = ref("curve"); // straight | polyline | curve
|
|
238
|
+
const viewMode = ref("2d"); // 2d | 3d
|
|
239
|
+
|
|
240
|
+
// 配置项
|
|
241
|
+
const config = ref({
|
|
242
|
+
selectedBorderColor: "#ffd400",
|
|
243
|
+
selectedLineColor: "#ffd400",
|
|
244
|
+
nodeDraggable: true,
|
|
245
|
+
nodeResizable: true,
|
|
246
|
+
enableZoom: true,
|
|
247
|
+
enablePan: true,
|
|
248
|
+
enableMarquee: true,
|
|
249
|
+
showGrid: true,
|
|
250
|
+
gridColor: "#1a2240",
|
|
251
|
+
gridStep: 40,
|
|
252
|
+
gridOpacity: 0.5,
|
|
253
|
+
radialGradient: {
|
|
254
|
+
startColor: "#0f1729",
|
|
255
|
+
endColor: "#060a14",
|
|
256
|
+
},
|
|
257
|
+
showStatusDot: true,
|
|
258
|
+
motionFx: true,
|
|
259
|
+
maxSelectNodes: "unlimited",
|
|
260
|
+
maxSelectEdges: "unlimited",
|
|
261
|
+
enableZOrder: false,
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
// 组件实例引用
|
|
265
|
+
const topoRef = ref(null);
|
|
266
|
+
|
|
267
|
+
// 事件处理函数
|
|
268
|
+
const onNodeClick = (node) => {
|
|
269
|
+
console.log("节点被点击:", node);
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const onEdgeSelect = (edge) => {
|
|
273
|
+
console.log("连线被选中:", edge);
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const onSelectionChange = (selection) => {
|
|
277
|
+
console.log("选中变化:", selection);
|
|
278
|
+
console.log("选中节点:", selection.nodes);
|
|
279
|
+
console.log("选中连线:", selection.edges);
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
const onDataChange = (payload) => {
|
|
283
|
+
console.log("数据变动:", payload);
|
|
284
|
+
console.log("变动摘要:", payload.summary);
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
const onEdgeCreate = (edge) => {
|
|
288
|
+
console.log("新建连线:", edge);
|
|
289
|
+
// 持久化新创建的连线
|
|
290
|
+
edges.value = [...edges.value, edge];
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
// 方法调用示例
|
|
294
|
+
const handleMethods = () => {
|
|
295
|
+
// 获取当前数据
|
|
296
|
+
const data = topoRef.value.getData();
|
|
297
|
+
|
|
298
|
+
// 添加节点
|
|
299
|
+
topoRef.value.addNodes([
|
|
300
|
+
{ label: "新节点", nodeType: "server", x: 500, y: 0 },
|
|
301
|
+
]);
|
|
302
|
+
|
|
303
|
+
// 添加连线
|
|
304
|
+
topoRef.value.addEdges([{ from: "core", to: "new-node-id" }]);
|
|
305
|
+
|
|
306
|
+
// 更新节点
|
|
307
|
+
topoRef.value.updateNodes([{ id: "core", label: "更新后的名称" }]);
|
|
308
|
+
|
|
309
|
+
// 删除选中元素
|
|
310
|
+
topoRef.value.removeSelected();
|
|
311
|
+
|
|
312
|
+
// 坐标转换
|
|
313
|
+
const svgPos = topoRef.value.clientToSvg(event);
|
|
314
|
+
const clientPos = topoRef.value.svgToClient(node.x, node.y);
|
|
315
|
+
|
|
316
|
+
// 图层管理(需启用 enableZOrder)
|
|
317
|
+
topoRef.value.moveToFront("node-id");
|
|
318
|
+
topoRef.value.moveToBack("node-id");
|
|
319
|
+
topoRef.value.moveLayerUp("node-id");
|
|
320
|
+
topoRef.value.moveLayerDown("node-id");
|
|
321
|
+
};
|
|
322
|
+
</script>
|
|
323
|
+
|
|
324
|
+
<style scoped>
|
|
325
|
+
.topology-container {
|
|
326
|
+
width: 100%;
|
|
327
|
+
height: 600px;
|
|
328
|
+
background: radial-gradient(ellipse at center, #0f1729 0%, #060a14 100%);
|
|
329
|
+
}
|
|
330
|
+
</style>
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
---
|
|
334
|
+
|
|
335
|
+
## 全局配置属性说明
|
|
336
|
+
|
|
337
|
+
| 配置项 | 类型 | 默认值 | 说明 |
|
|
338
|
+
| --------------------- | ----------------------- | --------- | ----------------------------------------------------------------------------------------- |
|
|
339
|
+
| `selectedBorderColor` | `String` | `#fbbf24` | 选中节点的描边颜色 |
|
|
340
|
+
| `selectedLineColor` | `String` | `#fbbf24` | 选中连线的颜色 |
|
|
341
|
+
| `nodeDraggable` | `Boolean` | `true` | 节点拖拽开关(需 `editable`) |
|
|
342
|
+
| `nodeResizable` | `Boolean` | `true` | 节点尺寸调整开关(需 `editable`) |
|
|
343
|
+
| `enableZoom` | `Boolean` | `true` | 滚轮缩放开关 |
|
|
344
|
+
| `enablePan` | `Boolean` | `true` | 画布拖拽平移开关 |
|
|
345
|
+
| `enableMarquee` | `Boolean` | `true` | Ctrl + 左键框选开关 |
|
|
346
|
+
| `maxSelectNodes` | `Number \| "unlimited"` | 不限制 | 最多选中节点数:`0`(禁止)/ `1` / `2` 或任意非负整数;不设或 `"unlimited"` 不限制 |
|
|
347
|
+
| `maxSelectEdges` | `Number \| "unlimited"` | 不限制 | 最多选中连线数(合并线按整组内全部连线计数) |
|
|
348
|
+
| `showGrid` | `Boolean` | `true` | 网格背景(设 `false` 关闭) |
|
|
349
|
+
| `gridColor` | `String` | `#1a2240` | 网格线颜色 |
|
|
350
|
+
| `gridStep` | `Number` | `40` | 网格间距(1x 缩放下的 px,随缩放联动) |
|
|
351
|
+
| `gridOpacity` | `Number` | `0.5` | 网格叠层透明度(0–1) |
|
|
352
|
+
| `radialGradient` | `Object` | 见说明 | 画布背景径向渐变 |
|
|
353
|
+
| `showCenterPoint` | `Boolean` | `false` | 画布中心十字标记 |
|
|
354
|
+
| `showEdgeTextBg` | `Boolean` | `false` | 连线文字底牌 |
|
|
355
|
+
| `edgeTextBgColor` | `String` | `#0a0e1a` | 连线文字底牌颜色 |
|
|
356
|
+
| `edgeCenterTextFollowLine` | `Boolean` | `true` | 中心文字是否跟随线方向旋转:`true` 随线倾斜(现状),`false` 始终保持水平 |
|
|
357
|
+
| `showNodeHandles` | `Boolean` | `false` | 节点手柄(开启后节点上下左右出现四个小圆圈,拖到另一节点松开自动连线) |
|
|
358
|
+
| `showStatusDot` | `Boolean` | `true` | 节点右上角状态圆点开关 |
|
|
359
|
+
| `labelGap` | `Number` | `18` | 节点标签与节点盒边缘的固定间距(1x 缩放下 px) |
|
|
360
|
+
| `watchDataChanges` | `Boolean` | `false` | 组件内数据变动检测(防抖 300ms 后触发 `data-change`) |
|
|
361
|
+
| `fx` | `Boolean` | 自动 | 装饰动效总开关:`false` 全关 / `true` 全开 / 不设 = 自动 |
|
|
362
|
+
| `motionFx` | `Boolean` | 自动 | 连接线动画(流光 + 粒子)单项覆盖 |
|
|
363
|
+
| `glowFx` | `Boolean` | 自动 | 连线发光单项覆盖 |
|
|
364
|
+
| `breatheFx` | `Boolean` | 自动 | 告警呼吸环单项覆盖:`false` 关闭告警动画,`true` 强制开启(忽略规模门控) |
|
|
365
|
+
| `alertStates` | `String[]` | 见说明 | 触发呼吸环的告警态白名单,默认 `["warning","error","master"]`;其余 state 只改配色不动画 |
|
|
366
|
+
| `alertStyle` | `"ring" \| "fill" \| "ripple" \| "blink"` | `"ring"` | 告警动画样式:ring 描边环外扩(error/master 叠错相位外环)/ fill 实心呼吸圈 / ripple 三道水波扩散 / blink 硬闪;单节点可用 `node.alertStyle` 覆盖 |
|
|
367
|
+
| `alertGlow` | `Boolean` | `true` | `fill` 实心呼吸圈的边缘渐变发光(贴边亮环 + 向外柔和衰减);设为 `false` 退回纯实心圈 |
|
|
368
|
+
| `fxMotionMaxEdges` | `Number` | `200` | 流光 + 粒子动画自动关闭的连线数阈值 |
|
|
369
|
+
| `fxGlowMaxEdges` | `Number` | `500` | 连线发光自动关闭的连线数阈值 |
|
|
370
|
+
| `fxAlertMaxNodes` | `Number` | `300` | 告警节点数超过该值时自动关闭呼吸环(按告警节点数计,不再因全图规模被整体关掉) |
|
|
371
|
+
| `fxBreatheMaxNodes` | `Number` | `300` | 旧版呼吸灯节点数阈值(已由 `fxAlertMaxNodes` 取代,保留兼容) |
|
|
372
|
+
| `enableZOrder` | `Boolean` | `false` | **启用 Z 轴层级排序**:启用后节点按 `zIndex` 属性渲染,支持图层操作 API、右键菜单和快捷键 |
|
|
373
|
+
| `enableDeleteKey` | `Boolean` | `true` | Delete / Backspace 删除当前选中(需 `editable`);父级监听 `delete-request` 时只抛事件、不直接删除 |
|
|
374
|
+
| `imageFit` | `"contain" \| "cover"` | `"contain"` | 图片节点填充方式:`contain` 整图完整显示在节点框内(居中留白、不裁剪);`cover` 铺满节点框并裁掉溢出。两者都等比缩放,不会拉伸变形 |
|
|
375
|
+
| `imageNodeBySource` | `Boolean` | `false` | **图片节点全局判定**:`true` 时节点只要带图片地址就按图片节点渲染(忽略 `nodeType`);文本节点、纯边框节点不受影响 |
|
|
376
|
+
| `edgeLayer` | `"split" \| "below"` | `"split"` | 节点与连线的绘制顺序:`split` 把节点拆两层——底板+边框在下、图标/图片/标签在上,连线夹在中间(线穿过节点不再被整块盖住);`below` 为传统的「所有连线在所有节点之下」 |
|
|
377
|
+
| `highlightEdgeOnTop` | `Boolean` | `true` | 选中的连线额外绘制在节点层之上,避免高亮链路被节点底板或图标遮挡(纯装饰层,不参与点击命中) |
|
|
378
|
+
|
|
379
|
+
### radialGradient 配置
|
|
380
|
+
|
|
381
|
+
```javascript
|
|
382
|
+
// 简单模式
|
|
383
|
+
radialGradient: {
|
|
384
|
+
startColor: "#0f1729",
|
|
385
|
+
endColor: "#060a14"
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// 高级模式(自定义渐变中心和色带)
|
|
389
|
+
radialGradient: {
|
|
390
|
+
cx: "50%",
|
|
391
|
+
cy: "50%",
|
|
392
|
+
r: "50%",
|
|
393
|
+
stops: [
|
|
394
|
+
{ offset: "0%", color: "#0f1729", opacity: 1 },
|
|
395
|
+
{ offset: "60%", color: "#1e293b", opacity: 0.8 },
|
|
396
|
+
{ offset: "100%", color: "#060a14", opacity: 1 }
|
|
397
|
+
]
|
|
398
|
+
}
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
---
|
|
402
|
+
|
|
403
|
+
## 节点属性说明
|
|
404
|
+
|
|
405
|
+
| 属性 | 类型 | 必填 | 默认值 | 说明 |
|
|
406
|
+
| --------------- | ------------------------------- | ---- | ----------- | ------------------------------------------------------------ |
|
|
407
|
+
| `id` | `String` | ✅ | - | 节点唯一标识 |
|
|
408
|
+
| `label` | `String \| null` | ❌ | `null` | 节点显示文本(支持 `\n` 换行),纯边框节点和文本节点可设为空 |
|
|
409
|
+
| `nodeType` | `String` | ❌ | `"server"` | 节点类型(见下方类型列表) |
|
|
410
|
+
| `image` | `String` | ❌ | - | 图片地址,也可用别名 `src` / `img` / `imageUrl`(见下方「图片节点」) |
|
|
411
|
+
| `state` | `String` | ❌ | `"none"` | 节点状态(见下方状态列表) |
|
|
412
|
+
| `alertStyle` | `String` | ❌ | - | 节点级告警样式覆盖(`ring` / `fill` / `ripple` / `blink`,见 `config.alertStyle`) |
|
|
413
|
+
| `x` | `Number` | ✅ | - | 节点 X 坐标(SVG 坐标系) |
|
|
414
|
+
| `y` | `Number` | ✅ | - | 节点 Y 坐标(SVG 坐标系) |
|
|
415
|
+
| `width` | `Number` | ❌ | `60` | 节点宽度(纯边框节点必需) |
|
|
416
|
+
| `height` | `Number` | ❌ | `60` | 节点高度(纯边框节点必需) |
|
|
417
|
+
| `rotation` | `Number` | ❌ | `0` | 节点旋转角度(度) |
|
|
418
|
+
| `fontColor` | `String` | ❌ | `"#ffffff"` | 文字颜色 |
|
|
419
|
+
| `fontSize` | `Number` | ❌ | `11` | 文字大小 |
|
|
420
|
+
| `fontWeight` | `Number \| String` | ❌ | `500` | 字重(100–900) |
|
|
421
|
+
| `borderColor` | `String` | ❌ | `"#94a3b8"` | 边框颜色 |
|
|
422
|
+
| `borderWidth` | `Number` | ❌ | `0` | 边框宽度(0 为无边框) |
|
|
423
|
+
| `borderStyle` | `"full" \| "dashed" \| "none"` | ❌ | `"full"` | 边框样式:`"full"` 实线,`"dashed"` 虚线,`"none"` 无边框 |
|
|
424
|
+
| `background` | `String` | ❌ | `"#0f1729"` | 背景颜色 |
|
|
425
|
+
| `borderRadius` | `Number` | ❌ | `6` | 圆角半径(px) |
|
|
426
|
+
| `labelPosition` | `"top" \| "center" \| "bottom"` | ❌ | `"bottom"` | 标签位置 |
|
|
427
|
+
| `zIndex` | `Number` | ❌ | `0` | Z 轴层级索引(需启用 `enableZOrder`) |
|
|
428
|
+
|
|
429
|
+
### 节点类型(nodeType)
|
|
430
|
+
|
|
431
|
+
| 类型 | 图标 | 说明 |
|
|
432
|
+
| ---------------- | ------------- | ---------------------------- |
|
|
433
|
+
| `"core"` | 六边形 | 核心设备 |
|
|
434
|
+
| `"server"` | 矩形堆叠 | 服务器 |
|
|
435
|
+
| `"firewall"` | 带横线的矩形 | 防火墙 |
|
|
436
|
+
| `"loadbalancer"` | 圆形 + 折线 | 负载均衡 |
|
|
437
|
+
| `"database"` | 圆柱体 | 数据库 |
|
|
438
|
+
| `"cdn"` | 同心圆 | CDN 节点 |
|
|
439
|
+
| `"cache"` | 六边形 + 圆心 | 缓存 |
|
|
440
|
+
| `"queue"` | 带竖线的矩形 | 队列 |
|
|
441
|
+
| `"text"` | 文本行 | 纯文本节点 |
|
|
442
|
+
| `"border-only"` | 空心矩形 | 纯边框节点(仅显示边框轮廓) |
|
|
443
|
+
|
|
444
|
+
### 图片节点
|
|
445
|
+
|
|
446
|
+
节点可以用图片(照片 / 头像 / 位图图标)渲染,组件会**等比缩放**它,不会出现拉伸变形。
|
|
447
|
+
|
|
448
|
+
**判定规则**(满足任一即为图片节点):
|
|
449
|
+
|
|
450
|
+
| 条件 | 示例 |
|
|
451
|
+
| ---- | ---- |
|
|
452
|
+
| `nodeType` 为 `"image"` / `"img"` / `"picture"`(大小写不敏感) | `{ nodeType: "image", image: "/a.png" }` |
|
|
453
|
+
| 没有 `nodeType`,且带图片地址 | `{ image: "/a.png" }` |
|
|
454
|
+
| 开启 `config.imageNodeBySource`,且带图片地址(忽略 `nodeType`) | `{ nodeType: "server", image: "/a.png" }` |
|
|
455
|
+
|
|
456
|
+
- 图片地址按 **`image` → `src` → `img` → `imageUrl`** 顺序取第一个有值的字段;必须是 trim 后非空的字符串。
|
|
457
|
+
- 文本节点(`nodeType: "text"`)与纯边框节点(`"border-only"`)始终按各自类型渲染,不会被转成图片。
|
|
458
|
+
- 带 `nodeType` 的节点**默认不会**变成图片节点(图标按 `nodeType` 渲染);整个图都是照片时,用 `imageNodeBySource: true` 一次性放开。
|
|
459
|
+
|
|
460
|
+
**填充方式**:`config.imageFit`
|
|
461
|
+
|
|
462
|
+
| 值 | 效果 |
|
|
463
|
+
| -- | ---- |
|
|
464
|
+
| `"contain"`(默认) | 整张图完整落在节点框内,居中留白(letterbox),**不裁剪** |
|
|
465
|
+
| `"cover"` | 按最短边铺满节点框,长边超出的部分被裁掉 |
|
|
466
|
+
|
|
467
|
+
**自定义插槽**:`#node-icon` 会收到以下 props,可自行决定画什么——
|
|
468
|
+
|
|
469
|
+
| prop | 说明 |
|
|
470
|
+
| ---- | ---- |
|
|
471
|
+
| `isImage` | 当前节点是否被判为图片节点 |
|
|
472
|
+
| `image` | 图片地址(可能为空串) |
|
|
473
|
+
| `fit` | `"contain"` / `"cover"` |
|
|
474
|
+
| `imageWidth` / `imageHeight` | 已按图片真实比例算好的绘制尺寸(图片节点才有,衬在节点框中心即可) |
|
|
475
|
+
| `width` / `height` | 节点框尺寸 |
|
|
476
|
+
|
|
477
|
+
```vue
|
|
478
|
+
<Topology :nodes="nodes" :edges="edges" :config="{ imageFit: 'contain' }">
|
|
479
|
+
<template #node-icon="{ isImage, image, imageWidth, imageHeight }">
|
|
480
|
+
<image
|
|
481
|
+
v-if="isImage"
|
|
482
|
+
:href="image"
|
|
483
|
+
:x="-imageWidth / 2"
|
|
484
|
+
:y="-imageHeight / 2"
|
|
485
|
+
:width="imageWidth"
|
|
486
|
+
:height="imageHeight"
|
|
487
|
+
/>
|
|
488
|
+
</template>
|
|
489
|
+
</Topology>
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
### 节点状态(state)
|
|
493
|
+
|
|
494
|
+
| 状态 | 颜色 | 说明 |
|
|
495
|
+
| ------------ | ---- | ---------- |
|
|
496
|
+
| `"none"` | 隐藏 | 无状态指示 |
|
|
497
|
+
| `"normal"` | 绿色 | 正常 |
|
|
498
|
+
| `"warning"` | 黄色 | 警告 |
|
|
499
|
+
| `"error"` | 红色 | 错误 |
|
|
500
|
+
| `"info"` | 蓝色 | 信息 |
|
|
501
|
+
| `"active"` | 青色 | 活跃 |
|
|
502
|
+
| `"disabled"` | 灰色 | 禁用 |
|
|
503
|
+
| `"locked"` | 紫色 | 锁定 |
|
|
504
|
+
| `"master"` | 深红 | 主节点 |
|
|
505
|
+
| `"expert"` | 粉红 | 专家节点 |
|
|
506
|
+
| `"advanced"` | 橙色 | 高级节点 |
|
|
507
|
+
| `"adept"` | 浅橙 | 熟练节点 |
|
|
508
|
+
| `"awakened"` | 蓝色 | 觉醒节点 |
|
|
509
|
+
| `"dormant"` | 灰紫 | 休眠节点 |
|
|
510
|
+
|
|
511
|
+
---
|
|
512
|
+
|
|
513
|
+
## 连线属性说明
|
|
514
|
+
|
|
515
|
+
| 属性 | 类型 | 必填 | 默认值 | 说明 |
|
|
516
|
+
| ------------ | ---------------------------- | ---- | ----------- | ----------------------------------------------------- |
|
|
517
|
+
| `id` | `String` | ✅ | - | 连线唯一标识(自动生成 uuid) |
|
|
518
|
+
| `from` | `String` | ✅ | - | 起始节点 id |
|
|
519
|
+
| `to` | `String` | ✅ | - | 目标节点 id |
|
|
520
|
+
| `speed` | `Number` | ❌ | `1` | 流速倍率(动画速度) |
|
|
521
|
+
| `animated` | `Boolean` | ❌ | `true` | 单条线动画开关(`false` 关闭该线流光/粒子) |
|
|
522
|
+
| `fromAnchor` | `"center" \| "edge"` | ❌ | `"edge"` | 起点锚点:`"center"` 中心 / `"edge"` 边界交点 |
|
|
523
|
+
| `toAnchor` | `"center" \| "edge"` | ❌ | `"edge"` | 终点锚点:`"center"` 中心 / `"edge"` 边界交点 |
|
|
524
|
+
| `fromText` | `String \| Object` | ❌ | `""` | 起点文字(见下方文字格式) |
|
|
525
|
+
| `centerText` | `String \| Object` | ❌ | `""` | 中心文字(见下方文字格式) |
|
|
526
|
+
| `toText` | `String \| Object` | ❌ | `""` | 终点文字(见下方文字格式) |
|
|
527
|
+
| `endMarker` | `"none" \| "arrow" \| "dot"` | ❌ | `"none"` | 终点标记:`"none"` 无 / `"arrow"` 箭头 / `"dot"` 圆点 |
|
|
528
|
+
| `fontSize` | `Number` | ❌ | `8` | 连线文字大小 |
|
|
529
|
+
| `fontColor` | `String` | ❌ | `"#00f0ff"` | 连线文字颜色 |
|
|
530
|
+
|
|
531
|
+
### 连线文字格式
|
|
532
|
+
|
|
533
|
+
```javascript
|
|
534
|
+
// 字符串简写形式
|
|
535
|
+
fromText: "起点文字"
|
|
536
|
+
|
|
537
|
+
// 对象完整形式
|
|
538
|
+
fromText: {
|
|
539
|
+
text: "起点文字", // 文字内容
|
|
540
|
+
rotation: 0, // 额外旋转角(度)
|
|
541
|
+
fontSize: 10, // 字号
|
|
542
|
+
color: "#ffffff" // 颜色
|
|
543
|
+
}
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
---
|
|
547
|
+
|
|
548
|
+
## 调用方法
|
|
549
|
+
|
|
550
|
+
通过模板 ref 调用组件暴露的方法:
|
|
551
|
+
|
|
552
|
+
```vue
|
|
553
|
+
<template>
|
|
554
|
+
<Topology ref="topoRef" />
|
|
555
|
+
</template>
|
|
556
|
+
|
|
557
|
+
<script setup>
|
|
558
|
+
import { ref } from "vue";
|
|
559
|
+
|
|
560
|
+
const topoRef = ref(null);
|
|
561
|
+
|
|
562
|
+
// 所有方法调用示例
|
|
563
|
+
const methods = () => {
|
|
564
|
+
// === 数据方法 ===
|
|
565
|
+
|
|
566
|
+
// 获取当前最新数据(深拷贝)
|
|
567
|
+
const { nodes, edges } = topoRef.value.getData();
|
|
568
|
+
|
|
569
|
+
// 重置数据变动检测基线
|
|
570
|
+
topoRef.value.resetDataBaseline();
|
|
571
|
+
|
|
572
|
+
// 组件外全量替换数据集
|
|
573
|
+
topoRef.value.setData({ nodes: nextNodes });
|
|
574
|
+
|
|
575
|
+
// 删除当前选中的节点与连线(删节点时级联删除所有与该节点相连的连线)
|
|
576
|
+
const removed = topoRef.value.removeSelected();
|
|
577
|
+
|
|
578
|
+
// 添加节点(数组或单个)
|
|
579
|
+
topoRef.value.addNodes([{ label: "新节点", x: 0, y: 0 }]);
|
|
580
|
+
|
|
581
|
+
// 添加连线(数组或单个)
|
|
582
|
+
topoRef.value.addEdges([{ from: "a", to: "b" }]);
|
|
583
|
+
|
|
584
|
+
// 按 id 局部更新节点(浅合并,undefined 忽略,未知 id 跳过)
|
|
585
|
+
topoRef.value.updateNodes([{ id: "node-1", label: "改名", width: 120 }]);
|
|
586
|
+
|
|
587
|
+
// 按 id 局部更新连线
|
|
588
|
+
topoRef.value.updateEdges([{ id: "edge-1", speed: 2 }]);
|
|
589
|
+
|
|
590
|
+
// === 图层管理(需 enableZOrder: true)===
|
|
591
|
+
|
|
592
|
+
// 上一层
|
|
593
|
+
topoRef.value.moveLayerUp("node-id");
|
|
594
|
+
|
|
595
|
+
// 下一层
|
|
596
|
+
topoRef.value.moveLayerDown("node-id");
|
|
597
|
+
|
|
598
|
+
// 最上层
|
|
599
|
+
topoRef.value.moveToFront("node-id");
|
|
600
|
+
|
|
601
|
+
// 最底层
|
|
602
|
+
topoRef.value.moveToBack("node-id");
|
|
603
|
+
|
|
604
|
+
// === 坐标转换 ===
|
|
605
|
+
|
|
606
|
+
// 场景一:鼠标坐标 → SVG 坐标(参数可传事件对象或坐标对)
|
|
607
|
+
const p1 = topoRef.value.clientToSvg(e); // 直接传事件对象
|
|
608
|
+
const p2 = topoRef.value.clientToSvg(clientX, y); // 传坐标对
|
|
609
|
+
|
|
610
|
+
// 场景二:SVG 坐标 → 屏幕坐标(输入 node.x / node.y,返回 clientX/clientY)
|
|
611
|
+
const node = topoRef.value.getData().nodes[0];
|
|
612
|
+
const { x, y } = topoRef.value.svgToClient(node.x, node.y);
|
|
613
|
+
menu.style.left = `${x}px`;
|
|
614
|
+
menu.style.top = `${y}px`;
|
|
615
|
+
};
|
|
616
|
+
</script>
|
|
617
|
+
```
|
|
618
|
+
|
|
619
|
+
---
|
|
620
|
+
|
|
621
|
+
## 事件说明
|
|
622
|
+
|
|
623
|
+
| 事件名 | 参数 | 说明 |
|
|
624
|
+
| ------------------- | --------------------------- | ---------------------------------------------------------------- |
|
|
625
|
+
| `update:nodes` | `nodes` | 节点数据更新(`v-model:nodes`);拖拽/尺寸调整过程中持续触发 |
|
|
626
|
+
| `update:lineMode` | `string` | 连线模式变更(`v-model:line-mode`) |
|
|
627
|
+
| `update:viewMode` | `string` | 视图模式变更(`v-model:view-mode`) |
|
|
628
|
+
| `node-click` | `node` | 节点被点击 |
|
|
629
|
+
| `node-select` | `node \| null` | 单选节点变化(多选场景请用 `selection-change`) |
|
|
630
|
+
| `edge-select` | `edge \| null` | 连线选中变化(合并线附带 `bundleEdges` 数组) |
|
|
631
|
+
| `selection-change` | `{ nodes, edges }` | 选中集合变化时触发;`nodes`/`edges` 为选中的节点与连线对象数组 |
|
|
632
|
+
| `data-change` | `{ nodes, edges, summary }` | `watchDataChanges` 开启时,数据变动防抖(300ms)后触发 |
|
|
633
|
+
| `edge-create` | `edge` | 节点手柄拖拽连接两个节点后触发 |
|
|
634
|
+
| `edge-remove` | `edges` | 删除连线后触发(含因节点删除而级联删除的连线) |
|
|
635
|
+
| `edge-update` | `edges` | `updateEdges()` 修改连线后触发 |
|
|
636
|
+
| `node-drag-start` | `node` | 节点拖拽开始;拖动多选集合时同样触发,payload 为被按住的那个节点 |
|
|
637
|
+
| `node-drag-end` | — | 节点拖拽结束 |
|
|
638
|
+
| `node-resize-start` | `node` | 尺寸调整开始 |
|
|
639
|
+
| `node-resize-end` | `node \| null` | 尺寸调整结束 |
|
|
640
|
+
|
|
641
|
+
### selection-change 事件详解
|
|
642
|
+
|
|
643
|
+
```javascript
|
|
644
|
+
// 支持多种选择方式
|
|
645
|
+
// 1. Ctrl+ 点击多选
|
|
646
|
+
// 2. Ctrl+ 左键框选
|
|
647
|
+
// 3. 普通点击单选
|
|
648
|
+
// 4. 点击空白处清空
|
|
649
|
+
|
|
650
|
+
@selection-change="(sel) => {
|
|
651
|
+
console.log('选中节点:', sel.nodes.map(n => n.id));
|
|
652
|
+
console.log('选中连线:', sel.edges.map(e => e.id));
|
|
653
|
+
}"
|
|
654
|
+
```
|
|
655
|
+
|
|
656
|
+
---
|
|
657
|
+
|
|
658
|
+
## 交互操作
|
|
659
|
+
|
|
660
|
+
| 操作 | 手势 | 说明 |
|
|
661
|
+
| ---------------- | ---------------------------------------------- | ------------------------------------------------------------------------------------ |
|
|
662
|
+
| 选中节点/连线 | 左键单击 | 重复点击已选中项取消选中;点击空白处清空全部选中 |
|
|
663
|
+
| 多选(点选) | Ctrl(macOS ⌘)+ 单击 | 将节点/连线加入或移出选中集合,支持节点与连线混合多选 |
|
|
664
|
+
| 多选(框选) | Ctrl(macOS ⌘)+ 左键拖动 | 在空白画布拉出矩形选框,**与选框相交的节点**并入当前选中(并集语义;仅节点参与框选) |
|
|
665
|
+
| 拖动单个节点 | 按住节点区域任意处拖动 | 需 `editable` 开启且 `config.nodeDraggable !== false`;拖动未选中的节点会先选中它 |
|
|
666
|
+
| 拖动多个节点 | 拖动任一**已选中**节点 | 选中集合整体移动,所有节点位移一致;松开后选中状态保留 |
|
|
667
|
+
| 画布平移(无限) | 空白处按住左键拖动;或任意位置按住**中键**拖动 | 平移无边界限制;内容与网格一起移动;不改变选中状态 |
|
|
668
|
+
| 缩放 | 滚轮 | 以鼠标位置为中心缩放(0.2x–5x) |
|
|
669
|
+
| 调整节点尺寸 | 选中**单个**节点后拖动 8 个控制点 | 需 `editable` 且 `config.nodeResizable !== false` |
|
|
670
|
+
| 拖拽创建连线 | 从节点手柄拖到目标节点 | 需 `config.showNodeHandles: true` |
|
|
671
|
+
|
|
672
|
+
### 图层管理快捷键(需启用 `enableZOrder`)
|
|
673
|
+
|
|
674
|
+
| 快捷键 | 操作 |
|
|
675
|
+
| ---------------------- | ------ |
|
|
676
|
+
| `Ctrl/Cmd + Shift + ↑` | 上一层 |
|
|
677
|
+
| `Ctrl/Cmd + Shift + ↓` | 下一层 |
|
|
678
|
+
| `Ctrl/Cmd + Shift + ←` | 最底层 |
|
|
679
|
+
| `Ctrl/Cmd + Shift + →` | 最上层 |
|
|
680
|
+
|
|
681
|
+
### 交互优先级说明
|
|
682
|
+
|
|
683
|
+
- **节点与连线重叠处**:点击/拖动作用于**节点**;连线在未被遮挡处正常点击——密集图形下节点始终可选中、可拖动
|
|
684
|
+
- **纯边框节点例外**:这类节点内部是空的,只有**框线**和**标签文字**响应点击(内部空白会让位给下面的连线与画布)——所以从框中穿过的连线依然可以单击选中;拖动纯边框节点请按住框线或标签
|
|
685
|
+
- **选中连线的提升层不参与命中**:`highlightEdgeOnTop` 把选中线画在节点之上纯属视觉,不会抢走节点或其它元素的点击
|
|
686
|
+
- **点击与拖动区分**:左键在空白处的「点击」与「拖动」按小阈值区分(平移 4px / 框选 3px / 节点拖动 3px):小于阈值仍视为点击(清空或切换选中),超过阈值才进入拖动
|
|
687
|
+
|
|
688
|
+
---
|
|
689
|
+
|
|
690
|
+
## 补充说明
|
|
691
|
+
|
|
692
|
+
### 数据同步机制
|
|
693
|
+
|
|
694
|
+
`props.nodes` / `props.edges` 为深度同步——父级整体替换数组、或就地修改字段,组件内都会跟随更新;组件内的拖拽/尺寸/增删改也会经 `update:nodes`、`edge-create`、`edge-update`、`edge-remove` 即时外发。父级把组件发出的数据原样写回不会造成循环或重复重建。
|
|
695
|
+
|
|
696
|
+
加载一整套新数据时,先更新你的 `nodes` / `edges`,再调用 `setData()` 清理组件内残留状态。
|
|
697
|
+
|
|
698
|
+
### 连线与节点的图层顺序
|
|
699
|
+
|
|
700
|
+
默认 `config.edgeLayer: "split"`,绘制顺序为:
|
|
701
|
+
|
|
702
|
+
```
|
|
703
|
+
分组框 → 节点底板(背景 + 边框) → 连线(基色/光效/粒子/文字) → 节点内容(图标/图片/标签/状态点)
|
|
704
|
+
```
|
|
705
|
+
|
|
706
|
+
这样连线穿过节点——或节点互相重叠时——会**压在节点底板上继续可见**,只被图标本体局部遮挡,不会整段消失。若需要传统的「所有连线都在所有节点之下」,设 `edgeLayer: "below"`。
|
|
707
|
+
|
|
708
|
+
配合 `config.highlightEdgeOnTop`(默认 `true`),被选中的连线还会在节点内容之上再画一遍,保证正在查看的链路永远不会被节点挡住。
|
|
709
|
+
|
|
710
|
+
### TypeScript 支持
|
|
711
|
+
|
|
712
|
+
```typescript
|
|
713
|
+
import type { TopologyInstance } from "vue3-network-graph";
|
|
714
|
+
|
|
715
|
+
const topoRef = ref<TopologyInstance | null>(null);
|
|
716
|
+
|
|
717
|
+
// 类型安全的方法调用
|
|
718
|
+
topoRef.value?.getData();
|
|
719
|
+
topoRef.value?.clientToSvg(event);
|
|
720
|
+
```
|
|
721
|
+
|
|
722
|
+
### 坐标系说明
|
|
723
|
+
|
|
724
|
+
- 组件的节点坐标(`node.x` / `node.y`)位于 **SVG 用户坐标系**
|
|
725
|
+
- 画布的缩放与平移由 `viewBox` 实现,因此该坐标系与屏幕像素之间的换算随视图状态变化
|
|
726
|
+
- `clientToSvg` 使用矩阵变换,**自动折算当前缩放与平移**,返回的就是与 `node.x` / `node.y` 同源的坐标
|
|
727
|
+
- `svgToClient` 是它的逆变换:输入节点坐标,返回屏幕 `clientX` / `clientY`,适合与 DOM 浮层配合
|
|
728
|
+
|
|
729
|
+
---
|
|
730
|
+
|
|
731
|
+
## 许可证
|
|
732
|
+
|
|
733
|
+
[MIT](../LICENSE) © 2026 vnodegit
|