当前位置: 首页 > news >正文

基于 vue-flow 实现可视化流程图

vue-flow 是一个基于 Vue.js 的强大且灵活的可视化流程图库,它允许开发者轻松创建交互式的流程图、工作流图、节点图等。

主要特点

  1. 易于使用 :提供了简洁的 API 和组件,开发者可以快速上手并创建复杂的流程图。
  2. 高度可定制 :支持自定义节点、边、连接点等元素的样式和行为,能够满足不同项目的设计需求。
  3. 交互功能丰富 :内置了节点拖拽、连接、缩放、平移等交互功能,增强用户体验。
  4. 响应式设计 :能够自适应不同的屏幕尺寸和容器大小,保证在各种设备上都有良好的显示效果。
  5. 性能优化 :采用了高效的渲染机制,即使在处理大量节点和边时也能保持流畅的性能。

组件

  • VueFlow :主组件,用于包裹整个流程图区域,负责管理流程图的状态和交互。
  • Node :表示流程图中的节点,可以自定义节点的内容和样式。
  • Edge :表示节点之间的连接边,支持不同的连接类型和样式。
  • Handle :连接点组件,用于定义节点上的输入和输出点,用户可以通过拖拽连接点来创建边。
  • @vue-flow/background 用于在流程图的背景添加网格或图案,增强流程图的视觉效果,方便用户更直观地定位节点和边。
  • @vue-flow/controls 用于控制流程图视图的工具按钮,包含缩放、平移等常用操作。
  • @vue-flow/minimap 用于显示流程图的缩略图,方便用户在处理大型复杂流程图时快速导航到指定区域。

安装

# 创建vue项目
yarn create vue@latest
cd <your-project-name># 安装依赖
yarn
yarn add @vue-flow/core
yarn add @vue-flow/minimap
yarn add @vue-flow/controls
yarn add @vue-flow/background# 运行
yarn dev

使用示例

index.html

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><link rel="icon" href="/favicon.ico" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Vite App</title></head><body><div id="app"></div><script type="module" src="/src/main.js"></script></body>
</html>

main.js

import { createApp } from "vue";
import "./assets/index.css";
import App from "./App.vue";createApp(App).mount("#app");

index.css

/* import the required styles */
@import '@vue-flow/core/dist/style.css';/* import the default theme (optional) */
@import '@vue-flow/core/dist/theme-default.css';@import '@vue-flow/controls/dist/style.css';
@import '@vue-flow/minimap/dist/style.css';body {color: #111;padding: 5px;
}#app {text-transform: uppercase;font-weight: 600;font-family: 'JetBrains Mono', monospace;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;text-align: center;color: #2c3e50;
}html,
body,
#app {margin: 0;height: 100%;
}

App.vue

<script setup>
import { h, ref } from 'vue'
import { Background } from '@vue-flow/background'
import { Controls } from '@vue-flow/controls'
import { MiniMap } from '@vue-flow/minimap'
import { VueFlow, useVueFlow } from '@vue-flow/core'
import CustomNode from './CustomNode.vue'
import CustomEdge from './CustomEdge.vue'const { onConnect, addEdges } = useVueFlow()const nodes = ref([{ id: '1', type: 'input', label: 'Node 1', position: { x: 250, y: 5 } },{ id: '2', type: 'output', label: 'Node 2', position: { x: 100, y: 100 } },{ id: '3', type: 'custom', label: 'Node 3', position: { x: 400, y: 100 } },
])const edges = ref([{ id: 'e1-2', source: '1', target: '2', type: 'custom' },{ id: 'e1-3', source: '1', target: '3', animated: true },
])onConnect((params) => {addEdges([params])
})
</script><template><div style="height: 100vh"><VueFlowv-model:nodes="nodes"v-model:edges="edges"fit-view-on-initclass="vue-flow-basic-example":default-zoom="1.5":min-zoom="0.2":max-zoom="4"><Background pattern-color="#aaa" :gap="8" /><MiniMap /><Controls /><template #node-custom="nodeProps"><CustomNode v-bind="nodeProps" /></template><template #edge-custom="edgeProps"><CustomEdge v-bind="edgeProps" /></template></VueFlow></div>
</template>

CustomNode.vue

