信息发布→ 登录 注册 退出

Vue.js 使用AntV X6的示例步骤

发布时间:2026-01-11

点击量:
目录
  • 0x0 前言
  • 0x1 安装
  • 0x2 节点侧边栏
  • 0x3 整合例子

0x0 前言

因为项目用到流程图,并且需求也算是不详细,所以选择比较灵活的 x6 图形编辑器作为流程图编辑器,从文档来看不算复杂,这边就是作为参考教程。

Antv X6 文档

0x1 安装

根据教程提示安装 x6 依赖即可,然后新建个容器进行实例化:

<div ref="containerRef" class="area-center-container" />
const data = {
  // 节点
  nodes: [
    {
      id: 'node1', // String,可选,节点的唯一标识
      x: 40,       // Number,必选,节点位置的 x 值
      y: 40,       // Number,必选,节点位置的 y 值
      width: 80,   // Number,可选,节点大小的 width 值
      height: 40,  // Number,可选,节点大小的 height 值
      label: 'hello', // String,节点标签
    },
    {
      id: 'node2', // String,节点的唯一标识
      x: 160,      // Number,必选,节点位置的 x 值
      y: 180,      // Number,必选,节点位置的 y 值
      width: 80,   // Number,可选,节点大小的 width 值
      height: 40,  // Number,可选,节点大小的 height 值
      label: 'world', // String,节点标签
    },
  ],
  // 边
  edges: [
    {
      source: 'node1', // String,必须,起始节点 id
      target: 'node2', // String,必须,目标节点 id
    },
  ],
}

function initGraph() {
  const graph = new Graph({
    container: this.$refs.containerRef,
    grid: {
      size: 10, // 网格大小 10px
      visible: true // 渲染网格背景
    },
    snapline: {
      enabled: true, // 对齐线
      sharp: true
    },
    scroller: {
      enabled: true,
      pageVisible: false,
      pageBreak: false,
      pannable: true
    }
  })
  // 画布居中
  graph.centerContent()

  graph.fromJSON(data)
}

就这样最简单例子实现了,上面不同的参数请参考文档对应的解释。

0x2 节点侧边栏

根据文档的 stencil 例子,可以简化很多代码量,直接用封装好的业务就行了,和上面一样直接写个容器实例化即可:

<el-aside ref="stencilRef" class="area-left" />
this.stencil = new Stencil({
    title: '流程节点侧边栏',
    target: graph,
    search: false,
    collapsable: true,
    stencilGraphWidth: this.$refs.stencilRef.$el.clientWidth,
    stencilGraphHeight: this.$refs.stencilRef.$el.clientHeight,
    groups: [
        {
            name: 'group',
            title: '流程图节点',
            collapsable: false
          }
        ],
    getDropNode: node => {
        let cloneNode = node.clone()
        switch (node.shape) {
            case 'rect':
                cloneNode = new RectShape()
                break
            case 'circle':
                cloneNode = new CircleShape()
                break
            case 'polygon':
                cloneNode = new PolylineShape()
                break
        }
        cloneNode.updateInPorts(graph)
        return cloneNode
    }
})
// 加载节点
this.stencil.load([new Rect(rectInfo), new Circle(circleInfo), new Polygon(polygonInfo)], 'group')

0x3 整合例子

在线:https://codesandbox.io/s/icy-meadow-rqihx

以上就是Vue.js 使用Antv X6的示例步骤的详细内容,更多关于Vue.js 使用 Antv X6的资料请关注其它相关文章!

标签:# 实现了  # source  # target  # Number  # width  # id  # String  # px  # height  # label  # 就行了  # 加载  # Vue  # 装好  # 请参考  # 更多关于  # 最简单  # 相关文章  # 编辑器  # 文档  # 可选  # vue3 + antv/x6实现流程图的全过程  # X6  # 使用AntV  
在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!