antv g6 决策树,鼠标点击位置与节点位置偏移

在这里插入图片描述
问题:antv g6 决策树,鼠标点击位置与节点位置偏移
原因:父级transfrom影响

现象:鼠标点击位置与节点位置偏移
解决方案:
//添加supportCSSTransform属性时树形组件不受父级transform影响(代码中放开注释即可)

  // graph.get('canvas').set('supportCSSTransform', true) 
<template>
  <div style="transform: scale(1.2)">
    <div id="container"></div>
  </div>
</template>

<script>
import G6 from "@antv/g6";

export default {
  name: "index",
  mounted() {
    // console.log(777777778889);
//  this.treedata=    JSON.parse(JSON.stringify(this.treedata).replace(/"name"/g,'"label"'))
//   this.treedata=   JSON.parse(JSON.stringify(this.treedata).replace(/"sub_folder"/g,'"children"'))
    let nodesizeWidth = 90//节点高
    let nodesizeHeight = 30//节点宽
    let iconSize = 10//icon
    let label = 8//title
    let subLabel = 10//左下
    let sub2Label = 10//右下
    let increaseHeight = 10
    let increaseWidth = 34

    let backgroundColor = 'rgba(255,255,255,0.88)'
    let fontColor = 'rgb(0,0,0)'
    let trueincreaseColor = 'rgb(111,255,115)'
    let increaseFontColor = 'rgb(255,255,255)'
    let falseincreaseColor = 'rgb(255,0,0)'
    let circleColor = 'rgba(17,28,236,0.85)'
    let lineColor = 'rgba(83,124,248,0.85)'
    let strokelineColor = 'rgba(15,52,146,0.85)'//边框颜色
    let heardBackgroundColor = 'rgba(87,128,255,0.65)'//边框颜色
    this.init(increaseHeight, increaseWidth, sub2Label, label, subLabel, iconSize, nodesizeWidth, nodesizeHeight, heardBackgroundColor, strokelineColor, backgroundColor, fontColor, trueincreaseColor, falseincreaseColor, circleColor, lineColor, increaseFontColor);
  },
  // props:["treedata"],

  methods: {
    init(increaseHeight, increaseWidth, sub2Label, label, subLabel, iconSize, nodesizeWidth, nodesizeHeight, heardBackgroundColor, strokelineColor, color, fontColor, trueincreaseColor, falseincreaseColor, circleColor, lineColor, increaseFontColor) {

// root node
      G6.registerNode('root', {
        draw: (cfg, group) => {
          const size = [80, 30];
          const keyShape = group.addShape('rect', {
            attrs: {
              width: size[0],
              height: size[1],
              x: -size[0] / 2,
              y: -size[1] / 2,
              fill: color,
              stroke: strokelineColor,
              radius: 5
            },
            draggable: true,
            name: 'root-keyshape'
          });
          group.addShape('text', {
            attrs: {
              text: `${cfg.ratio}%`,
              fill: fontColor,
              fontSize: 6,
              x: 10 - size[0] / 2,
              y: 3,
            },
            draggable: true,
            name: 'ratio-shape'
          });
          group.addShape('text', {
            attrs: {
              text: `${cfg.label}`,
              fill: fontColor,
              fontSize: 9,
              x: -6,
              y: 0,
            },
            draggable: true,
            name: 'label-shape'
          });
          group.addShape('line', {
            attrs: {
              x1: -6,
              x2: 35,
              y1: 2,
              y2: 2,
              stroke: 'rgba(255, 255, 255, 0.85)',
              lineWidth: 0.5
            },
            draggable: true,
            name: 'divider-shape'
          });
          group.addShape('text', {
            attrs: {
              text: `${cfg.subLabel}`,
              fill: fontColor,
              fontSize: 6,
              x: -6,
              y: 10,
            },
            draggable: true,
            name: 'sublabel-shape'
          });
          return keyShape;
        }
      });

// other node
      G6.registerNode('othernode', {
        draw: (cfg, group) => {
          const size = [nodesizeWidth, nodesizeHeight];
          const keyShape = group.addShape('rect', {
            attrs: {
              width: size[0],
              height: size[1],
              x: -size[0] / 2,
              y: -size[1] / 2,
              fill: color,
              //节点边框颜色
              stroke: strokelineColor,
              // radius: 5,
              size: [270, 80],
            },
            draggable: true,
            name: 'level1node-keyshape'
          });
          // 背景板
          group.addShape('rect', {
            attrs: {
              x: -size[0] / 2,
              y: -size[1] / 2,
              width: size[0],
              height: size[1] / 2,
              // radius: 5,
              // radius: [0, 0, rectConfig.radius, rectConfig.radius],//锐角化
              fill: heardBackgroundColor // 填充色
            }
          })
          //图标
          group.addShape('image', {
            attrs: {
              x: size[0] / 2 - 12,
              y: -size[1] / 2 + 2,
              img: require('@/assets/感叹号中.png'),
              width: iconSize,
              height: iconSize,
              cursor: 'pointer'
            },
            name: 'gt'
          })
          group.addShape('image', {
            attrs: {
              x: size[0] / 2 - 24,
              y: -size[1] / 2 + 3,
              img: require('@/assets/闪电发货.png'),
              width: iconSize,
              height: iconSize,
              cursor: 'pointer'
            },
            name: 'sd'
          })

          group.addShape('text', {
            attrs: {
              text: `${cfg.label}`,
              fill: fontColor,
              fontSize: label,
              x: 10 - size[0] / 2,
              y: -2,
              textAlign: 'left'
            },
            draggable: true,
            name: 'label-shape'
          });

          //左下字
          group.addShape('text', {
            attrs: {
              text: `${cfg.subLabel}`,
              fill: fontColor,
              fontSize: subLabel,
              fontWeight: 800,
              x: 10 - size[0] / 2,
              y: 10,
              textAlign: 'left'
            },
            draggable: true,
            name: 'sublabel-shape'
          });


          group.addShape('rect', {
            attrs: {
              x: 12,
              y: 1,
              width: increaseWidth,
              height: increaseHeight,
              radius: 2,
              fill: cfg.increase ? trueincreaseColor : falseincreaseColor
            },
            draggable: true,
            name: 'ratio-box'
          })
          //右下字
          group.addShape('text', {
            attrs: {
              text: cfg.increase ? `+${cfg.ratio}%` : `-${cfg.ratio}%`,
              fill: increaseFontColor,
              fontSize: sub2Label,
              x: 18,
              y: 3,
              textAlign: 'left',
              textBaseline: 'top'
            },
            draggable: true,
            name: 'ratio-shape'
          });

          // edge end
          group.addShape('line', {
            attrs: {
              x1: -size[0] / 2,
              x2: -size[0] / 2 + 6,
              y1: 0,
              y2: 0,
              lineWidth: 1,
              stroke: lineColor,
            }
          });

          return keyShape;
        },
        update: undefined
      }, 'rect')

// edge
      G6.registerEdge('round-poly', {
        getControlPoints: (cfg) => {
          const {startPoint, endPoint} = cfg;
          return [
            startPoint,
            {
              x: startPoint.x,
              y: endPoint.y
            },
            endPoint
          ];
        }
      }, 'polyline')

      const data = {
        id: 'root',
        label: '利息收入',
        subLabel: '3,283.456',
        ratio: 3,
        children: [
          {
            id: 'child-a',
            label: '平均利息',
            subLabel: '9%',
            ratio: 1,
            increase: true,
            children: [
              {
                id: 'child-a-1',
                label: '平均利息1',
                subLabel: '9%',
                ratio: 1,
                increase: true,
                children: [
                  {
                    id: 'child-a-1-1',
                    label: '平均利息1-1',
                    subLabel: '9%',
                    ratio: 1,
                    increase: true,
                    tootip: {
                      leftData: '800万元',
                      rightData: '700万元'
                    }
                  },
                  {
                    id: 'child-a-1-2',
                    label: '平均利息1-2',
                    subLabel: '9%',
                    ratio: 1,
                    increase: true,
                    tootip: {
                      leftData: '800万元',
                      rightData: '700万元'
                    }
                  }]
              },
              {
                id: 'child-a-2',
                label: '平均利息2',
                subLabel: '9%',
                ratio: 1,
                increase: false,
              }]
          }, {
            id: 'child-b',
            label: '贷款余额',
            subLabel: '1,789,567',
            ratio: 23,
            increase: true,
            children: [{
              id: 'child-b-a',
              label: '投放金额',
              subLabel: '2,385,124',
              ratio: 17,
              increase: true,
              operator: '-',
            }, {
              id: 'child-b-b',
              label: '还款金额',
              subLabel: '595,557',
              ratio: 12,
              increase: true,
            }
            ]
          }, {
            id: 'child-c',
            label: '还款期限',
            subLabel: '7',
            ratio: 23,
            increase: true,
          }
        ]
      };

      G6.Util.traverseTree(data, subtree => {
        if (subtree.level === undefined) subtree.level = 0;
        // subtree.children?.forEach(child => child.level = subtree.level + 1);
        if (subtree.children && subtree.children.forEach) {
          subtree.children.forEach(child => {
            if (child) {
              child.level = subtree.level + 1;
            }
          });
        }

        switch (subtree.level) {
          case 0:
            subtree.type = 'root';
            break;
          default:
            subtree.type = 'othernode';
        }
      });

      const width = container.scrollWidth;
      const height = (container.scrollHeight || 500) - 30;

      const tooltip = new G6.Tooltip({
        getContent(e) {
          // console.log("e=======>", e)
          if (e.item._cfg.model.tootip != undefined) {
            const outDiv = document.createElement("div");
            if (e.item._cfg.type == "node") {
              outDiv.style.width = "150px";
              outDiv.innerHTML = `
                  <div style="border: 1px solid #06a6db;position: relative;z-index:12">
                    <div style="background-color: #0786e0;padding: 10px 20px; font-size:12px;">${e.item._cfg.model.label}</div>
                    <div style="padding: 12px;position: relative;z-index:12">
                      <span>${e.item._cfg.model.tootip.leftData}</span>
                      <span>-></span>
                      <span>${e.item._cfg.model.tootip.rightData}</span>

                    </div>
                  </div>
                `;
              return outDiv;
            } else {
              outDiv.style.display = "none";
              return outDiv;
            }
            offset: 10
          }
        },
        shouldBegin(e) {
          console.log("shouldBegin=======>e====>", e.item._cfg.model.tootip)
          if (e.item._cfg.model.tootip != undefined) {
            if (e.target.cfg.name == 'gt') {
              return true;
            }
          } else {
            return false;
          }
        },
        itemTypes: ['node'],
        offsetX: 0,
        offsetY: 0,
      });

      const tooltip2 = new G6.Tooltip({
        getContent(e) {
          const outDiv = document.createElement("div");
          if (e.item._cfg.type == "node") {
            outDiv.style.width = "150px";
            outDiv.innerHTML = `
                  <div style="border: 1px solid #06a6db;position: relative;z-index:12">
                    <div style="background-color: #f5f5f5;padding: 10px 20px; font-size:12px;">血缘归因</div>
                  </div>
                `;
            return outDiv;
          } else {
            outDiv.style.display = "none";
            return outDiv;
          }
        },
        shouldBegin(e) {
          // console.log("shouldBegin=======>e====>", e)
          if (e.target.cfg.name == 'sd') {
            return true;
          }
        },
        itemTypes: ['node']
      });

      const graph = new G6.TreeGraph({
        container: 'container',
        width,
        height,
        fitView: true,
        layout: {
          type: 'compactBox',

          preventOverlap: true, // 防止节点重叠
          nodeSize: 50,
          // linkDistance: 150,// 指定边距离为150
          rankdir: "LR", // 展示方向
          nodesep: 40, // 节点间距
          ranksep: 1000, // 层间距

          direction: 'LR',
          getHGap: function getVGap() {
            return 5;
          },
        },
        defaultEdge: {
          type: 'round-poly',
          sourceAnchor: 0,
          targetAnchor: 1,
          style: {
            radius: 8,
            stroke: lineColor
          }
        },
        defaultNode: {
          size: [200, 40],
          anchorPoints: [
            [0.9, 0.5],
            [0, 0.5]
          ]
        },
        nodeStateStyles: {
          hover: {
            fill: '#fff',
            shadowBlur: 30,
            shadowColor: '#dc3434',
          },
          operatorhover: {
            'operator-box': {
              opacity: 1
            }
          }
        },
        plugins: [tooltip, tooltip2], //浮框样式
        modes: {
          default: ['zoom-canvas', 'drag-canvas', 'collapse-expand']
        }
      });

      graph.on('node:mouseenter', e => {
        // if (e.target.get('name')?.includes('operator')) {
        // if (e.target && e.target.get && e.target.get('name') && e.target.get('name').includes('operator')) {
        //
        //   graph.setItemState(e.item, 'operatorhover', true);
        // } else {
        graph.setItemState(e.item, 'hover', true);
        // }
      })
      graph.on('node:mouseleave', e => {
        graph.setItemState(e.item, 'operatorhover', false);
        graph.setItemState(e.item, 'hover', false);
      });

      //添加supportCSSTransform属性时树形组件不受父级transform影响

      // graph.get('canvas').set('supportCSSTransform', true)

      graph.data(data);
      graph.render();

    },
  },
};
</script>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值