可拖拽编辑的流程图X6

 先上图

//index.html,有时候可能加载失败,那就再找一个别的cdn 或者npm下载,如果npm下载,
//那么需要全局引入或者局部引入,代码里面写法也会不同,详细的可以看示例

<script src="https://cdn.jsdelivr.net/npm/@antv/x6/dist/x6.js"></script>
//chart.vue
<template>
  <div>
    <el-button type="primary" @click="download">导出PNG</el-button>
    <el-button type="primary" @click="downloadJSON">导出JSON</el-button>
    <input type="file" id="select-input" ref="files" style="width: 70px"/>
    删除某个节点   shift+backspace
    <div id="container">
      <div id="stencil"></div>
      <div id="graph-container"></div>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      graph: null,
    }
  },
  mounted(){
    // 为了协助代码演示
    const graph = new X6.Graph({
      container: document.getElementById("graph-container"),
      grid: true,
      background: {
        color: '#fffbe6', // 设置画布背景颜色
      },
      mousewheel: {
        enabled: true,
        zoomAtMousePosition: true,
        modifiers: "ctrl",
        minScale: 0.5,
        maxScale: 3
      },
      connecting: {
        router: {
          name: "manhattan",
          args: {
            padding: 1
          }
        },
        connector: {
          name: "rounded",
          args: {
            radius: 8
          }
        },
        anchor: "center",
        connectionPoint: "anchor",
        allowBlank: false,
        snap: {
          radius: 20
        },
        createEdge() {
          return new X6.Shape.Edge({
            attrs: {
              line: {
                stroke: "#A2B1C3",
                strokeWidth: 2,
                targetMarker: {
                  name: "block",
                  width: 12,
                  height: 8
                }
              }
            },
            zIndex: 0
          })
        },
        validateConnection({ targetMagnet }) {
          return !!targetMagnet
        }
      },
      highlighting: {
        magnetAdsorbed: {
          name: "stroke",
          args: {
            attrs: {
              fill: "#5F95FF",
              stroke: "#5F95FF"
            }
          }
        }
      },
      resizing: true,
      rotating: true,
      selecting: {
        enabled: true,
        rubberband: true,
        showNodeSelectionBox: true
      },
      snapline: true,
      keyboard: true,
      clipboard: true
    })
    this.graph = graph
    // #region 初始化 stencil
    const stencil = new X6.Addon.Stencil({
      title: "流程图",
      target: graph,
      stencilGraphWidth: 200,
      stencilGraphHeight: 180,
      collapsable: true,
      groups: [
        {
          title: "基础流程图",
          name: "group1"
        },
        {
          title: "系统设计图",
          name: "group2",
          graphHeight: 250,
          layoutOptions: {
            rowHeight: 70
          }
        }
      ],
      layoutOptions: {
        columns: 2,
        columnWidth: 80,
        rowHeight: 55
      }
    })
    document.getElementById("stencil").appendChild(stencil.container)
    // #region 快捷键与事件
    // copy cut paste
    graph.bindKey(["meta+c", "ctrl+c"], () => {
      const cells = graph.getSelectedCells()
      if (cells.length) {
        graph.copy(cells)
      }
      return false
    })
    graph.bindKey(["meta+x", "ctrl+x"], () => {
      const cells = graph.getSelectedCells()
      if (cells.length) {
        graph.cut(cells)
      }
      return false
    })
    graph.bindKey(["meta+v", "ctrl+v"], () => {
      if (!graph.isClipboardEmpty()) {
        const cells = graph.paste({ offset: 32 })
        graph.cleanSelection()
        graph.select(cells)
      }
      return false
    })

    //undo redo
    graph.bindKey(["meta+z", "ctrl+z"], () => {
      if (graph.history.canUndo()) {
        graph.history.undo()
      }
      return false
    })
    graph.bindKey(["meta+shift+z", "ctrl+shift+z"], () => {
      if (graph.history.canRedo()) {
        graph.history.redo()
      }
      return false
    })

    // select all
    graph.bindKey(["meta+a", "ctrl+a"], () => {
      const nodes = graph.getNodes()
      if (nodes) {
        graph.select(nodes)
      }
    })

    //delete
    graph.bindKey("shift+backspace", () => {
      const cells = graph.getSelectedCells()
      if (cells.length) {
        graph.removeCells(cells)
      }
    })

    // zoom
    graph.bindKey(["ctrl+1", "meta+1"], () => {
      const zoom = graph.zoom()
      if (zoom < 1.5) {
        graph.zoom(0.1)
      }
    })
    graph.bindKey(["ctrl+2", "meta+2"], () => {
      const zoom = graph.zoom()
      if (zoom > 0.5) {
        graph.zoom(-0.1)
      }
    })

    // 控制连接桩显示/隐藏
    const showPorts = (ports, show) => {
      for (let i = 0, len = ports.length; i < len; i = i + 1) {
        ports[i].style.visibility = show ? "visible" : "hidden"
      }
    }
    graph.on("node:mouseenter", () => {
      const container = document.getElementById("graph-container")
      const ports = container.querySelectorAll(".x6-port-body")
      showPorts(ports, true)
    })
    graph.on("node:mouseleave", () => {
      const container = document.getElementById("graph-container")
      const ports = container.querySelectorAll(".x6-port-body")
      showPorts(ports, false)
    })
    // #endregion

    // #region 初始化图形
    const ports = {
      groups: {
        top: {
          position: "top",
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: "#5F95FF",
              strokeWidth: 1,
              fill: "#fff",
              style: {
                visibility: "hidden"
              }
            }
          }
        },
        right: {
          position: "right",
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: "#5F95FF",
              strokeWidth: 1,
              fill: "#fff",
              style: {
                visibility: "hidden"
              }
            }
          }
        },
        bottom: {
          position: "bottom",
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: "#5F95FF",
              strokeWidth: 1,
              fill: "#fff",
              style: {
                visibility: "hidden"
              }
            }
          }
        },
        left: {
          position: "left",
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: "#5F95FF",
              strokeWidth: 1,
              fill: "#fff",
              style: {
                visibility: "hidden"
              }
            }
          }
        }
      },
      items: [
        {
          group: "top"
        },
        {
          group: "right"
        },
        {
          group: "bottom"
        },
        {
          group: "left"
        }
      ]
    }

    X6.Graph.registerNode(
      "custom-rect",
      {
        inherit: "rect",
        width: 66,
        height: 36,
        attrs: {
          body: {
            strokeWidth: 1,
            stroke: "#5F95FF",
            fill: "#EFF4FF"
          },
          text: {
            fontSize: 12,
            fill: "#262626"
          }
        },
        ports: { ...ports }
      },
      true
    )

    X6.Graph.registerNode(
      "custom-polygon",
      {
        inherit: "polygon",
        width: 66,
        height: 36,
        attrs: {
          body: {
            strokeWidth: 1,
            stroke: "#5F95FF",
            fill: "#EFF4FF"
          },
          text: {
            fontSize: 12,
            fill: "#262626"
          }
        },
        ports: {
          ...ports,
          items: [
            {
              group: "top"
            },
            {
              group: "bottom"
            }
          ]
        }
      },
      true
    )

    X6.Graph.registerNode(
      "custom-circle",
      {
        inherit: "circle",
        width: 45,
        height: 45,
        attrs: {
          body: {
            strokeWidth: 1,
            stroke: "#5F95FF",
            fill: "#EFF4FF"
          },
          text: {
            fontSize: 12,
            fill: "#262626"
          }
        },
        ports: { ...ports }
      },
      true
    )

    X6.Graph.registerNode(
      "custom-image",
      {
        inherit: "rect",
        width: 52,
        height: 52,
        markup: [
          {
            tagName: "rect",
            selector: "body"
          },
          {
            tagName: "image"
          },
          {
            tagName: "text",
            selector: "label"
          }
        ],
        attrs: {
          body: {
            stroke: "#5F95FF",
            fill: "#5F95FF"
          },
          image: {
            width: 26,
            height: 26,
            refX: 13,
            refY: 16
          },
          label: {
            refX: 3,
            refY: 2,
            textAnchor: "left",
            textVerticalAnchor: "top",
            fontSize: 12,
            fill: "#fff"
          }
        },
        ports: { ...ports }
      },
      true
    )

    const r1 = graph.createNode({
      shape: "custom-rect",
      label: "开始",
      attrs: {
        body: {
          rx: 20,
          ry: 26
        }
      }
    })
    const r2 = graph.createNode({
      shape: "custom-rect",
      label: "过程"
    })
    const r3 = graph.createNode({
      shape: "custom-rect",
      attrs: {
        body: {
          rx: 6,
          ry: 6
        }
      },
      label: "可选过程"
    })
    const r4 = graph.createNode({
      shape: "custom-polygon",
      attrs: {
        body: {
          refPoints: "0,10 10,0 20,10 10,20"
        }
      },
      label: "决策"
    })
    const r5 = graph.createNode({
      shape: "custom-polygon",
      attrs: {
        body: {
          refPoints: "10,0 40,0 30,20 0,20"
        }
      },
      label: "数据"
    })
    const r6 = graph.createNode({
      shape: "custom-circle",
      label: "连接"
    })
    stencil.load([r1, r2, r3, r4, r5, r6], "group1")

    const imageShapes = [
      {
        label: "Client",
        image:
          "https://gw.alipayobjects.com/zos/bmw-prod/687b6cb9-4b97-42a6-96d0-34b3099133ac.svg"
      },
      {
        label: "Http",
        image:
          "https://gw.alipayobjects.com/zos/bmw-prod/dc1ced06-417d-466f-927b-b4a4d3265791.svg"
      },
      {
        label: "Api",
        image:
          "https://gw.alipayobjects.com/zos/bmw-prod/c55d7ae1-8d20-4585-bd8f-ca23653a4489.svg"
      },
      {
        label: "Sql",
        image:
          "https://gw.alipayobjects.com/zos/bmw-prod/6eb71764-18ed-4149-b868-53ad1542c405.svg"
      },
      {
        label: "Clound",
        image:
          "https://gw.alipayobjects.com/zos/bmw-prod/c36fe7cb-dc24-4854-aeb5-88d8dc36d52e.svg"
      },
      {
        label: "Mq",
        image:
          "https://gw.alipayobjects.com/zos/bmw-prod/2010ac9f-40e7-49d4-8c4a-4fcf2f83033b.svg"
      }
    ]
    const imageNodes = imageShapes.map(item =>
      graph.createNode({
        shape: "custom-image",
        label: item.label,
        attrs: {
          image: {
            "xlink:href": item.image
          }
        }
      })
    )
    stencil.load(imageNodes, "group2")
    //编辑
    graph.on('cell:dblclick', ({ cell, e }) => {
      const isNode = cell.isNode()
      const name = cell.isNode() ? 'node-editor' : 'edge-editor'
      cell.removeTool(name)
      cell.addTools({
        name,
        args: {
          event: e,
          attrs: {
            backgroundColor: isNode ? '#EFF4FF' : '#FFF',
          },
        },
      })
    })
    //直接加在样式上不生效
    document.getElementById('graph-container').style.width = 'calc(100% - 180px)'
    document.getElementById('graph-container').style.height = '100%'

    document.getElementById("select-input").addEventListener("change", (e) =>{
      let file = e.target.files[0];
      let fileName = file.name.split('.')
      if(fileName[fileName.length-1] !== 'txt') {
        this.$refs.files.value = ''
        return this.$message({message: '请上传.txt格式文件',type: 'warning'});
      }
			if(!window.FileReader) return this.$message({message: 'Not supported by your browser!',type: 'warning'});
      // 创建FileReader对象(文件对象)
      const reader = new FileReader();
      // 读取出错时:
      reader.onerror = (e)=>{
        this.$message({message: '读取出错!',type: 'warning'});
      };
      // 读取中断时:
      reader.onabort = (e)=>{
        this.$message({message: '读取中断!',type: 'warning'});
      };
      // 读取成功时:
      reader.onload = (e)=>{
        // 输出文件
        this.$refs.files.value = ''
        this.graph.fromJSON(JSON.parse(e.target.result))
        this.$message({message: '读取成功!',type: 'success'});
      };
      reader.readAsText(file,"utf-8");
		}, false);
  },
  methods:{
    download(){
      this.graph.toPNG((dataUri) => {
        // 下载
        X6.DataUri.downloadDataUri(dataUri, '流程图.png')
      },{
          width: 600,
          height: 500,
          padding: 10,
        })
    },
    downloadJSON(){
      let d = this.graph.toJSON()
      let el = document.createElement('a')
      el.setAttribute('href','data:text.plain;charset=utf-8,'+encodeURIComponent(JSON.stringify(d)))
      el.setAttribute('download','图表数据.txt')
      el.style.display = 'none'
      document.body.appendChild(el)
      el.click()
      document.body.removeChild(el)
    },
  }
}
</script>

