绘制流程图

场景:不同的审批流程,对应不同的流程图。通过动态获取流程数据,动态绘制流程图

流程数据类似于:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
[
{
"nextname": "公司领导审批",
"currNodeCode": "377",
"id": 21256,
"currname": "业务管理中心领导审批",
"nextNodeCode": "389"
},
{
"nextname": "部门科室主管或县市部门领导审批",
"currNodeCode": "999",
"id": 21260,
"currname": "退回发起人",
"nextNodeCode": "102"
},
{
"nextname": "发起人确认",
"currNodeCode": "389",
"id": 21262,
"currname": "公司领导审批",
"nextNodeCode": "108"
},
{
"nextname": "部门科室主管或县市部门领导审批",
"currNodeCode": "100",
"id": 21245,
"currname": "发起人申请",
"nextNodeCode": "102"
},
{
"nextname": "政企部内分管领导",
"currNodeCode": "441",
"id": 21251,
"currname": "政企业务接口人",
"nextNodeCode": "442"
},
{
"nextname": "业务管理中心领导审批",
"currNodeCode": "442",
"id": 21253,
"currname": "政企部内分管领导",
"nextNodeCode": "377"
},
{
"nextname": "发起人确认",
"currNodeCode": "377",
"id": 21255,
"currname": "业务管理中心领导审批",
"nextNodeCode": "108"
},
{
"nextname": "流程正常归档",
"currNodeCode": "108",
"id": 21258,
"currname": "发起人确认",
"nextNodeCode": "888"
},
{
"nextname": "部门分管领导审批",
"currNodeCode": "102",
"id": 21246,
"currname": "部门科室主管或县市部门领导审批",
"nextNodeCode": "153"
},
{
"nextname": "发起人确认",
"currNodeCode": "153",
"id": 21248,
"currname": "部门分管领导审批",
"nextNodeCode": "108"
},
{
"nextname": "政企业务接口人",
"currNodeCode": "153",
"id": 21249,
"currname": "部门分管领导审批",
"nextNodeCode": "441"
}
]

主要功能点:自动生成流程节点、自动排列、点击全屏查看、缩放、整体拖动

主要技术:@antv/x6 和@antv/layout

一、安装插件

@antv/x6 使用的是 2.18.1 版本,@antv/layout 对应的需要手动选择 0.3.25 版本,而不是最新版。否则其 layout 会报错,这是插件本身版本的问题

完整代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<template>
<div class="flow-chart-container">
<div ref="container" class="flow-chart" @click="openFullscreen"></div>
<!-- 全屏预览模态框 -->
<div
v-if="isFullscreen"
class="fullscreen-preview"
@click.self="closeFullscreen"
>
<div class="preview-content">
<div ref="fullscreenContainer" class="fullscreen-chart"></div>
<div class="preview-toolbar">
<el-button @click="zoomIn" circle
><el-icon> <ZoomIn /> </el-icon
></el-button>
<el-button @click="zoomOut" circle
><el-icon> <ZoomOut /> </el-icon
></el-button>
<el-button @click="zoomToFit" circle
><el-icon> <FullScreen /> </el-icon
></el-button>
<el-button @click="closeFullscreen" circle
><el-icon> <Close /> </el-icon
></el-button>
<span class="zoom-level">{{ zoomPercentage }}%</span>
</div>
</div>
</div>
</div>
</template>

<script setup>
import { Graph } from '@antv/x6'
import { DagreLayout } from '@antv/layout'
import { ref, defineProps, onMounted, watch, computed, nextTick } from 'vue'
import { ZoomIn, ZoomOut, FullScreen, Close } from '@element-plus/icons-vue'
const props = defineProps({
flowData: {
type: Array,
default: () => [],
},
})
const fullscreenContainer = ref(null)
const isFullscreen = ref(false)
const fullscreenGraph = ref(null)
const currentZoom = ref(1)
const zoomPercentage = computed(() => {
return Math.round(currentZoom.value * 100)
})
// 初始化全屏视图的图表
const initFullscreenGraph = () => {
if (!fullscreenContainer.value) return

fullscreenGraph.value = new Graph({
container: fullscreenContainer.value,
history: false,
selecting: { enabled: true },
panning: {
enabled: true,
},
mousewheel: {
enabled: true,
minScale: 0.2,
maxScale: 3,
},
background: { color: '#f5f5f5' },
})

// 监听缩放事件
fullscreenGraph.value.on('scale', ({ sx }) => {
currentZoom.value = sx
})

renderGraph(fullscreenGraph.value)
fullscreenGraph.value.zoomToFit({ padding: 20 })
}
// 打开全屏预览
const openFullscreen = () => {
isFullscreen.value = true
nextTick(() => {
initFullscreenGraph()
})
}

// 关闭全屏预览
const closeFullscreen = () => {
isFullscreen.value = false
if (fullscreenGraph.value) {
fullscreenGraph.value.dispose()
fullscreenGraph.value = null
}
}

// 缩放控制
const zoomIn = () => {
if (fullscreenGraph.value) {
const current = fullscreenGraph.value.zoom()
const newZoom = Math.min(current + 0.2, 3)
fullscreenGraph.value.zoomTo(newZoom)
}
}

const zoomOut = () => {
if (fullscreenGraph.value) {
const current = fullscreenGraph.value.zoom()
const newZoom = Math.max(current - 0.2, 0.2)
fullscreenGraph.value.zoomTo(newZoom)
}
}

