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

Vue3 与 AntV X6 节点传参、自动布局及边颜色控制教程

以下是整理后的 AntV X6 + Vue 3 节点开发全流程指南,包含完整代码示例和关键实现细节:

环境搭建与项目初始化

使用 Vite 快速创建 Vue 3 项目:

npm create vite@latest x6-vue-demo
cd x6-vue-demo
npm install

安装核心依赖:

npm install @antv/x6 @antv/layout @antv/x6-vue-shape element-plus

配置 Element Plus(在 main.js/ts 中):

import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')

自定义节点组件开发

创建 NodeView.vue 组件:

<template><div class="node" @click="handleClick"><div class="node-title">{{ nodeData.name }}</div><div class="node-status">状态: {{ nodeData.status || "未执行" }}</div></div>
</template><script setup>
import { ref, onBeforeUnmount } from "vue"
const props = defineProps({ getNode: Function })
const node = props.getNode()
const nodeData = ref(node ? node.getData() : {})const handleChange = ({ current }) => {nodeData.value = { ...current }
}
if (node) node.on("change:data", handleChange)onBeforeUnmount(() => {if (node) node.off("change:data", handleChange)
})const handleClick = () => {alert(`点击了: ${nodeData.value.name}`)
}
</script><style scoped>
.node {width: 120px;height: 50px;background: #409eff;color: #fff;border-radius: 6px;display: flex;flex-direction: column;align-items: center;justify-content: center;font-size: 12px;cursor: pointer;
}
.node-title { font-weight: bold; }
.node-status { font-size: 10px; margin-top: 4px; }
</style>

主页面实现

创建 GraphDemo.vue 核心逻辑:

<template><div><el-button type="primary" @click="setStatus('成功')">父节点成功</el-button><el-button type="danger" @click="setStatus('失败')">父节点失败</el-button><el-button @click="setStatus('等待')">父节点等待</el-button><div class="graph-container" ref="graphRef"></div><TeleportContainer /></div>
</template><script setup>
import { ref, onMounted, defineComponent } from "vue"
import { Graph } from "@antv/x6"
import { register, getTeleport } from "@antv/x6-vue-shape"
import { DagreLayout } from "@antv/layout"
import NodeView from "../components/NodeView.vue"const graphRef = ref(null)
let graph = null
const TeleportContainer = defineComponent(getTeleport())const initGraph = () => {register({shape: "vue-node",component: NodeView,getComponentProps(node) {return { getNode: () => node }}})graph = new Graph({container: graphRef.value,background: { color: "#f9f9f9" },grid: true})const taskList = [{ id: "1", name: "任务A", status: "等待" },{ id: "2", name: "任务B", status: "等待" },{ id: "3", name: "任务C", status: "等待" }]const relations = [{ source: "1", target: "2" },{ source: "1", target: "3" }]const nodes = taskList.map((task) => ({id: task.id,shape: "vue-node",width: 120,height: 50,data: task}))const edges = relations.map((rel) => ({source: rel.source,target: rel.target,attrs: {line: {stroke: "#999",strokeWidth: 2,targetMarker: { name: "classic", width: 12, height: 8 }}}}))const layout = new DagreLayout({type: "dagre",rankdir: "LR",nodesep: 40,ranksep: 60})graph.fromJSON(layout.layout({ nodes, edges }))updateEdgesColor()
}const updateEdgesColor = () => {graph.getEdges().forEach((edge) => {const sourceNode = graph.getCellById(edge.getSourceCellId())const status = sourceNode?.getData()?.status || "等待"edge.setAttrs({line: { stroke: status === "成功" ? "green" :status === "失败" ? "red" : "gray"}})})
}const setStatus = (status) => {const parentNode = graph.getCellById("1")parentNode.setData({ ...parentNode.getData(), status })updateEdgesColor()
}onMounted(() => initGraph())
</script><style scoped>
.graph-container {width: 100%;height: 500px;border: 1px solid #ddd;margin-top: 10px;
}
</style>

关键功能说明

自动布局实现
通过 DagreLayout 自动计算节点位置,参数说明:

  • rankdir: "LR" 控制布局方向(Left to Right)
  • nodesepranksep 控制节点间距