<style lang="less" scoped>
#container {
  display: flex;
  border: 1px solid #dfe3e8;
  height: 100vh;
  width: 100%;
  margin-top: 10px;
}
#stencil {
  width: 180px;
  height: 100%;
  position: relative;
  border-right: 1px solid #dfe3e8;
}
.x6-widget-stencil  {
  background-color: #fff;
}
.x6-widget-stencil-title {
  background-color: #fff;
}
.x6-widget-stencil-group-title {
  background-color: #fff !important;
}
.x6-widget-transform {
  margin: -1px 0 0 -1px;
  padding: 0px;
  border: 1px solid #239edd;
}
.x6-widget-transform > div {
  border: 1px solid #239edd;
}
.x6-widget-transform > div:hover {
  background-color: #3dafe4;
}
.x6-widget-transform-active-handle {
  background-color: #3dafe4;
}
.x6-widget-transform-resize {
  border-radius: 0;
}
.x6-widget-selection-inner {
  border: 1px solid #239edd;
}
.x6-widget-selection-box {
  opacity: 0;
}
</style>

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

openGauss学习笔记-56 openGauss 高级特性-DCF

文章目录 openGauss学习笔记-56 openGauss 高级特性-DCF56.1 架构介绍56.2 功能介绍56.3 使用示例 openGauss学习笔记-56 openGauss 高级特性-DCF DCF全称是Distributed Consensus Framework&#xff0c;即分布式一致性共识框架。DCF实现了Paxos、Raft等解决分布式一致性问题典…