const zoomToFit = () => {
if (fullscreenGraph.value) {
fullscreenGraph.value.zoomToFit({ padding: 20 })
}
}
function processFlowData(flowData) {
const nodes = []
const edges = []
const nodeMap = new Map()

// 收集所有节点和边
flowData.forEach((item) => {
// 当前节点
if (!nodeMap.has(item.curr_node_code)) {
nodes.push({
id: item.curr_node_code,
label: item.curr,
width: 120,
height: 40,
})
nodeMap.set(item.curr_node_code, true)
}

// 下一个节点
if (item.next_node_code && !nodeMap.has(item.next_node_code)) {
nodes.push({
id: item.next_node_code,
label: item.nextn,
width: item.nextn.length * 14,
height: 40,
})
nodeMap.set(item.next_node_code, true)
}

// 添加边
if (item.next_node_code) {
edges.push({
source: item.curr_node_code,
target: item.next_node_code,
labels: item.express_name ? [{ text: item.express_name }] : [],
})
}
})

const dagreLayout = new DagreLayout({
type: 'dagre',
rankdir: 'LR', // 布局方向:从左到右
align: 'UL', // 对齐方式
ranksep: 40, // 层级间距
nodesep: 30, // 节点间距
controlPoints: true, // 显示边控制点
})

const layoutData = dagreLayout.layout({
nodes: nodes.map((node) => ({ ...node, size: [node.width, node.height] })),
edges,
})

// 转换布局后的数据为X6格式
return {
nodes: layoutData.nodes.map((node) => ({
id: node.id,
x: node.x,
y: node.y,
label: node.label,
type: 'rect',
width: node.width,
height: node.height,
attrs: {
body: {
fill: '#fff',
stroke: '#9254de',
strokeWidth: 2,
rx: 5,
ry: 5,
},
label: {
fontSize: 12,
fill: '#333',
},
},
})),
edges: layoutData.edges.map((edge) => ({
source: edge.source,
target: edge.target,
attrs: {
line: {
stroke: '#a0a0a0',
strokeWidth: 2,
targetMarker: {
name: 'block',
size: 6,
},
},
},
labels: edge.labels
? edge.labels.map((label) => ({
attrs: {
label: {
text: label.text,
fontSize: 10,
},
},
}))
: [],
})),
}
}
onMounted(() => {
init()
})
watch(
() => props.flowData,
() => {
renderGraph(graph.value)
graph.value.zoomToFit({ padding: 20, maxScale: 1 })
},
{ deep: true }
)
const container = ref(null)
const graph = ref(null)
const init = () => {
if (!container.value) return
graph.value = new Graph({
container: container.value,
history: false, // 禁用历史记录
selecting: {
enabled: false, // 禁用选择
rubberband: false, // 禁用框选
},
panning: {
enabled: true, // 启用平移
},
scroller: {
enabled: false,
},
minimap: {
enabled: true,
container: container.value,
},
background: {
color: '#ffff',
},
grid: {
size: 10,
visible: false,
},
interacting: {
// 禁用交互
nodeMovable: false, // 禁止节点移动
edgeMovable: false, // 禁止边移动
edgeLabelMovable: false, // 禁止边标签移动
arrowheadMovable: false, // 禁止箭头移动
vertexMovable: false, // 禁止顶点移动
vertexAddable: false, // 禁止添加顶点
vertexDeletable: false, // 禁止删除顶点
useEdgeTools: false, // 禁止使用边工具
},
})

renderGraph(graph.value)
// 自动调整视图
graph.value.zoomToFit({ padding: 20, maxScale: 1 })
}
const renderGraph = (graph) => {
if (!graph) return

graph.clearCells()
const data = props.flowData.filter((item) => item.curr_node_code !== '999')
const { nodes, edges } = processFlowData(data)

// 添加节点
nodes.forEach((node) => {
graph.addNode({
id: node.id,
shape: node.type || 'rect',
x: node.x,
y: node.y,
width: node.width,
height: node.height,
label: node.label,
attrs: node.attrs,
})
})

// 添加边
edges.forEach((edge) => {
graph.addEdge({
source: edge.source,
target: edge.target,
attrs: edge.attrs,
labels: edge.labels,
// connector: 'rounded',
router: {
name: 'manhattan',
},
})
})
}
</script>
<style scoped lang="less">
.flow-chart-container {
width: 100%;
height: 300px;
border: 1px solid #e8e8e8;
border-radius: 4px;
overflow: hidden;
position: relative;
cursor: pointer;

&:hover {
box-shadow: 0 0 8px rgba(0, 0, 0, 0.1);
}
}

.flow-chart {
width: 100%;
height: 100%;
}

.fullscreen-preview {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
z-index: 9999;
display: flex;
justify-content: center;
align-items: center;

.preview-content {
width: 90%;
height: 90%;
background: white;
border-radius: 4px;
display: flex;
flex-direction: column;

.fullscreen-chart {
flex: 1;
width: 100%;
}

.preview-toolbar {
padding: 10px;
background: #f5f5f5;
border-top: 1px solid #e8e8e8;
display: flex;
align-items: center;
justify-content: center;
gap: 10px;

.zoom-level {
margin-left: 15px;
font-size: 14px;
color: #666;
}
}
}
}
</style>

其他更多功能配置参考官网https://x6.antv.antgroup.com/tutorial/getting-started和DeepSeek官网https://chat.deepseek.com/

二、最终效果

image-20250428160841656

全屏预览

image-20250428160859700

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2023-2025 congtianfeng
  • 访问人数: | 浏览次数: