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