antvis/G6

polyline的controlPoints未按预期工作 #5156

huturen posted onGitHub

Describe the bug

polyline的controlPoints未按预期工作:

  1. 小正方形为起始点、弯折点、终点的坐标标记
  2. 红色的线不是预期路径,和预期路径有偏离
  3. 蓝色的线是我们预期想要弯折的路径

image

Your Example Website or App

https://codesandbox.io/s/sad-haze-hr6pm7?file=/index.js

Steps to Reproduce the Bug or Issue

代码如下:

import G6 from '@antv/g6';

G6.registerNode('node-with-ports', {
    draw(cfg, group) {
      if (cfg.id.startsWith('mark')) {
        const [width, height] = [cfg.width || 10, cfg.height || 10];
        return group.addShape('rect', {
            attrs: {
                x: -width / 2,
                y: -height / 2,
                width,
                height,
                fill: '#9ec9ff',
                stroke: '#5b8ff9',
                fillOpacity: 0.5,
            },
        });
      }
        const [width, height] = [240, 80];
        const keyShape = group.addShape('rect', {
            attrs: {
                x: -width / 2,
                y: -height / 2,
                width,
                height,
                fill: '#9ec9ff',
                stroke: '#5b8ff9',
                fillOpacity: 0.5,
            },
        });
        group.addShape('text', {
            attrs: {
                x: -width / 2 + 10,
                y: -height / 2 + 10,
                text: cfg.id,
                fontSize: 14,
                textAlign: 'left',
                textBaseline: 'middle',
                fill: '#0000D9',
            },
        });
        // // 添加端口
        cfg.ports?.forEach((port, index) => {
            const isBottom = port?.layoutOptions?.['elk.port.side'] === 'SOUTH';
            group.addShape('circle', {
                attrs: {
                    r: 3,
                    x: -width / 2 + (width / (cfg.ports.length + 1)) * (index + 1),
                    y: isBottom ? height / 2 : -height / 2,
                    fill: '#fff',
                    stroke: '#5b8ff9',
                },
                name: `port${index}`,
            });
        });

        return keyShape;
    },
});
const data = {
  "nodes": [
    {
      "id": "n1",
      "width": 240,
      "height": 80,
      "ports": [
        {
          "id": "n1_p1",
          "width": 7,
          "height": 7,
          "layoutOptions": {
            "elk.port.side": "SOUTH",
            "elk.port.index": 0
          },
          "x": 75.5,
          "y": 80
        },
        {
          "id": "n1_p2",
          "width": 7,
          "height": 7,
          "layoutOptions": {
            "elk.port.side": "SOUTH",
            "elk.port.index": 1
          },
          "x": 157.5,
          "y": 80
        }
      ],
      "layoutOptions": {
        "elk.portConstraints": "FIXED_SIDE"
      },
      "$H": 277,
      "x": 51,
      "y": 10,
      "anchorPoints": [
        [
          0.3333333333333333,
          1
        ],
        [
          0.6666666666666666,
          1
        ]
      ],
      "type": "node-with-ports",
      "style": {}
    },
    {
      "id": "n2",
      "width": 240,
      "height": 80,
      "ports": [
        {
          "id": "n2_p1",
          "width": 7,
          "height": 7,
          "layoutOptions": {
            "elk.port.side": "NORTH",
            "elk.port.index": 0
          },
          "x": 116.5,
          "y": -7
        }
      ],
      "layoutOptions": {
        "elk.portConstraints": "FIXED_SIDE"
      },
      "$H": 282,
      "x": 10,
      "y": 264,
      "anchorPoints": [
        [
          0.5,
          0
        ]
      ],
      "type": "node-with-ports",
      "style": {}
    },
    {
      "id": "n3",
      "width": 240,
      "height": 80,
      "ports": [
        {
          "id": "n3_p1",
          "width": 7,
          "height": 7,
          "layoutOptions": {
            "elk.port.side": "NORTH",
            "elk.port.index": 0
          },
          "x": 116.5,
          "y": -7
        }
      ],
      "layoutOptions": {
        "elk.portConstraints": "FIXED_SIDE"
      },
      "$H": 285,
      "x": 530,
      "y": 264,
      "anchorPoints": [
        [
          0.5,
          0
        ]
      ],
      "type": "node-with-ports",
      "style": {}
    },

    {
      id: 'mark1',
      "x": 212 - 120,
      "y": 97 - 40,
    },    
    {
      id: 'mark2',
      "x": 650 - 120,
      "y": 257 - 40,
    },   
    {
      id: 'mark3',
      "x": 212 - 120,
      "y": 177 - 40,
    },   
    {
      id: 'mark4',
      "x": 650 - 120,
      "y": 177 - 40,
    },            
  ],
  "edges": [
    {
      "id": "e0",
      "source": "n1",
      "target": "n2",
      "sourceAnchor": 0,
      "targetAnchor": 0,
      "controlPoints": [],
    },
    {
      "id": "e1",
      "source": "n1",
      "target": "n3",
      "sourceAnchor": 1,
      "targetAnchor": 0,
      "controlPoints": [
        { "x": 212, "y": 177 },
        { "x": 650, "y": 177 }
      ],
    }
  ]
};

