antv/x6使用vue组件渲染节点

1.antv x6 提供了一个独立的包 @antv/x6-vue-shape 来使用 Vue 组件渲染节点。

需要注意的是,x6 的版本要和 x6-vue-shape 的版本匹配,也就是两个包需要用同一个大版本。

npm install @antv/x6-vue-shape --save

2.vue节点组件

<template>
  <div class="rounded-corner">
    <div class="content">
      这是一段文字
    </div>
  </div>
</template>

<script lang="ts" setup>

</script>

<style scoped>
.rounded-corner {
  background-color: purple;
  height: 50px;
  width: 100px;
  border-radius: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.content {
  color: white;
  font-size: 14px;
}
</style>

2.注册vue节点,并创建流程图数据 

<template>
  <div id="container"></div>
</template>
<script lang="ts" setup>
import TestNode from './TestNode.vue'
import { onMounted } from 'vue'
import { Graph } from '@antv/x6'
import { register, getTeleport } from '@antv/x6-vue-shape'
// 填充流程图数据,包括节点和边(data=>nodes=>shape表示节点,此处可以使用自定义的vue节点)
const data = {
  nodes: [
    {
      id: 'node1',
      shape: 'custom-vue-node',
      x: 40,
      y: 40,
    },
    {
      id: 'node2',
      shape: 'custom-vue-node',
      x: 160,
      y: 180,
    },
  ],
  edges: [
    {
      shape: 'edge',
      source: 'node1',
      target: 'node2',
      label: 'x6',
      attrs: {
        // line 是选择器名称,选中的边的 path 元素
        line: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
        },
      },
    },
  ],
}


onMounted(() => {
  // 创建画布
  const graph = new Graph({
    container: document.getElementById('container') as HTMLElement,
    width: 800,
    height: 600,
    background: {
      color: '#F2F7FA',
    },

  })
  // 注册vue组件节点
  register({
    shape: 'custom-vue-node',
    width: 100,
    height: 50,
    component: TestNode,
  })
  // 将节点添加到画布上
  graph.fromJSON(data) // 渲染元素
  graph.centerContent() // 居中显示

})
</script>

### 如何在 AntV X6 中通过业务数据 ID 设置 Node 节点AntV X6流程图开发过程中,可以通过设置 `data` 属性来绑定业务数据到节点中。具体来说,可以在创建节点时将业务数据作为参数传入,并利用这些数据动态调整节点的行为和外观。 以下是详细的实现方式: #### 创建带有业务数据的节点 当初始化一个节点时,可以使用 `Graph.addNode()` 方法,并通过其配置项中的 `data` 字段存储业务数据。例如,假设有一个唯一的业务数据 ID (`businessId`) 和其他关联字段 (如名称、状态),可以这样定义节点[^1]: ```javascript const node = graph.addNode({ x: 100, y: 100, width: 100, height: 40, label: 'Node with Business Data', data: { businessId: 'uniqueBusinessId123', // 业务数据ID name: 'Example Node', status: 'active' // 假设的状态字段 } }); ``` 上述代码片段展示了如何向节点附加额外的业务数据。其中 `businessId` 是唯一标识符,用于后续查询或更新该节点。 #### 动态获取节点及其业务数据 一旦设置了节点的 `data` 属性,便可通过 Graph API 获取特定节点以及它的业务数据。例如,要查找具有某个指定 `businessId` 的节点,可遍历整个图表实例: ```javascript function getNodeByBusinessId(graph, targetBusinessId) { const nodes = graph.getNodes(); return nodes.find(node => node.getData().businessId === targetBusinessId); } // 示例调用 const specificNode = getNodeByBusinessId(graph, 'uniqueBusinessId123'); if (specificNode) { console.log('Found Node:', specificNode); } else { console.log('No matching node found.'); } ``` 此函数实现了基于给定 `businessId` 查找对应节点的功能。 #### 更新节点属性依据业务逻辑变化 如果需要根据外部条件改变节点的表现形式(比如颜色或者标签),可以根据业务数据的变化重新渲染节点。下面是一个简单的例子展示如何更改节点的颜色取决于其状态值: ```javascript graph.on('node:click', ({ node }) => { const { status } = node.getData(); // 提取status字段 let fill; switch(status){ case 'active': fill = '#87d068'; break; // 绿色表示激活状态 default: fill = '#f5f5f5'; // 默认灰色背景 } node.attr('body/fill', fill); // 修改填充色 }); ``` 这里监听了 `'node:click'` 事件,每次点击都会读取当前选中节点内的 `status` 数据,并据此决定应用何种视觉效果。 #### Vue 渲染自定义节点 对于更复杂的 UI 场景,可能希望完全控制节点内部结构甚至嵌套完整的 Web 组件,则需要用到 [@antv/x6-vue-shape](https://github.com/antvis/x6/tree/master/packages/vue-shape)[^3] 插件支持 Vue.js 构建复杂视图的能力。首先需安装依赖包: ```bash npm install @antv/x6-vue-shape vue-class-component classnames --save ``` 接着注册新的形状类并映射至实际使用Vue Component 上面去完成高度定制化的需求。 --- ### 总结 综上所述,在 AntV X6 中能够方便地借助内置机制把任意类型的元信息注入进各个单元格对象之中;同时提供了灵活多样的扩展途径允许开发者针对不同层次的应用需求做出相应的适配方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值