“北科Java面试宝典(211最详细讲解)“

Version : V1.0 北科Java面试宝典一、Java基础面试题【24道】二、JVM虚拟机面试题【14道】三、集合相关面试题【17道】四、多线程 【25道】五、IO【5道】六、网络编程 【9道】七、MySQL以及SQL面试题【20道】八、常用框架【19道】九、中间件和分布式 【54道】十、设计模式面试 …

unity 之 如何获取父物体与子物体

文章目录 获取父物体获取子物体 获取父物体 在Unity中&#xff0c;你可以使用Transform组件的属性来获取对象的父物体。以下是在C#脚本中如何获取父物体的示例代码&#xff1a; using UnityEngine;public class GetParentExample : MonoBehaviour {void Start(){// 获取当前物…

R3LIVE源码解析(6) — R3LIVE流程详解

目录 1 R3LIVE框架简介 2 R3LIVE的launch文件 3 R3LIVE的r3live_config文件 4 R3LIVE从哪开始阅读 1 R3LIVE框架简介 R3LIVE是香港大学Mars实验室提出的一种融合imu、相机、激光的SLAM方法&#xff0c;R3LIVE由两个子系统组成&#xff0c;一个激光惯性里程计&#xff08;L…

网站常见安全漏洞 | 青训营

Powered by:NEFU AB-IN 文章目录 网站常见安全漏洞 | 青训营 网站基本组成及漏洞定义服务端漏洞SQL注入命令执行越权漏洞SSRF文件上传漏洞 客户端漏洞开放重定向XSSCSRF点击劫持CORS跨域配置错误WebSocket 网站常见安全漏洞 | 青训营 网站常见安全漏洞-网站基本组成及漏洞定义…

