antvis/G6

[Bug]: Node icons not centered #7016

GabrieleMaurina posted onGitHub

Describe the bug / 问题描述

In React, node icons are not centered.

Here is a simple example:

import React, { useEffect, useRef } from "react";
import * as G6 from '@antv/g6';

const GraphComponent = () => {
    const containerRef = useRef(null);

    useEffect(() => {
        if (!containerRef.current) return;

        const graph = new G6.Graph({
            container: containerRef.current,
            width: 600,
            height: 400,
            node: {
                size: 30,
            },
            edge: {
                style: {
                    stroke: "#999",
                    lineWidth: 2,
                },
            },
            modes: {
                default: ["drag-canvas", "zoom-canvas", "drag-node"],
            },
            layout: {
                type: "circular",
                radius: 100,
            },
        });

        const data = {
            nodes: [
                { id: "node1", label: "Node 1", style: { iconText: '+' } },
                { id: "node2", label: "Node 2", style: { iconText: 'W' } },
            ],
            edges: [{ source: "node1", target: "node2" }],
        };

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

        return () => graph.destroy();
    }, []);

    return <div ref={containerRef} />;
};

export default GraphComponent;

The nodes look like this:

Image

The icons inside the nodes are not centered.

No response

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

Use the above component inside your React app.

Version / 版本

🆕 5.x

OS / 操作系统

  • macOS
  • Windows
  • Linux
  • Others / 其他

Browser / 浏览器

  • Chrome
  • Edge
  • Firefox
  • Safari (Limited support / 有限支持)
  • IE (Nonsupport / 不支持)
  • Others / 其他

Please refer to the v5 official website for the latest node examples.

posted by yvonneyx 13 days ago

The example contains these two things:

iconFontFamily: 'iconfont',
iconText: '\ue602',

but there is nothing about centering the icon and there is no explanation.

When I use an ascii character, it is not centered.

posted by GabrieleMaurina 12 days ago

You can use image node, see here or use iconSrc with a image url / base64.

posted by hustcc 12 days ago

The link above leads to a page in Chinese. Is there an equivalent in English?

That said, using iconSrc solves the problem for me. However, iconSrc is not mentioned in the English playground.

posted by GabrieleMaurina 11 days ago

The link above leads to a page in Chinese. Is there an equivalent in English?

That said, using iconSrc solves the problem for me. However, iconSrc is not mentioned in the English playground.

Now we are upgrading the document, after that, we will translate to English, welcome to contribute.

posted by hustcc 11 days ago

As hustcc mentioned, we’re working on the Chinese docs now and will sync the English version by late April. Feel free to check the examples first—we’ll update the docs ASAP. If you’d like to help with translation, we’d love that!

posted by yvonneyx 10 days ago

Thank you, issue solved for me!

posted by GabrieleMaurina 10 days ago

Fund this Issue

$0.00
Funded

Pull requests