如何使用antvg6自定义节点

使用antvg6绘制树状图但是节点样式不符合要求,如何自定义呢?
查找文档参考这个例子 https://g6.antv.antgroup.com/examples/item/customNode/#card

下面是我写的代码

   G6.registerNode(
        'flow-circle',
        {
          /**
           * 绘制节点,包含文本
           * @param  {Object} cfg 节点的配置项
           * @param  {G.Group} group 图形分组,节点中图形对象的容器
           * @return {G.Shape} 返回一个绘制的图形作为 keyShape,通过 node.get('keyShape') 可以获取。
           * 关于 keyShape 可参考文档 核心概念-节点/边/Combo-图形 Shape 与 keyShape
           */
          draw(cfg, group) {
            const { name, childrenNum } = cfg
            const circleConfig = {
              width: 20,
              height: 20,
              lineWidth: 1,
              fontSize: 12,
              stroke: 'rgba(255,117,0, 1)',
              opacity: 1,
              r: 20,
              fill: 'rgba(255,117,0, 0.9)',
              cursor: 'pointer'
            }

            const nodeOrigin = {
              x: -circleConfig.width / 2,
              y: -circleConfig.height / 2
            }

            const textConfig = {
              textAlign: 'center',
              textBaseline: 'bottom',
              cursor: 'pointer',
              opacity: 1,
              fontSize: 16
            }
            /** 绘制大圆点 */
            const circle = group.addShape('circle', {
              attrs: {
                ...circleConfig,
                x: 0,
                y: 0
              },
              name: 'big-dot-shape'
            })

            /** 子节点数量 */
            group.addShape('text', {
              attrs: {
                ...textConfig,
                x: nodeOrigin.x + 10,
                y: nodeOrigin.y + 20,
                text: childrenNum,
                fill: '#fff'
              },
              name: 'childNum-shape'
            })

            /** 节点lable */
            const text = group
              .addShape('text', {
                attrs: {
                  ...textConfig,
                  // x: nodeOrigin.x + 10,
                  // y: nodeOrigin.y + 60,
                  x: nodeOrigin.x - 5,
                  y: nodeOrigin.y + 15,
                  text: name,
                  fill: '#000',
                  textAlign: 'end'
                },
                name: 'lable-shape'
              })
              .translate(-10, 6)
            // 三个顶级节点的文字不用转角度
            if (cfg.id !== '-1') {
              text.rotate(-0.4)
            }

            return circle
          },
          /**
           * 更新节点,包含文本
           * @override
           * @param  {Object} cfg 节点的配置项
           * @param  {Node} node 节点
           */
          update(cfg, node) {
            const { childrenNum, name } = cfg
            // 更新子节点数量
            const group = node.getContainer()
            const childNumShape = group.find(ele => ele.get('name') === 'childNum-shape')
            childNumShape.attrs.text = childrenNum

            // 更新所有的label
            const nameShape = group.find(ele => ele.get('name') === 'lable-shape')
            nameShape.attrs.text = name
            this.updateLinkPoints(cfg, group)
          },
          setState(name, value, item) {
            const group = item.getContainer()
            const bigDotShap = group.find(e => e.get('name') === 'big-dot-shape')
            if (name === 'highlight') {
              bigDotShap.attr({
                stroke: 'rgba(95,149,255, 1)',
                fill: 'rgba(95,149,255, 1)'
              })
            } else if (name === 'dark') {
              bigDotShap.attr({
                stroke: 'rgba(255,117,0, 1)',
                fill: 'rgba(255,117,0, 0.9)'
              })
            }
          }
        },
        'circle'
      )

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值