vue antv X6流程图

2023-11-06

第一 下载2.0插件

第二 引入代码

<!--  -->
<template>
  <div id="container" style="min-width: 400px; min-height: 600px"></div>
</template>

<script >
//这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
//例如:import 《组件名称》 from '《组件路径》';
import { Graph, Shape } from "@antv/x6";
import { Stencil } from "@antv/x6-plugin-stencil";
import { Transform } from "@antv/x6-plugin-transform";import { Selection } from "@antv/x6-plugin-selection";
import { Snapline } from "@antv/x6-plugin-snapline";
import { Keyboard } from "@antv/x6-plugin-keyboard";
import { Clipboard } from "@antv/x6-plugin-clipboard";
import { History } from "@antv/x6-plugin-history";
import insertCss from "insert-css";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {};
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    init() {
      const data = {
        nodes: [
          {
            id: "node1",
            shape: "rect",
            x: 40,
            y: 40,
            width: 100,
            height: 40,
            label: "hello",
            attrs: {
              // body 是选择器名称,选中的是 rect 元素
              body: {
                stroke: "#8f8f8f",
                strokeWidth: 1,
                fill: "#fff",
                rx: 6,
                ry: 6,
              },
            },
          },
          {
            id: "node2",
            shape: "rect",
            x: 160,
            y: 180,
            width: 100,
            height: 40,
            label: "world",
            attrs: {
              body: {
                stroke: "#8f8f8f",
                strokeWidth: 1,
                fill: "#fff",
                rx: 6,
                ry: 6,
              },
            },
          },
        ],
        edges: [
          {
            shape: "edge",
            source: "node1",
            target: "node2",
            label: "x6",
            attrs: {
              // line 是选择器名称,选中的边的 path 元素
              line: {
                stroke: "#8f8f8f",
                strokeWidth: 1,
              },
            },
          },
        ],
      };
      const graph = new Graph({
        container: document.getElementById("graph-container"),
        grid: true,
        mousewheel: {
          enabled: true,
          zoomAtMousePosition: true,
          modifiers: "ctrl",
          minScale: 0.5,
          maxScale: 3,
        },
        connecting: {
          router: "manhattan",
          connector: {
            name: "rounded",
            args: {
              radius: 8,
            },
          },
          anchor: "center",
          connectionPoint: "anchor",
          allowBlank: false,
          snap: {
            radius: 20,
          },
          createEdge() {
            return new 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",
              },
            },
          },
        },
      });
      // #region 使用插件
      graph
        .use(
          new Transform({
            resizing: true,
            rotating: true,
          })
        )
        .use(
          new Selection({
            rubberband: true,
            showNodeSelectionBox: true,
          })
        )
        .use(new Snapline())
        .use(new Keyboard())
        .use(new Clipboard())
        .use(new History());
      // #endregion

      // #region 初始化 stencil
      const stencil = new 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);
      // #endregion

      // #region 快捷键与事件
      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.canUndo()) {
          graph.undo();
        }
        return false;
      });
      graph.bindKey(["meta+shift+z", "ctrl+shift+z"], () => {
        if (graph.canRedo()) {
          graph.redo();
        }
        return false;
      });

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

      // delete
      graph.bindKey("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 += 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");
        // if (this.isPortsShow) return
        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",
          },
        ],
      };

      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
      );

      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
      );

      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
      );

      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");
    },

    preWork() {
      // 这里协助演示的代码,在实际项目中根据实际情况进行调整
      const container = document.getElementById("container");
      const stencilContainer = document.createElement("div");
      stencilContainer.id = "stencil";
      const graphContainer = document.createElement("div");
      graphContainer.id = "graph-container";
      container.appendChild(stencilContainer);
      container.appendChild(graphContainer);

      insertCss(`
        #container {
          display: flex;
          border: 1px solid #dfe3e8;
        }
        #stencil {
          width: 180px;
          height: 100%;
          position: relative;
          border-right: 1px solid #dfe3e8;
        }
        #graph-container {
          width: calc(100% - 180px);
          height: 100%;
        }
        .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;
        } `);
    },
  },
  //生命周期 - 创建完成(可以访问当前this实例)
  created() {},
  //生命周期 - 挂载完成(可以访问DOM元素)
  mounted() {
    this.preWork();
    this.init();
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
};
</script>
<style scoped>
/* @import url(); 引入公共css类 */

#container {
  width: 100%;
  height: 600px;
}
</style>

第三 运行

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vue antv X6流程图 的相关文章

