antv x6使用(支持节点排序、新增节点、编辑节点、删除节点、选中节点)

news2025/4/15 17:23:32

项目需要实现如下效果流程图,功能包括节点排序、新增节点、编辑节点、删除节点、选中节点等

html部分如下: 

<template>
  <div class="MindMapContent">
    <el-button size="small" @click="addNode">新增节点</el-button>
    <el-button size="small" @click="updateNode">编辑节点</el-button>
    <el-button size="small" type="danger" plain @click="removeNode">删除节点</el-button>
    <div id="mindContent" style="height: 300px">
      <div id="container"></div>
    </div>

    <el-dialog v-model="visible" :title="pageType == 'edit' ? '编辑' : '新增'">
      <div>
        <el-form class="search-form" ref="formData" size="small" label-width="120px" :model="formData">
          <el-form-item label="节点名称" prop="label" :rules="[{required: true, message: '请输入节点名称',trigger: 'blur'}]">
            <el-input v-model="formData.label" style="width: 60%"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
          <el-button @click="cancelDialog">返回</el-button>
          <el-button type="primary" @click="submitData">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

需要后后端返回的数据格式如下:

mindData: {
    edgeList: [
        {source: '50',target: '54'},
        {source: '50',target: '61'},
        {source: '54',target: '66'},
        {source: '61',target: '67'},
        {source: '67',target: '69'},
        {source: '50',target: '71'},
    ],
    nodeList: [
        {id: '50', label: '根节点'},
        {id: '54', label: '111'},
        {id: '61', label: '222'},
        {id: '66', label: '333'},
        {id: '67', label: '444'},
        {id: '69', label: '555'},
        {id: '71', label: '666'},
     ]
}

        获取后端返回数据后, 需要为节点和边设置样式,所以需要对数据进行处理。x6图最好只加载一次,后续再进行操作时只需要更新数据即可。因为在项目中可以为某个节点绑定其它属性id,绑定后仍保持选中状态,所以设置selectNodeId,当有selectNodeId时,需要选中node.id为selectNodeId的节点

    //获取节点数据
    getNodeData(bool,selectNodeId){
        this.objData.nodes = (this.mindData.nodeList || []).map(item => {
          return {
            id: item.id, // String,可选,节点的唯一标识
            width: 120,   // Number,可选,节点大小的 width 值
            height: 30,  // Number,可选,节点大小的 height 值
            label: item.label, // String,节点标签
            data: {
              portalId: item.portalId || '',
            },
            attrs: {
              body: {
                stroke: 'rgba(238, 238, 238, 1)',
                strokeWidth: 1,
                rx: 5,
                ry: 5,
                style: {
                  filter: 'drop-shadow(0px 0px 8px rgba(0,0,0,0.07))'
                }
              },
              label: {
                fontSize: 12,
                textWrap: {
                  ellipsis: true,
                  width: 105
                }
              }
            }
          }
        })
        this.objData.edges = (this.mindData.edgeList || []).map(item => {
          return{
            source: item.source, // String,必须,起始节点 id
            target: item.target, // String,必须,目标节点 id
            router: {
              name: 'manhattan',
              args: {
                startDirections: ['right'],
                endDirections: ['left']
              }
            },
            attrs: {
              line: {
                stroke: '#1d6ee4'
              }
            }
          }
        })
        //初始化加载mind,更新数据时不初始化mind
        if(bool){
          this.initGraph()
        }else {
          this.graph.cleanSelection()
          this.nowData = {}
          //更新节点信息后重新布局 
          let gridLayout = new DagreLayout({
            type: 'dagre',
            rankdir: 'LR',
            align: undefined,
            ranksep: 45,
            nodesep: 5,
          })

          this.graph.fromJSON(gridLayout.layout(this.objData))

          //如果有selectNodeId,则选中node.id为selectNodeId的节点
          if (selectNodeId) {
            const node = this.graph.getCellById(selectNodeId)
            if (node) {
              this.graph.resetSelection(node)
              this.nowData = {
                id: node.id,
                label: node.label,
                portalId: node.data.portalId || ''
              }
              //返回选中的数据  
              this.$emit('getData', this.nowData)
            }
          }
        }
    },

 初始化画布