const width = document.getElementById('container').scrollWidth;
const height = document.getElementById('container').scrollHeight || 500;
const graph = new G6.Graph({
  container: 'container',
  width,
  height,
  fitCenter: true,
  modes: {
    default: ['drag-node'],
  },
  defaultNode: {
      type: 'node-with-ports', // 使用自定义节点
  },  

  defaultEdge: {
      type: 'polyline',
      style: {
          stroke: '#ff0000',
          endArrow: true,
      },
  },

});

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

Expected behavior

预期:n1到n3,采用polyline的controlPoints控制点可以按直角的预期路径弯折 期望按如下弯折: image

Screenshots or Videos

No response

Platform

  • OS: macOS 13.5
  • Browser: Chrome 119.0.6045.105
  • Version: @antv/g6 - 4.8.23

Additional context

No response


hi @huturen, welcome!

posted by github-actions[bot] over 1 year ago

Hi @huturen, Please star this repo if you find it useful! Thanks :star:! 你好 @huturen。如果该仓库对你有用,可以 star 一下,感谢你的 :star:!

posted by github-actions[bot] over 1 year ago

你好, 折现能实现按坐标点拐弯的需求吗? 看样子你这个也是想用 G6 实现 X6 的 ELK 布局,X6倒是有 vertical 控制,就是性能不行。

posted by America-first-melon over 1 year ago

@America-first-melon 我们实现是,把节点/边信息喂给ELK,再把ELK返回的布局信息(包含弯折点信息)转换成G6数据进行渲染。

【点/边】 => ELK => G6

但是,过程中发现,开启anchorPoints后,ELK返回的弯折点信息传给G6后,部分位置会发生偏移。 检查ELK本身返回的布局信息数据是ok的,问题可能出在G6这里。

posted by huturen over 1 year ago

@America-first-melon 我们实现是,把节点/边信息喂给ELK,再把ELK返回的布局信息(包含弯折点信息)转换成G6数据进行渲染。

【点/边】 => ELK => G6

但是,过程中发现,开启anchorPoints后,ELK返回的弯折点信息传给G6后,部分位置会发生偏移。 检查ELK本身返回的布局信息数据是ok的,问题可能出在G6这里。

跟我这里一样,既然这样,我试试 V5 可以不可以。

posted by America-first-melon over 1 year ago

This issue has been closed because it has been outdate for a long time. Please open a new issue if you still need help.

这个 issue 已经被关闭,因为 它已经过期很久了。 如果你仍然需要帮助,请创建一个新的 issue。

posted by github-actions[bot] 10 months ago

Fund this Issue

$0.00
Funded

Pull requests