官方文档
官方文档链接(看完之后的感觉是我懂了,但没完全懂。)
具体使用
业务需求
- 做数据血缘,大概类似做ER图吧,一开始想使用echarts拓扑图来做,但是echarts有个定位问题一直没弄明白,找不到办法解决,echarts的节点需要自定义位置,但是我们是动态数据,且力引导算法不满足业务需求,所以最后选择了antv g6。
主要问题
1、需要自定义节点,g6使用自定义dom节点满足改需求,但是根据文档提供,dom节点无法使用g6提供的点击事件。
- 通过配置节点type,使节点指向某自定义节点。
// 当前节点高亮 通过type匹配自定义节点
if (this.data.nodes[i].highLight == 1) {
this.data.nodes[i].type = 'center'
} else {
this.data.nodes[i].type = 'dom-node'
}
- 自定义dom节点
G6.registerNode(
'dom-node', {
draw: (cfg, group) => {
// console.log(cfg, group)
const shape = group.addShape('dom', {
attrs: {
width: cfg.size[0],
height: cfg.size[1],
// 传入 DOM 的 html
html: `
<div onclick="select(${cfg.name})" id="${cfg.select}" class="dom-node-style" style="cursor:pointer; border-radius: 5px; width: ${
cfg.size[0] - 5
}px; height: ${cfg.size[1] - 5}px; display: flex;">
<span style="margin:auto; padding:auto; color: #000">${cfg.id}</span>
</div> `,
},
draggable: true,
});
return shape;
},
},
'single-node',
);
// 当前表节点
G6.registerNode(
'center', {
draw: (cfg, group) => {
// console.log(cfg)
const shape = group.addShape('dom', {
attrs: {
width: cfg.size[0],
height: cfg.size[1],
// 传入 DOM 的 html ${cfg.isActive ? "class='selected-style'" : "class='node-style'"}"
html: `
<div onclick="select(${cfg.name})" id="${cfg.select}" class="node-style" style="width: ${
cfg.size[0] - 5
}px; height: ${cfg.size[1] - 5}px; display: flex;border-radius: 5px;cursor:pointer;">
<span style="margin:auto; padding:auto; color: #000">${cfg.id}<