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

feat(graphs): add hierarchical graph #2692

Merged
merged 6 commits into from
Sep 11, 2024
Merged

feat(graphs): add hierarchical graph #2692

merged 6 commits into from
Sep 11, 2024

Conversation

yvonneyx
Copy link
Collaborator

  • 接入 ChartLoading 组件,用于在数据加载时显示加载动画
  • 接入 ErrorBoundary 组件,用于捕获并处理图表渲染过程中的错误
  • 向外暴露图表实例,通过 ref 可以直接获取图表实例
  • 支持节点展开/收起功能
  • 支持分层图表 HierarchicalGraph
  • 内置两种基础 React 节点:简单节点 PlainNode、组织架构卡片 OrganizationChartNode

HierarchicalGraph

const HierarchicalGraph = () => {
  const options: GraphOptions = {
    autoFit: 'view',
    data,
  };

  return <HierarchicalGraphComponent {...options} />;
};

image

OrganizationChart

const OrganizationChart = () => {
  const [data, setData] = useState(undefined);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    setLoading(true);
    fetch('https://assets.antv.antgroup.com/g6/organization-chart.json')
      .then((res) => res.json())
      .then((data) => {
        setData(data);
        setLoading(false);
      });
  }, []);

  const options: GraphOptions = {
    data,
    padding: 20,
    autoFit: 'view',
    node: {
      style: {
        component: (d) => {
          const { name, position, status } = d.data || {};
          const isActive = d.states?.includes('active');
          return <OrganizationChartNode name={name} position={position} status={status} isActive={isActive} />;
        },
        size: [240, 80],
      },
    },
    edge: {
      style: {
        radius: 16,
        lineWidth: 2,
        endArrow: true,
      },
    },
    layout: {
      type: 'antv-dagre',
      nodesep: 24,
      ranksep: -20,
    },
  };

  return <HierarchicalGraphComponent {...options} loading={loading} />;
}

image

图配置项

interface GraphOptions extends ContainerConfig, G6GraphOptions {
  /**
   * 是否支持节点收起/展开
   * @default true
   */
  collapsible?: boolean | CollapsibleOptions;
}

展开/收起能力配置项

interface CollapsibleOptions {
  /**
   * 点击指定元素,触发节点收起/展开
   * - 'icon': 点击内置图标
   * - 'node': 点击节点
   * - HTMLElement: 自定义元素
   * @default 'icon'
   */
  trigger?: 'icon' | 'node' | HTMLElement;
  /**
   * 收起/展开指定方向上的邻居节点
   * - 'in': 前驱节点
   * - 'out': 后继节点
   * - 'both': 前驱和后继节点
   * @default 'out'
   */
  direction?: 'in' | 'out' | 'both';
  /**
   * 渲染函数,用于自定义收起/展开图标
   * @param isCollapsed - 当前节点是否已收起
   * @returns 自定义图标
   */
  iconRender?: (isCollapsed: boolean) => React.ReactNode;
  /**
   * 指定图标的 CSS 类名
   */
  iconClassName?: string;
  /**
   * 指定图标的内联样式
   */
  iconStyle?: React.CSSProperties;
}

packages/graphs/package.json Outdated Show resolved Hide resolved
packages/graphs/package.json Outdated Show resolved Hide resolved
@lxfu1 lxfu1 merged commit 6528d95 into v2 Sep 11, 2024
8 checks passed
@lxfu1 lxfu1 deleted the graphs/hierarchy branch September 11, 2024 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants