vue +antvX6 根据节点与线,动态设置节点坐标生成流程图

本文主要是介绍vue +antvX6 根据节点与线,动态设置节点坐标生成流程图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

需求

vue2 + antvX6完成流程图,但只有节点与线,没有节点的坐标,需要根据节点的顺序显示流程图。
需求:

1.根据数据动态生成对应的节点与线;
2.节点不能重叠;
3.节点与线可拖拽;
4.因为线存在重叠可能,所有鼠标移入时线必须高亮显示(红色),鼠标移出复原;
5.要求有对齐线;
6.线不能与节点重叠(先不能穿过节点)。

效果

图片
在这里插入图片描述
动图
在这里插入图片描述

参数

{"line_data": [ // 线数据{"name": "条件1-1", // 线名称"source_state_id": 6, // 源节点"destination_state_id": 5, // 目标节点"attribute_type_id": 1 // 成功或失败状态},{"name": "条件2-1","source_state_id": 9,"destination_state_id": 6,"attribute_type_id": 1},{"name": "条件2-2","source_state_id": 5,"destination_state_id": 6,"attribute_type_id": 2},{"name": "条件3-1","source_state_id": 10,"destination_state_id": 9,"attribute_type_id": 1},{"name": "条件3-2","source_state_id": 5,"destination_state_id": 9,"attribute_type_id": 2},{"name": "条件4-1","source_state_id": 11,"destination_state_id": 10,"attribute_type_id": 1},{"name": "条件4-2","source_state_id": 5,"destination_state_id": 10,"attribute_type_id": 2},{"name": "条件5-1","source_state_id": 12,"destination_state_id": 11,"attribute_type_id": 1},{"name": "条件5-2","source_state_id": 5,"destination_state_id": 11,"attribute_type_id": 2},{"name": "条件6-1","source_state_id": 13,"destination_state_id": 12,"attribute_type_id": 1},{"name": "条件6-2","source_state_id": 5,"destination_state_id": 12,"attribute_type_id": 2},{"name": "条件7-1","source_state_id": 18,"destination_state_id": 13,"attribute_type_id": 1},{"name": "条件7-2","source_state_id": 5,"destination_state_id": 13,"attribute_type_id": 2},{"name": "条件8-1","source_state_id": 19,"destination_state_id": 6,"attribute_type_id": 3},{"name": "条件8-2","source_state_id": 11,"destination_state_id": 19,"attribute_type_id": 1}],"node_data": [ // 节点数据{"id": 1, // 节点id"name": "开始", // 节点名称"type_id": 1, // 节点状态"order_id": 1 // 节点顺序},{"id": 2,"name": "过程1","type_id": 0,"order_id": 2},{"id": 3,"name": "过程2-1","type_id": 0,"order_id": 3},{"id": 4,"name": "过程2-2","type_id": 0,"order_id": 3},{"id": 5,"name": "过程3","type_id": 0,"order_id": 4},{"id": 6,"name": "过程4","type_id": 0,"order_id": 5},{"id": 7,"name": "过程5","type_id": 0,"order_id": 6},{"id": 8,"name": "过程6","type_id": 0,"order_id": 7},{"id": 9,"name": "结束","type_id": 2,"order_id": 8}]
}

代码

安装插件

1.antvX6
npm install @antv/x6 --save
2.对齐线
npm install @antv/x6-plugin-snapline --save

html代码

<template><div class="info-box"><div class="top-box"id="top-width"><el-button type=""@click="zoomToFit">填满</el-button></div><div class="content-box"><div class="container-box"><div id="container"></div></div></div></div>
</template>

js代码

<script>
import API from '../api' // 接口
import { Graph } from '@antv/x6' // 引入antvX6
import { Snapline } from '@antv/x6-plugin-snapline' // 引入对齐线
Graph.registerNode( // 设置节点基础样式'custom-rect',{inherit: 'rect',width: 200,height: 40,attrs: {body: {strokeWidth: 1,stroke: '#5F95FF',fill: '#EFF4FF'},text: {fontSize: 12,fill: '#262626'}},text: {fontSize: 12,fill: '#262626'}},true
)
export default {data() {return {loading: false,graph: null, // 画布实例对象data: {nodes: [],edges: []}}},mounted() {// 先初始化画布this.initGraph()},beforeDestroy() {// 画布的销毁以及回收this.graph.dispose()this.graph = null},methods: {// 初始化流程图画布initGraph() {const container = document.getElementById('container')this.graph = new Graph({container: container, // 画布容器width: container.offsetWidth, // 画布宽height: container.offsetHeight, // 画布高autoResize: true,background: { // 背景color: '#F2F7FA'},panning: {enabled: true // 支持滚动放大缩小},mousewheel: {enabled: true,modifiers: 'Ctrl', // 按住ctrl按键滚动鼠标滚轮缩放factor: 1.1,maxScale: 10, // 最大放大minScale: 0.05 // 最小缩小},grid: {visible: true, // 渲染网格背景type: 'doubleMesh',args: [{color: '#eee', // 主网格线颜色thickness: 1 // 主网格线宽度},{color: '#ddd', // 次网格线颜色thickness: 1, // 次网格线宽度factor: 4 // 主次网格线间隔}]}})this.graph.use( // 启用对齐线new Snapline({enabled: true}))// 鼠标移入线this.graph.on('edge:mouseenter', ({ e, edge, view }) => {edge.attr({line: {stroke: 'red',strokeWidth: 3}})})// 鼠标移出线this.graph.on('edge:mouseleave', ({ edge }) => {edge.attr({line: {stroke: '#8f8f8f',strokeWidth: 1}})})},// 获取数据init() {this.loading = trueAPI.getData().then(res => {if (res.code === 200) {this.setGraphData(res)} else {this.$message.error(res.msg)}}).finally(() => {this.loading = false})},// 设置画布数据setGraphData(data) {// const X = document.getElementById('top-width').offsetWidth / 2 - 100 // 居中const X = 200this.data = {nodes: [],edges: []}const obj = {}// 转为对象数组 节点有可能顺序相同,顺序相同的配列在同一行data.node_data.map(item => {if (obj[item.order_id]) {obj[item.order_id].push(item)} else {obj[item.order_id] = []obj[item.order_id].push(item)}})// 遍历对象数组  通过遍历数组,将节点数据转为流程图中需要的数据类型Object.keys(obj).forEach((key, objIndex) => {obj[key].map((item, index) => {const node = {id: item.id, // 节点idshape: 'custom-rect', // 这是上边定义的节点类型label: item.name, // 节点名称x: X + 300 * index, // 节点x轴坐标 因为存在顺序相同的节点,需要排在同一行,但是y不一样y: 40 + 100 * objIndex, // 节点y轴坐标 顺序不同的节点,y轴坐标不同attrs: {body: { // 这里是区分普通节点与开始结束节点的, 具体看效果图rx: item.type_id === 0 ? 4 : 10,ry: item.type_id === 0 ? 4 : 10}}}this.data.nodes.push(node)})})// 遍历线的数据 通过遍历数组,将线数据转为流程图中需要的数据类型data.line_data.map((item, index) => {const obj = {id: item.id, // 线idshape: 'edge', // 类型为线source: item.destination_state_id, // 源节点target: item.source_state_id, // 目标节点labels: [ // 线名称样式{attrs: {label: {text: item.name // 线名称}},position: 0.4 // 名称在线的相对位置(0-1)一般为0.5}],router: { // 线的路由name: 'manhattan', // 智能路由 移动节点时,线自动避免与节点接触args: { // 这里根据线的状态来判断线是从源节点的哪里开始,到目标节点的哪里结束// 值为1 线从源节点下方开始,到目标节点上方结束 // 值为2 线从源节点左方开始,到目标节点左方结束 // 值其他 线从源节点右方开始,到目标节点右方结束startDirections: item.attribute_type_id === 1 ? ['bottom'] : item.attribute_type_id === 2 ? ['left'] : ['right'],endDirections: item.attribute_type_id === 1 ? ['top'] : item.attribute_type_id === 2 ? ['left'] : ['right']}},tools: [{name: 'segments',args: {snapRadius: 20,attrs: {fill: '#444'}}}],attrs: { // 线样式line: {stroke: '#8f8f8f',strokeWidth: 1}}}this.data.edges.push(obj)})this.graph.fromJSON(this.data) // 渲染数据 将添加的节点与线画出来},zoomToFit() {this.graph.zoomToFit({padding: 20,preserveAspectRatio: true,maxScale: 1})}}
}
</script>

css代码

<style lang="scss" scoped>
.info-box {position: relative;width: 100%;height: 100%;padding: 1rem;box-sizing: border-box;.top-box {width: 100%;height: 3rem;}.content-box {width: 100%;height: calc(100% - 3rem);.container-box {width: 100%;height: 100%;}}
}
</style>

这篇关于vue +antvX6 根据节点与线,动态设置节点坐标生成流程图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/928504

相关文章

Vue3 的 shallowRef 和 shallowReactive:优化性能

大家对 Vue3 的 ref 和 reactive 都很熟悉,那么对 shallowRef 和 shallowReactive 是否了解呢? 在编程和数据结构中,“shallow”(浅层)通常指对数据结构的最外层进行操作,而不递归地处理其内部或嵌套的数据。这种处理方式关注的是数据结构的第一层属性或元素,而忽略更深层次的嵌套内容。 1. 浅层与深层的对比 1.1 浅层(Shallow) 定义

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

【 html+css 绚丽Loading 】000046 三才归元阵

前言:哈喽,大家好,今天给大家分享html+css 绚丽Loading!并提供具体代码帮助大家深入理解,彻底掌握!创作不易,如果能帮助到大家或者给大家一些灵感和启发,欢迎收藏+关注哦 💕 目录 📚一、效果📚二、信息💡1.简介:💡2.外观描述:💡3.使用方式:💡4.战斗方式:💡5.提升:💡6.传说: 📚三、源代码,上代码,可以直接复制使用🎥效果🗂️目录✍️

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

第10章 中断和动态时钟显示

第10章 中断和动态时钟显示 从本章开始,按照书籍的划分,第10章开始就进入保护模式(Protected Mode)部分了,感觉从这里开始难度突然就增加了。 书中介绍了为什么有中断(Interrupt)的设计,中断的几种方式:外部硬件中断、内部中断和软中断。通过中断做了一个会走的时钟和屏幕上输入字符的程序。 我自己理解中断的一些作用: 为了更好的利用处理器的性能。协同快速和慢速设备一起工作

AI一键生成 PPT

AI一键生成 PPT 操作步骤 作为一名打工人,是不是经常需要制作各种PPT来分享我的生活和想法。但是,你们知道,有时候灵感来了,时间却不够用了!😩直到我发现了Kimi AI——一个能够自动生成PPT的神奇助手!🌟 什么是Kimi? 一款月之暗面科技有限公司开发的AI办公工具,帮助用户快速生成高质量的演示文稿。 无论你是职场人士、学生还是教师,Kimi都能够为你的办公文

动态规划---打家劫舍

题目: 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。 给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。 思路: 动态规划五部曲: 1.确定dp数组及含义 dp数组是一维数组,dp[i]代表

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

pdfmake生成pdf的使用

实际项目中有时会有根据填写的表单数据或者其他格式的数据,将数据自动填充到pdf文件中根据固定模板生成pdf文件的需求 文章目录 利用pdfmake生成pdf文件1.下载安装pdfmake第三方包2.封装生成pdf文件的共用配置3.生成pdf文件的文件模板内容4.调用方法生成pdf 利用pdfmake生成pdf文件 1.下载安装pdfmake第三方包 npm i pdfma

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n