AntV G6节点实时更新

在项目的实现过程中,需要实现节点的实时更新,即:后台返回的数据可能是实时变化的,所以前端的拓扑图节点也需要有一个实时变化的效果,所以对节点的更新做了如下操作:

代码布局如下:

主要代码如下:<我这边只修改了特定的某一个节点,真实项目中肯定不能这样写,可参考注释中watch下的写法>

 /**
       * 更新节点数据<我这边例子是取第一个节点的数据进行更新,因为我的数据是前端写的假数据,不是实时更新的>
       * 如果真的在项目中,数据是实时更新的,可以在watch中写这段代码,通过for循环对数据进行遍历更新,大致代码如下,如果不正常的话适当微调即可
       * watch: {
    'this.data.nodes'(val, oldVal) {
      if(val) {
          const that = this
        val.forEach(function(value, index, array) {
          const el = that.graph.findById(value.id)
          console.log('model', el._cfg.model)
          console.log('value', value)
          el._cfg.model.id = value.id
          el._cfg.model.label = value.label
          el._cfg.model.ip= value.ip
          el._cfg.model.status= value.status
          if (value.status == 1) { /
            el._cfg.model.img = '.....'
          }
          if (value.status == 0) {
            el._cfg.model.img = '.....'
          }
          that.graph.refreshItem(el)
        })
      }
      }
  },
       */
      const el = this.graph.findById(this.data.nodes[0].id)
      console.log('model', el)
      el._cfg.model.label = '哈哈哈,我变成了可爱的小奶龙'
      el._cfg.model.ip = '192.168.....'
      el._cfg.model.status = 4
      el._cfg.model.img = 'https://img1.baidu.com/it/u=1049473449,988642504&fm=253&fmt=auto&app=120&f=JPEG?w=328&h=328'
      this.graph.refreshItem(el)

完整代码:

<template>
  <!--设置parentContent的宽高为浏览器大小-->
  <div class="parentContent" ref="parentContent">
    <div id="container" ref="container"></div>
  </div>
</template>
<script>
import G6 from '@antv/g6'