// 初始化流程图画布
    initGraph() {
      let container = document.getElementById('container')
      this.graph = null
      this.graph = new Graph({
        container,
        width: '100%',
        height: '100%',
        //最大最小缩放比例
        scaling: {
          min: 0.7,
          max: 1.2
        },
        autoResize: true,
        panning: true,
        mousewheel: true,
        background: {
          color: '#ffffff', // 设置画布背景颜色
        },
      })

      //使用布局插件自动布局  
      let gridLayout = new DagreLayout({
        type: 'dagre',
        rankdir: 'LR',
        align: undefined,
        ranksep: 45,
        nodesep: 5,
      })
      //渲染布局数据
      this.graph.fromJSON(gridLayout.layout(this.objData))

      //使用x6选中插件
      this.graph.use(
          new Selection({
            enabled: true,
            multiple: false,
            movable: false,
            rubberband: false,
            showNodeSelectionBox: true,
            clearSelectionOnBlank: false
          })
      )

      //节点点击选中  
      this.graph.on('node:click', ({ e,node }) => {
        e.stopPropagation()
        tooltip.style.display = 'none'
        this.graph.resetSelection(node)
        this.nowData = {
          id: node.id,
          label: node.label,
          portalId: node.data.portalId || ''
        }
        this.$emit('getData',this.nowData)
      })
      //点击节点外清空点击数据  
      this.graph.on('blank:click', ({ e,node }) => {
        this.graph.cleanSelection()
        this.nowData = {}
      })

      //node节点有宽度限制,label超过宽度时显示...,但是需要tooltip显示完整的label 
      const tooltip = document.createElement('div')
      tooltip.className = 'x6-tooltip'
      tooltip.style.position = "absolute"
      tooltip.style.display = 'none'
      tooltip.style.padding = '6px'
      tooltip.style.borderRadius = '5px'
      tooltip.style.backgroundColor = '#303133'
      tooltip.style.color = '#ffffff'
      tooltip.style.fontSize = '12px'
      let mindContent = document.getElementById('mindContent')
      mindContent.appendChild(tooltip)
      this.graph.on('node:mouseenter', ({ node }) => {
        if(node.label){
          const position = this.graph.localToGraph(node.getBBox().getCenter())
          tooltip.style.display = 'block'
          tooltip.style.left = `${position.x - 60}px`
          tooltip.style.top = `${position.y - 50}px`
          tooltip.textContent = node.label
        }
      })
      this.graph.on('node:mouseleave', ({ node }) => {
        tooltip.style.display = 'none'
      })
    },

节点操作

    //删除节点
    removeNode(){
      if(!this.nowData.id){
        this.$message.error('请选择需要删除的节点')
      }else{
        this.mindData.nodeList = this.mindData.nodeList.filter(item => item.id != this.nowData.id)
        this.mindData.edgeList = this.mindData.edgeList.filter(item => item.target != this.nowData.id)
        this.getNodeData(false)
      }
    },
    //新增节点
    addNode(){
      this.formData = {}
      this.pageType = 'add'
      if (this.objData.nodes.length == 0){
        this.visible = true
      } else{
        if(!this.nowData.id){
          this.$message.error('请选择父节点')
        }else{
          this.visible = true
        }
      }
    },
    //编辑节点
    updateNode(){
      this.formData = {}
      this.pageType = 'edit'
      if(!this.nowData.id){
        this.$message.error('请选择编辑的节点')
      }else{
        this.formData = this.nowData
        this.visible = true
      }
    },

新增节点和编辑节点弹窗操作

    //cancelDialog
    cancelDialog(){
      this.visible = false
    },
    submitData(){
      // 新增的时候,formData就是新增本身,nowData就是父节点
      // 编辑的时候,获取到nowData,赋值给formData
      this.$refs.formData.validate(valid => {
        if(valid){
          if (this.pageType == 'edit'){
            let obj = this.mindData.nodeList.find(item => item.id == this.formData.id)
            obj.label = this.formData.label
            this.visible = false
            this.getNodeData(false)
          }else{
            let id = Math.random().toString(36).substring(2, 4)
            this.mindData.nodeList.push({
              id, label: this.formData.label,
            })
            this.mindData.edgeList.push({
              target: id, source: this.nowData.id,
            })
            this.visible = false
            this.getNodeData(false)
          }
        }
      })
    },

涉及的样式

<style scoped>
.MindMapContent{
  padding: 10px 25px;
  height: 350px;
  background-color: #ffffff;
}
#mindContent{
  position: relative;
}
</style>

