• antv/x6 自定义html节点并且支持动态更新节点内容


    效果图

    在这里插入图片描述

    定义一个连接桩公共方法

    const ports = {
      groups: {
        top: {
          position: 'top',
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: '#cf1322',
              strokeWidth: 1,
              fill: '#fff',
              style: {
                visibility: 'visible',
              },
            },
          },
        },
        right: {
          position: 'right',
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: '#389e0d',
              strokeWidth: 1,
              fill: '#fff',
              style: {
                visibility: 'visible',
              },
            },
          },
        },
        bottom: {
          position: 'bottom',
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: '#389e0d',
              strokeWidth: 1,
              fill: '#fff',
              style: {
                visibility: 'visible',
              },
            },
          },
        },
        left: {
          position: 'left',
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: '#cf1322',
              strokeWidth: 1,
              fill: '#fff',
              style: {
                visibility: 'visible',
              },
            },
          },
        },
      },
      items: [
        {
          group: 'top',
        },
        {
          group: 'right',
        },
        {
          group: 'bottom',
        },
        {
          group: 'left',
        },
      ],
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    注册图形节点

    Shape.HTML.register({
      shape: 'html',
       width: 70,
       height: 36,
       effect: ['data'],
       html(cell) {
         const { label, props } = cell.getData()
         const div = document.createElement('div')
         div.style.width = 70
         const titleDiv = document.createElement('div')
         titleDiv.style.width = '70px'
         titleDiv.style.height = '36px'
         titleDiv.style.background = '#eb2f96'
         titleDiv.style.color = 'white'
         titleDiv.style.fontSize = '14px'
         titleDiv.style.textAlign = 'center'
         titleDiv.style.lineHeight = '36px'
         titleDiv.style.boxSizing = 'border-box'
         titleDiv.style.fontSize = '12px'
         titleDiv.style.borderRadius = '6px'
         titleDiv.style.whiteSpace = 'nowrap'
         titleDiv.style.overflow = 'hidden'
         titleDiv.style.textOverflow = 'ellipsis'
         titleDiv.setAttribute('title', label)
         titleDiv.textContent = label
         div.append(titleDiv)
         return div
       },
       ports: { 
         ...ports,
         items: [
           {
             group: 'left'
           },
           {
             group: 'right'
           }
         ]
       },
     })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    1. effect 是当前节点的 prop 数组,当 effect 包含的 prop 有变动时,会重新执行 html 方法,返回新的 dom,更新节点内容;
    2. ports 是此节点的连接桩;此节点只用到了左右两个连接桩;

    创建html节点

    const r2 = this.graph.createNode({
     shape: 'html',
      data: {
        props:{
          desc: ''
        },
        label: '自定义html',
      },
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. shape 要和注册节点里的名称一致;

    动态更新节点内容

    let cell = this.graph.getCellById(id)
    cell.prop('data/label', '文字')
    cell.prop('data/props/voice', 'desc')
    
    • 1
    • 2
    • 3
    • id 是要更改内容的cell的id;
  • 相关阅读:
    WinUI 3 踩坑记:前言
    《Java》【速学】对象-多态
    跨时钟域问题(三)异步FIFO的Verilog实现(格雷码)
    c 语言基础:L1-047 装睡
    【Java】一文认识IO操作流
    深度讲解React Props
    【C刷题】day7
    JAVA -- 把一个大的sql文件分割成多个小sql文件
    switch case 语句(详细)
    【数据结构与算法】二叉树的链式访问
  • 原文地址:https://blog.csdn.net/qq_35517283/article/details/134245959