antvis/G6

点击节点中的文字可以获取到该节点,点击节点外层的矩形却获取不到节点,(目的:想要在点击矩形的时候也获取到节点) #4635

guoliuqing123 posted onGitHub

问题描述

当点击节点文字的时候,节点边框有颜色的变化,当点击节点边框(也就是矩形)的时候,没有节点颜色的变化 QQ截图20230613093046 QQ截图20230613093035 想请大佬指点下原因

重现链接

重现步骤

    //G6增加自定义节点信息;
    G6.registerNode('tree-node', { //tree-node 自定义节点名称
      draw: (cfg, group) => { //用于绘制节点 包含文本 锚点等
        const width = cfg.label.length * 10;
        const rect = group.addShape('rect', { //继承内置节点的rect有rect,path 等
          attrs: {
            ...cfg.style,
            x: -10, //矩形左上角的 x 坐标。
            y: -22,  //矩形左上角的 y 坐标。
            width,  //矩形的宽度。
            height: 20,  //矩形的高度。
            stroke: "#000", // 边框颜色
          },
          name: 'rect-shape',
        });

        //绘制文本节点
        const label = group.addShape('text', {
          attrs: {
            text: cfg.label.replace(/(.{11})/g, '$1\n'),
            fill: '#000', // 文字颜色
            fontSize: 12,
            x: 2,
            y: -5
          },
          name: 'label-shape',
        });

        const bbox = label.getBBox();
        rect.attr({
          height: bbox.height + 12,
          width: bbox.width + 16,
        });
        label.attr({
          x: 0,
          // y: bbox.height + 12 > 24 ? 10 : -3,
          y: -3 + [(bbox.height + 12)/18 - 1] * 13,
        })

        const hasChildren = cfg.children && cfg.children.length > 0;
        if (hasChildren) {
          group.addShape('marker', {
            attrs: {
              x: -3,
              y: -12,
              r: 2,
              symbol: G6.Marker.collapse,
              stroke: '#999',
              lineWidth: 2,
            },
            name: 'collapse-icon',
          });
        }

        // group.addShape('path', {
        //   attrs: {
        //     lineWidth: 1,
        //     fill: '#ccc',
        //     path: [['M', -10, -22], ['L', bboxWidth, 20]] //移动到0,0
        //   },
        //   name: 'path-shape',
        // })
        return rect;
      }
    }, 'rect');
    const graph = new TreeGraph({
      container: container,
      plugins: [menu, toolbar],
      width: window.innerWidth * 0.77,
      height: window.innerHeight,
      pixelRatio: 2,
      modes: {
        default: [
          'drag-canvas',
          'zoom-canvas',
          {
            type: 'scroll-canvas',
            zoomKey: "ctrl",
          },
          {
            type: 'collapse-expand',
            trigger: 'dblclick',
          },
          {
            type: 'click-select',
            trigger: 'ctrl',
            multiple: true, //可不配置,默认值为true
          }
        ]
      },
      // 节点类型及样式
      defaultNode: {
        // 设置节点的形状 circle:圆,rect:矩形,ellipse:椭圆;image:图片;marker:标记;path:路径;text:文本;dom(svg):DOM(图渲染方式 renderer 为 'svg' 时可用)。
        type: 'tree-node',
        size: [85, 20],
        // width: 'auto',
        anchorPoints: [[0, 0.5], [1, 0.5]],
        style: {
          stroke: 'black', //边框颜色
          cursor: 'move',
        },
        labelCfg: {
          style: {
            fontSize: 12
          }
        }
      },
      // 连线类型及样式
      defaultEdge: {
        type: 'cubic-horizontal',
        style: {
          stroke: '#ccc'
        }
      },
      //布局
      layout: {
        type: 'mindmap', //思维导图
        direction: 'LR',
        preventOverlap: true, // 防止节点重叠
        getHeight: function getHeight() {
          return 16;
        },
        getWidth: function getWidth() {
          return 16;
        },
        getVGap: function getVGap() {
          return 16;
        },
        getHGap: function getHGap() {
          return 90;
        }
      },
      fitView: true, //是否开启画布自适应。
      fitViewPadding: [20, 40, 50, 20],// fitView 为 true 时生效。图适应画布时,指定四周的留白。可以是数组,
      // trigger: 'click',
      minZoom: 0.5, // 最小缩放比例
      maxZoom: 1, // 最大缩放比例
    });
    graph.node(function (node) {
      // depth 类似节点标识
      if (node.depth === 0) {
        return {
          // size: [150, 30],
          style: {
            fill: 'darkseagreen',//背景色
            stroke: 'darkseagreen',//边框颜色
            radius: 10,
          },
        }
      }
      if (node.pointType === '软件') {
        return {
          style: {
            fill: '#eee',//背景颜色
          },
        }
      }
      return {
        labelCfg: {
          offset: 5
        }
      };
    });

预期行为

平台

  • 操作系统: [macOS, Windows, Linux, React Native ...]
  • 网页浏览器: [Google Chrome, Safari, Firefox]
  • G6 版本: [4.5.1 ... ] 无

屏幕截图或视频(可选)

补充说明(可选)

No response


可以获取到的呀,graph.on('node:click', e => { e.item 是节点元素 })

posted by Yanyan-Wang almost 2 years ago

https://github.com/antvis/G6/assets/105954867/ca73ca75-c1bb-4212-a694-1cf1b3dd04a7 我也用的graph.on('node:click',e=>{}),视频中我其实一直在点击这个地方,但就是偶尔有的时候没获取到节点

posted by guoliuqing123 almost 2 years ago

在点击的过程中 ,有console.log(e.item),点击的时候也没打印出来

posted by guoliuqing123 almost 2 years ago

我用你的代码,节点上任意地方都可以点到:https://codesandbox.io/s/long-fast-y7ftgr?file=/index.js

给个复现在线 demo 吧

posted by Yanyan-Wang almost 2 years ago

Fund this Issue

$0.00
Funded

Pull requests