1、Spring是什么?

Spring 是一款主流的 Java EE 轻量级开源框架 。 框架 你可以理解为是一个程序的半成品&#xff0c;它帮我们实现了一部分功能&#xff0c;用这个框架我们可以减少代码的实现和功能的开发。 开源 也就是说&#xff0c;它开放源代码。通过源代码&#xff0c;你可以看到它是如何…

学习ts(十一)本地存储与发布订阅模式

localStorage实现过期时间 目录 准备 安装 npm i rollup typescript rollup-plugin-typescript2// tsconfig.json"module": "ESNext","moduleResolution": "node", "strict": false, // rollup.config.js import …

android studio安装教程

1、android studio 下载 下载网址&#xff1a;Download Android Studio & App Tools - Android Developers 2、开始安装 因为不需要每次连接手机进行调试&#xff0c;android studio给我们提供了模拟器调试环境。 一般选择自定义安装&#xff0c;这样可选sdk以及下载路径…

Apipost:为什么是开发者首选的API调试工具

文章目录 前言正文接口调试接口公共参数、环境全局参数的使用快速生成并导出接口文档研发协作接口压测和自动化测试结论 前言 Apipost是一款支持 RESTful API、SOAP API、GraphQL API等多种API类型&#xff0c;支持 HTTPS、WebSocket、gRPC多种通信协议的API调试工具。除此之外…