动态边颜色控制
updateEdgesColor 方法根据源节点状态改变连线颜色:

  • 成功状态:绿色边
  • 失败状态:红色边
  • 默认状态:灰色边

节点数据响应式
通过监听 change:data 事件实现节点状态实时更新,注意在组件卸载时移除事件监听

效果说明

运行后可见:

  1. 三个采用自动布局排列的节点
  2. 父节点(任务A)控制两个子节点的边颜色
  3. 点击按钮可切换父节点状态(成功/失败/等待)
  4. 点击节点会显示节点名称提示框

文章转载自:

http://9iKaSYjh.hpprx.cn
http://WAnvv93X.hpprx.cn
http://0c3U0oWY.hpprx.cn
http://rEFpXEAn.hpprx.cn
http://8WP4RnGB.hpprx.cn
http://yLsFHPcA.hpprx.cn
http://CsFF4A3l.hpprx.cn
http://hX9HAP6t.hpprx.cn
http://2DGQDUNz.hpprx.cn
http://Kz4TSYmj.hpprx.cn
http://Cj0WKOdO.hpprx.cn
http://jA5AYsvI.hpprx.cn
http://TAA4XI93.hpprx.cn
http://ABHqrS8L.hpprx.cn
http://efG054GM.hpprx.cn
http://SLY5y9FS.hpprx.cn
http://eJbhKFQM.hpprx.cn
http://SR7QYKSb.hpprx.cn
http://QYBKFAUl.hpprx.cn
http://rqZZKs1H.hpprx.cn
http://gKe3vpTb.hpprx.cn
http://u9utZWYy.hpprx.cn
http://GVLBFR0e.hpprx.cn
http://v8T1PnWy.hpprx.cn
http://S884AYna.hpprx.cn
http://43OllqEY.hpprx.cn
http://TvQ7xLd8.hpprx.cn
http://Wvxw6h4z.hpprx.cn
http://HMfFqta5.hpprx.cn
http://TSRXcJBK.hpprx.cn
http://www.dtcms.com/a/374761.html

相关文章:

  • 线程与进程的区别
  • RAC概念笔记
  • 如何将视频从安卓手机传输到电脑?
  • Day04_苍穹外卖——套餐管理(实战)
  • ElementUI 组件概览
  • fifo之读写指针
  • 【第三次全国土壤普查】一键制备土壤三普环境变量23项遥感植被指数神器
  • Java反射机制详解
  • PDF文件中的广告二维码图片该怎么批量删除
  • 记一次 .NET 某中医药附属医院门诊系统 崩溃分析
  • WPF/Prism 中计算属性的通知机制详解 —— SetProperty 与 RaisePropertyChanged
  • jmeter使用指南
  • 硬件(六)arm指令
  • 后端错误处理的艺术:BusinessException 与 ResultUtils 的完美分工
  • MCU、CPLD、DSP、FPGA 有什么区别,该如何选择?
  • 【React Native】点赞特效动画组件FlowLikeView
  • android studio gradle 访问不了
  • 【C++】C++11 篇二
  • Kubernetes 配置检查与发布安全清单
  • Perforce Klocwork 2025.2版本更新:默认启用现代分析引擎、支持 MISRA C:2025 新规、CI构建性能提升等
  • 工业总线协议转换核心:SG-DP_MOD-110 Profibus-DP 转 Modbus-RTU 网关,打通异构设备数据链路
  • Win系统下配置PCL库第三步之链接库的路径(超详细)
  • 【远程运维】Linux 远程连接 Windows 好用的软件:MobaXterm 实战指南
  • Java入门级教程13-多线程同步安全机制synchronized(内置锁)、JavaMail发送电子邮箱、爬取CSDN到邮箱、备份数据库
  • 玩转Docker | 使用Docker部署KissLists任务管理工具
  • STL库——map/set(类函数学习)
  • STM32 串口接收数据包(自定义帧头帧尾)
  • 正向代理,反向代理,负载均衡还有nginx
  • 用户态与内核态的深度解析:安全、效率与优化之道
  • 搭建本地gitea服务器