如何使用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
    评论
你好,感谢您使用ChitGPT进行对话。针对您的问题,您可以使用AntV G6提供的registerEdge() API来实现根据节点状态改变连线颜色的效果。具体实现方式如下: 1. 在定义节点时,可以为每个节点添加状态属性,比如: ``` const nodes = [ { id: 'node1', label: 'Node 1', status: 'normal' }, { id: 'node2', label: 'Node 2', status: 'normal' }, { id: 'node3', label: 'Node 3', status: 'error' } ] ``` 2. 定义连线时,可以通过调用registerEdge()注册一个自定义的edge类,为每条连线设置绘制时的颜色,同时可以通过source和target节点的状态属性来决定使用哪种颜色。示例代码如下: ``` const customEdge = { draw(cfg, group) { let strokeColor = '#aaaaaa'; // 默认颜色 const source = cfg.sourceNode.getModel(); const target = cfg.targetNode.getModel(); if (source.status === 'normal' && target.status === 'normal') { strokeColor = '#1890ff'; // 使用蓝色 } else if (source.status === 'error' || target.status === 'error') { strokeColor = '#ff4d4f'; // 使用红色 } const shape = group.addShape('path', { attrs: { stroke: strokeColor, lineAppendWidth: 10, path: this.getPath(cfg) } }); return shape; } }; G6.registerEdge('customEdge', customEdge); ``` 3. 在实例化G6图的时候,将edge使用定义的自定义edge类替换就可以了: ``` const graph = new G6.Graph({ container: 'container', width: 500, height: 500, defaultEdge: { type: 'customEdge' } }); ``` 希望这个回答对您有帮助。如果您还有其他问题,可以继续问我哦!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值