可拖拽编辑的流程图X6

news2024/11/20 12:38:47

 先上图

//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>

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

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

相关文章

python可视化——pycharm——Grid - Grid_overlap_multi_xy_axis图

这段代码使用了pyecharts库来创建一个Grid-Overlap图&#xff0c;包括柱状图和折线图。 首先&#xff0c;创建了一个柱状图对象bar&#xff0c;并添加了两个y轴的数据。第一个y轴代表csdn博客数量&#xff0c;第二个y轴代表粉丝数量。然后&#xff0c;使用extend_axis方法扩展…

6、深入解析Kotlin类与对象:构造、伴生、单例全面剖析

前言 本篇文章将带您了解Kotlin编程中的重要概念&#xff1a;类及构造函数、访问修饰符、伴生对象和单例模式。就像搭积木一样&#xff0c;我们会逐步揭开这些概念的面纱&#xff0c;让您轻松理解它们的作用和用法。无论您是编程新手还是有经验的开发者&#xff0c;本文都将为…

一个独立的测试人如何完成一个项目的测试 + 发布

其实啊&#xff0c;每个人都可以「独立完成一个项目的测试 发布」 很多同学&#xff0c;工作了五六年&#xff0c;都没有机会&#xff08;也许是&#xff1a;不敢&#xff09;独立负责一个完整项目的测试&#xff08;独立负责一个项目测试后的上线流程&#xff0c;机会就更少…

手把手教你调用5个公共API获取数字货币市场数据(内附详细源码)

数量技术宅团队在CSDN学院推出了量化投资系列课程 欢迎有兴趣系统学习量化投资的同学&#xff0c;点击下方链接报名&#xff1a; 量化投资速成营&#xff08;入门课程&#xff09; Python股票量化投资 Python期货量化投资 Python数字货币量化投资 C语言CTP期货交易系统开…

three.js(十):线性几何体

线性几何体 WireframeGeometry 网格几何体EdgesGeometry 边缘几何体 WireframeGeometry 网格几何体 WireframeGeometry( geometry : BufferGeometry ) geometry — 任意几何体对象。 const geometry new SphereGeometry(); const wireframe new WireframeGeometry(geometr…

2022年06月 C/C++(五级)真题解析#中国电子学会#全国青少年软件编程等级考试

第1题&#xff1a;逃离迷宫 你在一个地下迷宫中找到了宝藏&#xff0c;但是也触发了迷宫机关&#xff0c;导致迷宫将在T分钟后坍塌&#xff0c;为此你需要在T分钟内逃离迷宫&#xff0c;你想知道你能不能逃离迷宫。迷宫是一个边长为m的正方形&#xff0c;其中"S"表示…

完善会计流程的关键功能:了解必备的会计软件功能!

会计软件已经成为许多大小企业的财务管理标配。会计软件可以帮助企业自动化财务流程&#xff0c;提高工作效率&#xff0c;减少错误&#xff0c;并提供准确和可靠的财务数据。一款合格的会计软件应该具备什么功能呢&#xff1f; 会计软件应有的功能 1、账户管理&#xff1a;会…

overlayfs

参考&#xff1a;How containers work: overlayfs how overlays work Overlay filesystems, also known as “union filesystems” or “union mounts” let you mount a filesystem using 2 directories: a “lower” directory, and an “upper” directory. Basically: t…

C语言之练习题

欢迎来到我的&#xff1a;世界 希望作者的文章对你有所帮助&#xff0c;有不足的地方还请指正&#xff0c;大家一起学习交流 ! 目录 前言填空题&#xff1a;第一题第二题第三题第四题 编程题&#xff1a;第一题&#xff1a;第二题&#xff1a; 总结 前言 填空题&#xff1a; …

网御ACM上网行为管理系统bottomframe.cgi接口存在SQL注入漏洞 附POC

文章目录 网御ACM上网行为管理系统bottomframe.cgi接口存在SQL注入漏洞 附POC1. 网御ACM上网行为管理系统简介2.漏洞描述3.影响版本4.fofa查询语句5.漏洞复现6.POC&EXP7.整改意见8.往期回顾 网御ACM上网行为管理系统bottomframe.cgi接口存在SQL注入漏洞 附POC 免责声明&am…

three.js(九):内置的路径合成几何体

路径合成几何体 TubeGeometry 管道LatheGeometry 车削ExtrudeGeometry 挤压 TubeGeometry 管道 TubeGeometry(path : Curve, tubularSegments : Integer, radius : Float, radialSegments : Integer, closed : Boolean) path — Curve - 一个由基类Curve继承而来的3D路径。 De…

关于Linux系统时间的问题

关于Linux系统时间的问题 当我们进行一些特定的业务需求时&#xff0c;需要修改当前Linux系统的系统时间。我们可以用以下命令进行修改时间。 data -s "2022-08-31 15:00:00"当我们将时间设置为某个时间点后&#xff0c;Linux系统的时间会出现一个问题&#xff1a;…

React原理 - React Reconciliation-上

目录 扩展学习资料 React Reconciliation Stack Reconciler【15版本、栈协调】 Stack Reconciler-事务性 事务性带来的弊端&#xff1a; 扩展学习资料 名称 链接 备注 官方文档 Reconciliation – React 英文 stack reconciler Implementation Notes – React 英文…

IntelliJ IDEA 2023.2.1使用Git时弹出“使用访问令牌登录”问题解决

这里写目录标题 一、内网Git环境GitLabGogsGitea 二、外网Git环境GitHubGitee 升级为IntelliJ IDEA 2023.2.1后&#xff0c;使用Git时弹出“使用访问令牌登录”的窗口&#xff0c;习惯使用Git帐号密码登录的用户&#xff0c;面对这个突如其来的弹窗真的很懵。 一、内网Git环境 …

虚拟化技术原理

计算虚拟化 介绍 把物理主机上物理资源&#xff08;CPU&#xff0c;内存&#xff0c;IO外设&#xff09;&#xff0c;通过虚拟化层抽象成超量、等量的逻辑资源&#xff08;虚拟CPU&#xff0c;虚拟内存&#xff0c;虚拟IO设备&#xff09;&#xff0c;然后重新组合形成新的虚…

大数据学习:impala基础

impala基础 1. impala介绍 1.1 impala概述 Impala是Cloudera公司推出&#xff0c;提供对HDFS、Hbase数据的高性能、低延迟的交互式SQL查询功能。官方测试性能比hive快10到100倍&#xff0c;其sql查询比sparkSQL还要更加快速&#xff0c;号称是当前大数据领域最快的查询sq工具…

【排序】快排非递归

模拟递归的下标&#xff0c;让他们入栈

投票同款特效样式

先看效果&#xff1a; 再看代码&#xff08;查看更多&#xff09;&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><style>import url("https://fonts.…