项目地址

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2334535.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

榕壹云在线商城系统:基于THinkPHP+ Mysql+UniApp全端适配、高效部署的电商解决方案

项目背景&#xff1a;解决多端电商开发的痛点 随着移动互联网的普及和用户购物习惯的碎片化&#xff0c;传统电商系统面临以下挑战&#xff1a; 1. 多平台适配成本高&#xff1a;需要同时开发App、小程序、H5等多端应用&#xff0c;重复开发导致资源浪费。 2. 技术依赖第三方…

Android studio打包uniapp插件

一.参考资料与环境准备 原生工程配置需要使用到Android studio和HbuilderX 当前测试的as版本-20240301,下载地址&#xff1a;HbuilderX版本&#xff1a;4.36 二.插件创建流程 1.导入下载的UniPlugin-Hello-AS工程&#xff08;下载地址见参考资料&#xff09; 2.生成jks证书…

App Cleaner Pro for Mac 中 Mac软件卸载工具

App Cleaner Pro for Mac 中 Mac软件卸载工具 一、介绍 App Cleaner & Uninstaller Pro Mac破解&#xff0c;是一款Mac软件卸载工具&#xff0c;残余垃圾清除工具&#xff01;可以卸载应用程序或只删除不需要的服务文件&#xff0c;甚至可以删除以前删除的应用程序中的文…

开发规范——Restful风格

目录 Restful Apifox 介绍 端口号8080怎么来的&#xff1f; 为什么要使用Apifox? Restful 如果请求方式是Post&#xff0c;那我就知道了要执行新增操作&#xff0c;要新增一个用户 如果请求方式是Put&#xff0c;那就代表我要修改用户 具体要对这些资源进行什么样的操…

大模型——Llama Stack快速入门 部署构建AI大模型指南

Llama Stack快速入门 部署构建AI大模型指南 介绍 Llama Stack 是一组标准化和有主见的接口,用于如何构建规范的工具链组件(微调、合成数据生成)和代理应用程序。我们希望这些接口能够在整个生态系统中得到采用,这将有助于更轻松地实现互操作性。 Llama Stack 定义并标准化…

利用阿里云企业邮箱服务实现Python群发邮件

目录 一、阿里云企业邮箱群发邮件全流程实现 1. 准备工作与环境配置 2. 收件人列表管理 3. 邮件内容构建 4. 附件添加实现 5. 邮件发送核心逻辑 二、开发过程中遇到的问题与解决方案 1. 附件发送失败问题 2. 中文文件名乱码问题 3. 企业邮箱认证失败 三、完整工作流…

08-JVM 面试题-mk

文章目录 1.JVM 的各部分组成2.运行时数据区2.1.什么是程序计数器?2.2.你能给我详细的介绍Java堆吗?2.3.能不能解释一下方法区?2.3.1常量池2.3.2.运行时常量池2.4.什么是虚拟机栈?2.4.1.垃圾回收是否涉及栈内存?2.4.2.栈内存分配越大越好吗?2.4.3.方法内的局部变量是否线…

PostgreSQL技术大讲堂 - 第86讲:数据安全之--data_checksums天使与魔鬼

PostgreSQL技术大讲堂 - 第86讲&#xff0c;主题&#xff1a;数据安全之--data_checksums天使与魔鬼 1、data_checksums特性 2、避开DML规则&#xff0c;嫁接非法数据并合法化 3、避开约束规则&#xff0c;嫁接非法数据到表中 4、避开数据检查&#xff0c;读取坏块中的数据…

从宇树摇操avp_teleoperate到unitree_IL_lerobot:如何基于宇树人形进行二次开发(含Open-TeleVision源码解析)

前言 如之前的文章所述&#xff0c;我司「七月在线」正在并行开发多个订单&#xff0c;目前正在全力做好每一个订单&#xff0c;因为保密协议的原因&#xff0c;暂时没法拿出太多细节出来分享 ​但可以持续解读我们所创新改造或二次开发的对象&#xff0c;即解读paper和开源库…

告别 ifconfig:为什么现代 Linux 系统推荐使用 ip 命令

告别 ifconfig&#xff1a;为什么现代 Linux 系统推荐使用 ip 命令 ifconfig 指令已经被视为过时的工具&#xff0c;不再是查看和配置网络接口的推荐方式。 与 netstat 被 ss 替代类似。 本文简要介绍 ip addr 命令的使用 简介ip ifconfig 属于 net-tools 包&#xff0c;这个…