随机推荐

  • 【KEIL-MDK】系列——如何使用PC-Lint代码检查工具

    系列文章目录 01 KEIL MDK 系列 主题配色 文章目录 前言 一 PC Lint 是什么 二 在 KEIL MDK 中集成 PC Lint 工具 1 安装 PC Lint 工具 2 打开 KEIL MDK 进行简单设置 三 使用 P
  • 伯努利模型的极大似然估计和贝叶斯估计

    定义随机变量A为一次伯努利试验的结果 A A A的取值为 0 1 概率分布为 P A P A
  • @PostConstruct注解说明

    PostConstruct 是在Java中使用的注解 用于标识一个方法在一个bean被实例化和初始化之后 但在其投入使用之前应该被执行 它通常在Spring框架应用中使用 当一个bean在Spring应用上下文中被创建时 容器会通过调用其构
  • mssql 远程无法连接mysql_如何开启SqlServer 远程访问

    当Microsoft Sqlserver 2012 安装好后 接下的的工作需要配置Sqlserver数据库允许远程访问 只有配置了Sqlserver远程访问 其他ip客户端才能访问 配置sqlserver远程访问大致分为三步 即配置SQL
  • Android开发中如何添加自定义开机广播

    Android开发中如何添加自定义开机广播 在Android开发中 我们经常需要在设备开机完成后执行一些特定的操作 比如初始化应用程序或者启动服务 为了实现这样的需求 我们可以通过添加自定义开机广播来实现 首先 我们需要创建一个广播接收器类
  • 修改windows的默认编码

    修改windows的默认编码 环境 win10 问题背景 使用7z解压缩一个 zip文件之后 发现文件包内的文件名是乱码 但是使用同版本的7z在另一个win10系统下解压缩 没有这个问题 问题原因 是win10默认编码的问题 由于这次的电脑
  • 【深度学习】基于华为MindSpore和pytorch的卷积神经网络LeNet5实现MNIST手写识别

    1 实验内容简介 1 1 实验目的 1 熟练掌握卷积 池化概念 2 熟练掌握卷积神经网络的基本原理 3 熟练掌握各种卷积神经网络框架单元 4 熟练掌握经典卷积神经网络模型 1 2 实验内容及要求 请基于pytorch和mindspore平台
  • csdn积分怎么查看

    csdn积分怎么查看 博客积分查询 https mp csdn net mp blog analysis article all 下载积分查询 https mp csdn net mp download analysis download
  • js 与 jsc 文件不能混用

    不然会出现莫名bug 转载于 https www cnblogs com guomengkai p 11511410 html
  • WORD软件安装

    WORD插件Aurora安装及下载 准备软件下载 正式安装 常见问题 准备软件下载 安装包下载地址 链接 https pan baidu com s 1gsTSDL0KPdeXdiucE3HGlA 提取码 vbi5 正式安装 按照流程安装即
  • 视频恢复软件哪个好用?推荐这几款恢复率高的软件

    如果你意外删除了电脑保存的视频文件 无论是单击回收站 还原 还是通过电脑系统备份 还原 它都无法恢复 如何解决这个问题 此时 你需要寻求文件删除恢复软件的帮助 推荐下面这几款恢复率高的视频恢复软件 如何操作呢 你可以看看下面的详细解说 第一
  • MongoDB分片

    MongoDB分片 集群搭建 环境准备 mongo1 127 0 0 1 mongo2 127 0 0 1 mongo3 127 0 0 1 config1 端口 27018 config2 端口 27028 config3 端口 2703
  • 如何实现动态代理

    1 动态代理和静态代理的区别 静态代理 在编译之前就已经确定好代理对象 代理方法等等 动态代理 在编译后才明确代理对象以及代理方法等等 2 JDK代理原理 使用JDK动态代理方法 我们需要代理类和被代理类同时继承同一个接口才能进行增强 3
  • canvas鼠标在屏幕上的互动效果实现

    1 首先我们需要整屏画布 你也可以随机设置 2 想要鼠标经过的时候有大小圆圈跟着鼠标动 故需要创建一个类来装圆的属性 随机的圆唯一的标识 id我这里用index 坐标 x y 半径r 颜色color 因为要很多圆需要一个数组来装 上面变量中
  • 从零开始,手把手教你实现一个高效的OA会议系统

    目录 前言 我将手把手教大家做一个会议系统 这个系统大概有8次文章 祝大家学的快乐 项目思路 将从以下方面去进行更新 1 项目简介 2 项目需要的开发文档 数据库建表 会议OA需求文档 会议OA需求规格说明书 OA会议系统数据库表结构 1
  • 【Fortran】Fortran中Open, Read 和 Write的用法

    1 write string write unit FMT string write unit 6 FMT string 以上等价 6是默认输出位置 即屏幕 2 print string Print只能对屏幕输出 3 integer kin
  • 关于show arp表,显示Incomplete问题

    最近出现一个问题 在核心交换机上查看交换机的ARP表的时候 很多ARP表项目会显示如信息 Internet 172 21 6 1 0 Incomplete ARPA 这表示没有学习到IP地址的mac地址 找了很久 终于找到问题 写下来分享一
  • Microsoft SQL Server Management Studio附加数据库时出错。有关详细信息,请单击“消息”列中的超链接。

    附加数据库时出错 有关详细信息 请单击 消息 列中的超链接 导入别人的项目时导入mdf和ldf数据库文件时 附加数据库出错 这是由于文件夹的权限问题 详细解决办法如下图所示 我们编辑ldf和mdf文件的上级目录数据库文件夹 gt 数据库文件
  • SPRINGBOOT的常用注解

    Spring是用来管理业务层的框架 通过集成持久层框架也可以用来管理持久层 主要的两大功能是控制反转和面向切面编程 主要目的是实现程序的解耦 SpringMVC是用来管理控制层的框架 主要是实现和WEB的交互 注意不要直接在控制层编写业务代
  • vue antv X6流程图

    第一 下载2 0插件 第二 引入代码