<script setup>
import { Handle, Position } from '@vue-flow/core'
import { ref } from 'vue'const counter = ref(0)
</script><template><div class="custom-node"><Handle type="target" :position="Position.Top" /><button class="increment nodrag" @click="counter++">Increment</button><div v-if="counter > 0" class="counter"><div class="count" v-for="count of counter" :key="`count-${count}`">{{ count }}</div></div></div>
</template><style>
.custom-node {min-width: 100px;gap: 4px;padding: 8px;background: white;border: 1px solid black;border-radius: 4px;
}.increment {border-radius: 4px;background: #42b983;font-size: 10px;color: #fff;cursor: pointer;border: none;
}.increment:hover {box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
}.counter {margin-top: 8px;display: grid;grid-template-columns: repeat(4, minmax(0, 1fr));gap: 4px;
}.count {font-size: 6px;color: #ff0072;border: 1px solid rgba(0, 0, 0, 0.3);border-radius: 8px;
}
</style>

CustomEdge.vue

<script setup>
import { computed } from 'vue'
import { BaseEdge, EdgeLabelRenderer, getBezierPath, useVueFlow } from '@vue-flow/core'const props = defineProps({id: {type: String,required: true,},sourceX: {type: Number,required: true,},sourceY: {type: Number,required: true,},targetX: {type: Number,required: true,},targetY: {type: Number,required: true,},sourcePosition: {type: String,required: true,},targetPosition: {type: String,required: true,},data: {type: Object,required: false,},markerEnd: {type: String,required: false,},style: {type: Object,required: false,},
})const { removeEdges } = useVueFlow()const path = computed(() => getBezierPath(props))
</script><script>
export default {inheritAttrs: false,
}
</script><template><BaseEdge :path="path[0]" /><EdgeLabelRenderer><div:style="{pointerEvents: 'all',position: 'absolute',transform: `translate(-50%, -50%) translate(${path[1]}px,${path[2]}px)`,}"class="nodrag nopan"><button class="edgebutton" @click="removeEdges(id)">×</button></div></EdgeLabelRenderer>
</template><style>
.edgebutton {border-radius: 999px;cursor: pointer;
}.edgebutton:hover {box-shadow: 0 0 0 2px pink, 0 0 0 4px #f05f75;
}
</style>

效果图

1

相关链接

https://vueflow.dev/

http://www.xdnf.cn/news/269695.html

相关文章:

  • 第7章 【Python数据类型大爆炸】Python 基础语法和数据类型特性的实例
  • “c++11“,右值,右值引用,可变参数模板...
  • GPU集群监控系统开发实录:基于Prometheus+Grafana的算力利用率可视化方案
  • 第15章 对API的身份验证和授权
  • 论系统安全架构设计及其应用
  • 【KWDB 创作者计划】使用Docker实现KWDB数据库的快速部署与配置
  • vLLM 本地部署Qwen大模型
  • ES6语法
  • 【大模型面试每日一题】Day 7:为什么大模型训练选择 Adam 而非 SGD?Adam 的关键改进是什么?
  • 被低估的AI+数据标注
  • DeepSeek辅助学术写作之修订与校稿以及发表与推广相关提示词分享祝你顺利毕业~
  • 介绍最前沿的人工智能创新,‘无反向传播’神经网络训练方法?
  • 53、【OS】【Nuttx】编码规范解读(一)
  • [蓝桥杯真题题目及解析]2025年C++b组
  • 计组复习笔记 3
  • 《计算机系统结构》考题知识点整理
  • 经典算法 求解台阶问题
  • 【深度学习-Day 4】掌握深度学习的“概率”视角:基础概念与应用解析
  • AUTOSAR图解==>AUTOSAR_SRS_CoreTest
  • Python----卷积神经网络(LeNet-5的手写体识别)
  • 降维大合集
  • 使用PageHelper实现分页查询(详细)
  • 【多线程】计算机工作原理、操作系统(内含进程、PCB属性、进程调度、内存分配、进程间的通信) —— 简单介绍
  • Nginx相关知识
  • Space Engineers 太空工程师 [DLC 解锁] [Steam] [Windows]
  • 突破养生误区迷障,开启科学养生新程
  • Pytorch-CUDA版本环境配置
  • 实验-组合电路设计1-全加器和加法器(数字逻辑)
  • 冒泡排序详解:从零理解其核心思想与循环设计原理
  • 【信息系统项目管理师-论文真题】2012下半年论文详解(包括解题思路和写作要点)