export default {
  name: 'g6',
  mounted () {
    this.initComponent()
    this.initSize()
  },
  data () {
    return {
      data: {}, // 拓扑图数据
      graph: undefined, // new G6
      canvasWidth: 0, // 画布宽度
      canvasHeight: 0 // 画布高度
    }
  },
  methods: {
    /**
       * 设置画布大小自适应
       */
    initSize () {
      const self = this // 因为箭头函数会改变this指向,指向windows。所以先把this保存
      setTimeout(() => {
        // todo 浏览器窗口发生变化时
        window.onresize = function () {
          // todo 获取div parentContent 的宽度和高度
          this.canvasWidth = self.$refs.parentContent.clientWidth
          this.canvasHeight = self.$refs.parentContent.clientHeight
          // todo 修改画布的大小
          self.graph.changeSize(this.canvasWidth, this.canvasHeight)
          // todo 将图移动到画布中心位置
          self.graph.fitCenter()
        }
      }, 20)
    },
    /**
       * 创建G6,并对G6的一些设置
       * */
    initComponent () {
      this.data = {
        nodes: [
          {
            id: 'node1',
            label: '采集服务器',
            ip: '192.168.1.1',
            status: 0
            // 此处的key值一定不要出现type,如果出现type,图片修改无效
          },
          {
            id: 'node2',
            label: '数据库',
            ip: '192.168.1.2',
            status: 1
          },
          {
            id: 'node3',
            label: '终端',
            ip: '192.168.1.3',
            status: 2
          },
          {
            id: 'node4',
            label: '引擎',
            ip: '192.168.1.4',
            status: 0
          },
          {
            id: 'node5',
            label: '引擎5',
            ip: '192.168.1.4',
            status: 0
          },
          {
            id: 'node6',
            label: '引擎6',
            ip: '192.168.1.4',
            status: 2
          },
          {
            id: 'node7',
            label: '引擎7',
            ip: '192.168.1.4',
            status: 1
          },
          {
            id: 'node8',
            label: '引擎8',
            ip: '192.168.1.4',
            status: 2
          },
          {
            id: 'node9',
            label: '引擎9',
            ip: '192.168.1.4',
            status: 1
          },
          {
            id: 'node10',
            label: '引擎10',
            ip: '192.168.1.4',
            status: 0
          }
        ],
        edges: [
          {
            source: 'node1',
            target: 'node2'
          },
          {
            source: 'node1',
            target: 'node3'
          },
          {
            source: 'node1',
            target: 'node4'
          },
          {
            source: 'node4',
            target: 'node5'
          },
          {
            source: 'node1',
            target: 'node6'
          },
          {
            source: 'node6',
            target: 'node7'
          },
          {
            source: 'node7',
            target: 'node8'
          },
          {
            source: 'node1',
            target: 'node8'
          },
          {
            source: 'node2',
            target: 'node9'
          },
          {
            source: 'node3',
            target: 'node10'
          }
        ]
      }
      /**
         * 遍历节点数据,给节点添加图片
         */
      for (let i = 0, len = this.data.nodes.length; i < len; i++) {
        // eslint-disable-next-line eqeqeq
        if (this.data.nodes[i].status == 0) { // 'offlineAnomaly'
          this.data.nodes[i].img = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp05%2F19100122420C335-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641970684&t=70b9b0b3a05f6ca5d56d6c4234fdd1fd'
        }
        // eslint-disable-next-line eqeqeq
        if (this.data.nodes[i].status == 1) { // 'onlineAuth'
          this.data.nodes[i].img = 'https://yyb.gtimg.com/aiplat/page/product/visionimgidy/img/demo6-16a47e5d31.jpg?max_age=31536000'
        }
        // eslint-disable-next-line eqeqeq
        if (this.data.nodes[i].status == 2) { // 'onlineAuth'
          this.data.nodes[i].img = 'https://img0.baidu.com/it/u=3927459320,2138990686&fm=26&fmt=auto'
        }
      }
      // todo 设置鼠标悬停显示详情操作
      const tooltip = new G6.Tooltip({
        offsetX: 70,
        offsetY: 20,
        getContent (e) {
          const outDiv = document.createElement('div')
          outDiv.style.width = '180px'
          outDiv.innerHTML = `
            <ul id="nodeDetails">
              <li>名称: ${e.item.getModel().label}</li>
              <li>IP: ${e.item.getModel().ip}</li>
              <li>ID: ${e.item.getModel().id}</li>
              <li>status: ${e.item.getModel().status}</li>
            </ul>`
          return outDiv
        },
        itemTypes: ['node']
      })
      // todo 初始化画布宽高为div parentContent 的宽度和高度
      this.canvasWidth = this.$refs.parentContent.clientWidth
      this.canvasHeight = this.$refs.parentContent.clientHeight
      this.graph = new G6.Graph({
        container: 'container',
        width: this.canvasWidth,
        height: this.canvasHeight,
        linkCenter: true,
        plugins: [tooltip], // 配置 Tooltip 插件
        modes: {
          default: ['drag-canvas', 'zoom-canvas', 'drag-node'] // 允许拖拽画布、放缩画布、拖拽节点
        },
        layout: {
          type: 'force',
          preventOverlap: true, // 防止节点重叠
          // 防碰撞必须设置nodeSize或size,否则不生效,由于节点的size设置了40,虽然节点不碰撞了,但是节点之间的距离很近,label几乎都挤在一起,所以又重新设置了大一点的nodeSize,这样效果会好很多
          nodeSize: 100,
          linkDistance: 150 // 指定边距离为150
        },
        defaultNode: { // 节点样式修改
          type: 'image', // 设置节点为图片
          size: [40, 40], // 节点大小
          labelCfg: { // 修改节点label样式
            style: {
              fill: '#5B8FF9', // 字体颜色
              fontSize: 14 // 字体大小
            }
          }
        }
      })
      // 接收数据并渲染
      this.graph.data(this.data)
      this.graph.render()
      /**
       * 更新节点数据<我这边例子是取第一个节点的数据进行更新,因为我的数据是前端写的假数据,不是实时更新的>
       * 如果真的在项目中,数据是实时更新的,可以在watch中写这段代码,通过for循环对数据进行遍历更新,大致代码如下,如果不正常的话适当微调即可
       * watch: {
    'this.data.nodes'(val, oldVal) {
      if(val) {
          const that = this
        val.forEach(function(value, index, array) {
          const el = that.graph.findById(value.id)
          console.log('model', el._cfg.model)
          console.log('value', value)
          el._cfg.model.id = value.id
          el._cfg.model.label = value.label
          el._cfg.model.ip= value.ip
          el._cfg.model.status= value.status
          if (value.status == 1) { /
            el._cfg.model.img = '.....'
          }
          if (value.status == 0) {
            el._cfg.model.img = '.....'
          }
          that.graph.refreshItem(el)
        })
      }
      }
  },
       */
      const el = this.graph.findById(this.data.nodes[0].id)
      console.log('model', el)
      el._cfg.model.label = '哈哈哈,我变成了可爱的小奶龙'
      el._cfg.model.ip = '192.168.....'
      el._cfg.model.status = 4
      el._cfg.model.img = 'https://img1.baidu.com/it/u=1049473449,988642504&fm=253&fmt=auto&app=120&f=JPEG?w=328&h=328'
      this.graph.refreshItem(el)
    }
  }
}
</script>

<style>
  .parentContent {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
  }

  #nodeDetails {
    list-style: none;
  }

  #nodeDetails > li {
    padding: 5px 0;
    text-align: left;
  }
</style>

参考地址

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 21
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值