当前位置: 首页 > news >正文

vue3+ts使用antv/x6

使用 2.x 版本 x6.antv 新官网:

安装

npm install @antv/x6
//"@antv/x6": "^2.1.6",

项目结构
在这里插入图片描述

1、初始化画布 index.vue
<template><div id="container"></div>
</template><script setup lang='ts'>
import { onMounted } from "vue";
import { Graph } from '@antv/x6';
let graph:Graph
const graphInit = ()=>{graph = new Graph({container: document.getElementById('container')!,});
}onMounted(()=>{graphInit()
})
</script><style scoped>
#container{width: 100vw;height: 100vh;
}
</style>
2、注册节点

渲染 Vue 节点,这个文档完全够用

npm install @antv/x6-vue-shape
//"@antv/x6-vue-shape": "^2.0.9",

节点node.vue

<template><div class="nodeitem">{{ data?.nodeName }}</div>
</template>
<script setup lang='ts'>
import { inject, onMounted,ref } from "vue";
import { Node } from '@antv/x6'interface NodeDTO {nodeId?: stringnodeName: string
}const getNode: Function | undefined = inject<Function>("getNode");
const data =  ref<NodeDTO|undefined>(undefined)
onMounted(() => {const node = getNode?.() as Node;data.value = node?.getData()
});
</script><style scoped>
.nodeitem{width:100px;border: 1px solid #ccc;
}
</style>
3、在画布引入并注册自定义节点,配置节点信息

主画布:index.vue

<template><div id="container"></div><TeleportContainer/>
</template><script setup lang='ts'>
import { onMounted } from "vue";
import { Graph,Cell } from '@antv/x6';
import NodeItem from "./node.vue";
import {register,getTeleport} from '@antv/x6-vue-shape'
let graph:Graph
register({shape: "node-item",width: 150,height: 100,component: NodeItem,
});// 注册自定义节点
const TeleportContainer = getTeleport();// 自定义节点优化
const refreshData = (data)=>{//渲染节点数据const cells: Cell[] = []data.nodes.forEach((item: any) => {cells.push(graph.createNode(item))})data.edges?.forEach((item: any) => {cells.push(graph.createEdge(item))})graph.resetCells(cells)graph.centerContent()graph.zoomToFit({ padding: 10, maxScale: 1 })
}
const graphInit = ()=>{graph = new Graph({container: document.getElementById('container')!,});let data = {nodes: [{id: 'node1', // String,可选,节点的唯一标识shape: 'node-item',x: 40,       // Number,必选,节点位置的 x 值y: 40,       // Number,必选,节点位置的 y 值data: {nodeId: 'node1',nodeName: '节点1',},}],edges:[]}refreshData(data)
}
onMounted(()=>{graphInit()
})
</script>

展示
在这里插入图片描述
小小预告:
下一篇 自定义节点样式
再下一篇 侧边栏
再下一篇 整理画布

http://www.lryc.cn/news/119274.html

相关文章:

  • wsl1 ubuntu通过宿主机代理连接外网
  • ubuntu20.04 opencv4.2 安装笔记
  • ubuntu安装nginx以及php的部署
  • IntelliJ IDEA 2021/2022关闭双击shift全局搜索
  • HTML 元素中的name 属性
  • 快速上手React:从概述到组件与事件处理
  • K8S系列文章之 离线安装自动化工具Ansible
  • mysql8.0.3集群搭建
  • vue中router路由的原理?两种路由模式如何实现?(vue2) -(上)
  • 消息队列(3) -封装数据库的操作
  • PostgreSQL中根据时间段范围查询数据,如19:29:10到20:29:10范围内的数据,排除年月日
  • 【二分+贪心】CF1665 C
  • 【Wamp】安装 | 局域网内设备访问
  • 【golang】类型推断和变量重声明
  • “算法详解”系列第3卷贪心算法和动态规划出版
  • CSS前端开发指南:创造精美的用户界面
  • 代数学与理论物理中常见的群
  • 解析xml文件,获取需要的数据并写入txt文件中
  • JavaScript基础 第三天
  • 2.Redis部署到Windows服务器
  • 【修正-高斯拉普拉斯滤波器-用于平滑和去噪】基于修正高斯滤波拉普拉斯地震到达时间自动检测研究(Matlab代码实现)
  • Go语言基础: 有参函数Func、Map、Strings详细案例教程
  • JDBC连接数据库如何实现你会吗???
  • C#与C++交互(2)——ANSI、UTF8、Unicode文本编码
  • SQLSTATE[42000]: this is incompatible with sql_mode=only_full_group_by in
  • 企业权限管理(五)-订单分页
  • Blender如何给fbx模型添加材质贴图并导出带有材质贴图的模型
  • MySQL不走索引的情况分析
  • 安装ubuntu22.04系统,配置国内源以及ssh远程登录
  • win10 安装ubuntu子系统并安装宝塔