antvX6使用分享(绘制流程图)

首先附上官方文档:https://x6.antv.antgroup.com/tutorial/about

1.安装依赖

# npm
$ npm install @antv/x6 --save

# yarn
$ yarn add @antv/x6

2. 开始使用

1.创建画布

X6 支持 JSON 格式数据,该对象中 nodes 代表节点数据,edges 代表边数据,可以使用 attrs 属性来定制节点和边的样式(可以类比 CSS)。

<div id="container"></div>
onMounted(() => {
  const graph = new Graph({
    container: document.getElementById('container') as HTMLElement,
    width: 800,
    height: 600,
    background: {
      color: '#F2F7FA',
    },

  })
  graph.fromJSON(data) // 渲染元素
  graph.centerContent() // 居中显示
})

2. 渲染节点和边

X6 支持 JSON 格式数据,该对象中 nodes 代表节点数据,edges 代表边数据,可以使用 attrs 属性来定制节点和边的样式(可以类比 CSS)。

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,
        },
      },
    },
  ],
}

使用Vue配合AntV X6绘制流程图时,`ref`是一个非常重要的属性,它允许我们在Vue组件中引用DOM元素或子组件实例。通过`ref`,我们可以获取到AntV X6的实例,从而进行一系列的操作,如初始化、添加节点、添加边等。 以下是一个简单的示例,展示了如何在Vue中使用`ref`配合AntV X6绘制流程图: 1. **安装依赖**: 首先,确保你已经安装了`antv-x6`和`vue`。你可以使用npm或yarn进行安装: ```bash npm install antv-x6 npm install vue ``` 2. **创建Vue组件**: 创建一个新的Vue组件,例如`FlowChart.vue`,并在其中使用AntV X6绘制流程图。 ```vue <template> <div ref="container" style="width: 100%; height: 600px; border: 1px solid #ccc;"></div> </template> <script> import { onMounted, ref } from 'vue'; import { Graph } from '@antv/x6'; export default { name: 'FlowChart', setup() { const container = ref(null); let graph = null; onMounted(() => { // 初始化Graph实例 graph = new Graph({ container: container.value, width: 800, height: 600, grid: true, background: { color: '#ffffff', }, }); // 添加节点 const source = graph.addNode({ x: 100, y: 100, width: 100, height: 40, label: '开始', attrs: { body: { fill: '#f5f5f5', stroke: '#ccc', }, }, }); const target = graph.addNode({ x: 300, y: 100, width: 100, height: 40, label: '结束', attrs: { body: { fill: '#f5f5f5', stroke: '#ccc', }, }, }); // 添加边 graph.addEdge({ source, target, attrs: { line: { stroke: '#000', }, }, }); }); return { container, }; }, }; </script> <style scoped> </style> ``` 3. **使用组件**: 在你的主应用或父组件中使用`FlowChart`组件。 ```vue <template> <div> <FlowChart /> </div> </template> <script> import FlowChart from './components/FlowChart.vue'; export default { name: 'App', components: { FlowChart, }, }; </script> <style> </style> ``` 通过以上步骤,你就可以在Vue中使用`ref`配合AntV X6绘制一个简单的流程图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值