MySQL中的Buffer Pool

一、概述 Buffer Pool是数据库的一个内存组件&#xff0c;里面缓存了磁盘上的真实数据&#xff0c;然后我们的Java系统对数据库执行的增删改操作&#xff0c;其实主要就是对这个内存数据结构中的缓存数据执行的。我们先来看一下下面的图&#xff0c;里面就画了数据库中的Buffer…

【LeetCode-中等题】98. 验证二叉搜索树

文章目录 题目方法一&#xff1a;BFS 层序遍历方法二&#xff1a; 递归方法三&#xff1a; 中序遍历&#xff08;栈&#xff09;方法四&#xff1a; 中序遍历&#xff08;递归&#xff09; 题目 思路就是首先得知道什么是二叉搜索树 左孩子在&#xff08;父节点的最小值&#x…

尚硅谷宋红康MySQL笔记 14-18

是记录&#xff0c;不会太详细&#xff0c;受本人知识限制会有错误&#xff0c;会有个人的理解在里面 第14章 视图 了解一下&#xff0c;数据库的常见对象 对象描述表(TABLE)表是存储数据的逻辑单元&#xff0c;以行和列的形式存在&#xff0c;列就是字段&#xff0c;行就是记…

博睿数据当选粤港澳大湾区金融创新研究院理事会单位,助力金融科技创新发展

近日&#xff0c;博睿数据当选粤港澳大湾区金融创新研究院理事会单位。这是对博睿数据在金融科技领域所取得成绩的高度认可&#xff0c;也是对其创新能力和发展潜力的充分肯定。 粤港澳大湾区金融创新研究院由粤港澳三地金融行业、高等院校高层和专家学者共同发起&#xff0c;经…

QT初始学习中的个人基础认知

整体感觉 安装的时候感觉更像python的库安装和编译器版本的配合安装。进入创建工程时&#xff0c;感觉是c语言的创建工程的感觉&#xff0c;而且可以看到main和h的头文件&#xff0c;整体来看是C来编写的程序。完成整个工程个人感觉是C编写功能&#xff0c;使用VB实现界面设计…

2023-8-31 spfa判断负环

题目链接&#xff1a;spfa判断负环 #include <iostream> #include <cstring> #include <algorithm> #include <queue>using namespace std;const int N 100010;int n, m; int h[N], e[N], w[N], ne[N], idx;int dist[N], cnt[N]; int st[N];void ad…

SpringBoot的四种handler类型

Controller ReuestMapping 实现Controller接口 使用Component将该类封装成一个Bean 实现HttpRequestHandler 实现RouterFunction

leetcode 516. 最长回文子序列

2023.8.27 本题依旧使用dp算法做&#xff0c;可以参考 回文子串 这道题。dp[i][j]定义为&#xff1a;子串s[i,j] 的最长回文子串。 直接看代码: class Solution { public:int longestPalindromeSubseq(string s) {vector<vector<int>> dp(s.size(),vector<int&…

vue v-for 例子

vue v-for 例子 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title> </head&…

4年外包终上岸,我只能说这类公司能不去就不去...

我大学学的是计算机专业&#xff0c;毕业的时候&#xff0c;对于找工作比较迷茫&#xff0c;也不知道当时怎么想的&#xff0c;一头就扎进了一家外包公司&#xff0c;一干就是4年。现在终于跳槽到了互联网公司了&#xff0c;我想说的是&#xff0c;但凡有点机会&#xff0c;千万…

python把txt变成list,并且写入xslx文件

需求&#xff1a; 1、把txt文件的内容变成list 2、然后写入excel中 txt文件内容 IP.txt 192.168.199.201,4C8G,200G 192.168.199.202,4C8G,200G 192.168.199.203,4C8G,200G 192.168.199.204,4C8G,200G 192.168.199.205,4C8G,200G192.168.199.206,4C8G,200G 192.168.199.207…