Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Setting offsetX and offsetY in getLabelStyle does not take effect #6814

Open
2 of 10 tasks
zmbxy opened this issue Feb 28, 2025 · 1 comment
Open
2 of 10 tasks
Labels
waiting for maintainer Triage or intervention needed from a maintainer.

Comments

@zmbxy
Copy link

zmbxy commented Feb 28, 2025

Describe the bug / 问题描述

自定义边,在getLabelStyle中设置offsetX、offsetY不生效

import { BaseEdge, ExtensionCategory, Graph, register } from '@antv/g6';

class PolylineEdge extends BaseEdge {

  getKeyStyle(attributes) {
    return { ...super.getKeyStyle(attributes), lineWidth: 2, stroke: '#000' };
  }

  getLabelStyle(attributes) {
    const labelStyle = {
      ...super.getLabelStyle(attributes),
      offsetX: 30,
      offsetY: 30,
    }
    console.log(labelStyle);
    return labelStyle;
  }

  drawLabelShape(attributes, container) {
    const style = this.getLabelStyle(attributes);
    this.upsert('label', Label, style, container);
  }

  getKeyPath(attributes) {
    const [sourcePoint, targetPoint] = this.getEndpoints(attributes);

    return [
      ['M', sourcePoint[0], sourcePoint[1]],
      ['L', targetPoint[0] / 2 + (1 / 2) * sourcePoint[0], sourcePoint[1]],
      ['L', targetPoint[0] / 2 + (1 / 2) * sourcePoint[0], targetPoint[1]],
      ['L', targetPoint[0], targetPoint[1]],
    ];
  }
}

register(ExtensionCategory.EDGE, 'custom-polyline', PolylineEdge);

const graph = new Graph({
  container: 'container',
  data: {
    nodes: [
      { id: 'node-0', style: { x: 100, y: 100, ports: [{ key: 'right', placement: [1, 0.5] }] } },
      { id: 'node-1', style: { x: 250, y: 200, ports: [{ key: 'left', placement: [0, 0.5] }] } },
    ],
    edges: [{ source: 'node-0', target: 'node-1' }],
  },
  edge: {
    type: 'custom-polyline',
    style: {
      startArrow: true,
      endArrow: true,
      stroke: '#000',
      labelText: '12313',
    },
  },
  behaviors: ['drag-element'],
});

graph.render();
Image

预期:

Image

Reproduction link / 复现链接

No response

Steps to Reproduce the Bug or Issue / 重现步骤

No response

Version / 版本

🆕 5.x

OS / 操作系统

  • macOS
  • Windows
  • Linux
  • Others / 其他

Browser / 浏览器

  • Chrome
  • Edge
  • Firefox
  • Safari (Limited support / 有限支持)
  • IE (Nonsupport / 不支持)
  • Others / 其他
@zmbxy zmbxy added the waiting for maintainer Triage or intervention needed from a maintainer. label Feb 28, 2025
@github-actions github-actions bot changed the title [Bug]: 自定义遍,在getLabelStyle中设置offsetX、offsetY不生效 [Bug]: Setting offsetX and offsetY in getLabelStyle does not take effect Feb 28, 2025
Copy link

在您的代码中,您尝试在 getLabelStyle 方法中设置 offsetXoffsetY,但这些属性似乎没有生效。这个问题可能与 upsert 方法的实现有关,或者与 Label 的处理方式有关。

以下是一些可能的解决方案和检查步骤:

  1. 检查 upsert 方法:确保 upsert 方法正确地应用了 offsetXoffsetY。如果 upsert 方法没有考虑这些属性,您可能需要修改它以支持这些偏移量。

  2. 验证 Label 的实现:确保 Label 组件能够接受并正确处理 offsetXoffsetY 属性。

  3. 调试日志:您已经在 getLabelStyle 中使用了 console.log 来输出 labelStyle。请确保这些日志显示的值是正确的,并且在 upsert 方法中没有被覆盖或忽略。

  4. 检查 G6 文档:确认 offsetXoffsetY 是在当前版本的 G6 中支持的属性,并且它们的用法符合文档说明。

如果这些步骤没有解决问题,您可能需要查看 G6 的源代码或相关文档,以确保这些属性在自定义边中是可用的。

如果需要进一步的帮助,请随时告诉我。

🪧 Tips For further assistance, please describe your question in the comments and @petercat-assistant to start a conversation with me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for maintainer Triage or intervention needed from a maintainer.
Projects
None yet
Development

No branches or pull requests

1 participant