VUE中 用AntV G6 + element 实现关系图展示

本文介绍如何使用AntV G6库创建字段血缘关系展示的思维导图,并通过实例代码展示了如何初始化、渲染数据及实现动态数据更新。通过`handleBlood`方法,用户可以触发血缘关系在el-dialog中实时显示,依赖后台接口getColumnLineage获取数据并重新绘制图谱。
摘要由CSDN通过智能技术生成

1.效果图 (我的需求是做字段血缘展示)

2.官方文档 :https://antv-g6.gitee.io/zh/examples/tree/mindmap#hCustomSideMindmap

3.

3.1

npm install @antv/g6 --save

3.2

 <div id="mountNode" ></div>

3.3 method

initG6() {
      console.log(this.data)
      const nodes = this.data.nodes
      // 创建 G6 图实例

      this.graph = new G6.Graph({
        container: 'mountNode',  // 画布容器id
        width: 1400,  // 画布宽度
        height: 1000,    // 画布高度
        modes: {
          default: ['drag-canvas', 'click-select']
        },
        layout: {
          type: 'mindmap',
          direction: 'H',
          align: 'UL',
          nodesep: 50,
          ranksep: 50,  //层次距离
          controlPoints: true,
        },
        //默认节点样式设置
        defaultNode: {
          size: [150, 50],
          type: 'rect',
          color: "#ffffff",
          style: {
            lineWidth: 2,
            stroke: '#5B8FF9',
            fill: '#C6E5FF',
            radius: 5
          },
          labelCfg: {
            style: {
              fontSize: 12,
            },
          },
        },
        // 默认的边样式设置
        defaultEdge: {
          type: 'polyline',
          size: 1,
          color: '#c8c8c8',
          style: {
            endArrow: {
              path: 'M 0,0 L 8,4 L 8,-4 Z',
              fill: '#c8c8c8',
            },
            radius: 5,
          },
        },
      })
      this.chart = this.graph
      this.graph.data(this.data)  // 读取数据
      this.graph.render()  // 渲染数据
      this.graph.on('click', (ev) => {  // 监听节点点击事件
        this.evt = ev.item
        const shape = ev.target;
        if (ev.item) {
          const item = ev.item;
          const type = item.getType();
          // console.log('点击节点shape,item,type',shape,item,type)
          this.selectedItem = ev.item._cfg.model
          console.log('treecomplex click 当前节点信息', this.selectedItem)
        }
      });
      if (typeof window !== 'undefined')
        window.onresize = () => {
          if (!this.graph || this.graph.get('destroyed')) return;
          if (!mountNode || !mountNode.scrollWidth || !mountNode.scrollHeight) return;
          this.graph.changeSize(mountNode.scrollWidth, mountNode.scrollHeight);
        };
    },

3.4

  mounted() {
    this.initG6();

  }

3.5  销毁画布  定义 chart

    // 避免多次渲染数据 销毁画布
      if (this.chart !== '') {
        this.chart.destroy()
      }

3.6 针对我的需求 设计

 

 写了一个血缘的按钮 ,点击后 血缘关系在对话框 el-dialog 上弹框展示

 点击后的方法:

    handleBlood(data) {
      this.bloodVisible = true;
      // 避免多次渲染数据 销毁画布
      if (this.chart !== '') {
        this.chart.destroy()
      }
      getColumnLineage({id: data.id}).then(res => {
        this.data = res.result;
        this.initG6()
      })
    },
getColumnLineage 是请求后端的接口,我的血缘关系是根据不同入参动态展示的,所以data信息就不给大家粘贴啦

有人问数据结构,我把接口的返回值粘在着了,可以参考,这个xy位置、style都是后端计算好返回的

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值