MySQL——MVCC(多版本并发控制)

目录 1.MVCC多版本并发控制的一些基本概念 MVCC实现原理 记录中的隐藏字段 undo log undo log 版本链 ReadView 数据访问规则 具体实现逻辑 总结 1.MVCC多版本并发控制的一些基本概念 当前读&#xff1a;该取的是记录的最新版本&#xff0c;读取时还要保证其他并发事务…

Gateway-网关-分布式服务部署

前言 什么是API⽹关 API⽹关(简称⽹关)也是⼀个服务, 通常是后端服务的唯⼀⼊⼝. 它的定义类似设计模式中的Facade模式(⻔⾯模式, 也称外观模式). 它就类似整个微服务架构的⻔⾯, 所有的外部客⼾端访问, 都需要经过它来进⾏调度和过滤. 常⻅⽹关实现 Spring Cloud Gateway&a…

Docker部署MySQL大小写不敏感配置与数据迁移实战20250409

Docker部署MySQL大小写不敏感配置与数据迁移实战 &#x1f9ed; 引言 在企业实际应用中&#xff0c;尤其是使用Java、Hibernate等框架开发的系统&#xff0c;MySQL默认的大小写敏感特性容易引发各种兼容性问题。特别是在Linux系统中部署Docker版MySQL时&#xff0c;默认行为可…

面试题之网络相关

最近开始面试了&#xff0c;410面试了一家公司 问了我几个网络相关的问题&#xff0c;我都不会&#xff01;&#xff01;现在来恶补一下&#xff0c;整理到博客中&#xff0c;好难记啊&#xff0c;虽然整理下来了。在这里先祝愿大家在现有公司好好沉淀&#xff0c;定位好自己的…

[春秋云镜] Tsclient仿真场景

文章目录 靶标介绍&#xff1a;外网mssql弱口令SweetPotato提权上线CSCS注入在线用户进程上线 内网chisel搭建代理密码喷洒攻击映像劫持 -- 放大镜提权krbrelayup提权Dcsync 参考文章 考点: mssql弱口令SweetPotato提权CS注入在线用户进程上线共享文件CS不出网转发上线密码喷洒…

数据集 handpose_x_plus 3D RGB 三维手势 - 手工绘画 场景 draw picture

数据集 handpose 相关项目地址&#xff1a;https://github.com/XIAN-HHappy/handpose_x_plus 样例数据下载地址&#xff1a;数据集handpose-x-plus3DRGB三维手势-手工绘画场景drawpicture资源-CSDN文库

deskflow使用教程:一个可以让两台电脑鼠标键盘截图剪贴板共同使用的开源项目

首先去开源网站下载&#xff1a;Release v1.21.2 deskflow/deskflow 两台电脑都要下载这个文件 下载好后直接打开找到你想要的exe desflow.exe 然后你打开他&#xff0c;将两台电脑的TLS都关掉 下面步骤两台电脑都要完成&#xff1a; 电脑点开edit-》preferences 把这个取…

详解MYSQL表空间

目录 表空间文件 表空间文件结构 行格式 Compact 行格式 变长字段列表 NULL值列表 记录头信息 列数据 溢出页 数据页 当我们使用MYSQL存储数据时&#xff0c;数据是如何被组织起来的&#xff1f;索引又是如何组织的&#xff1f;在本文我们将会解答这些问题。 表空间文…

[Windows] 音速启动 1.0.0.0

[Windows] 音速启动 链接&#xff1a;https://pan.xunlei.com/s/VONiGZhtsxpPzze0lDIH-mR9A1?pwdxu7f# [Windows] 音速启动 1.0.0.0 音速启动是一款桌面管理软件&#xff0c;以仿真QQ界面的形式结合桌面工具的特点&#xff0c;应用于软件文件夹网址的快捷操作。

Hyper-V 虚拟机配置静态IP并且映射到局域网使用

环境 win11hyper-v麒麟v10 配置 编辑文件 vi /etc/sysconfig/network-scripts/ifcfg-eth0文件内容 GATEWAY 需要参考网络中配置的网关地址 TYPEEthernet PROXY_METHODnone BROWSER_ONLYno BOOTPROTOstatic DEFROUTEyes IPV4_FAILURE_FATALno IPV6INITyes IPV6_AUTOCONFyes …