Skip to content

@graphrs/g6

AntV G6 5.x 的高层集成层。提供布局引擎、社区检测和中心性分析,直接兼容 G6 数据格式。

bash
npm install @graphrs/g6

布局

createGraphrsLayout(options?)

创建 G6 兼容的布局对象,带有 execute 方法。

typescript
import { createGraphrsLayout } from '@graphrs/g6';

const layout = createGraphrsLayout({
  algorithm: 'fruchterman-reingold',
  iterations: 500,
  center: [400, 300],
  width: 800,
  height: 600,
});
// 用法: new G6.Graph({ layout, ... })

返回: { type: string; execute: (data: G6GraphData) => Promise<NodePositionMap> }

registerGraphrsLayouts(register)

将所有 graphrs 布局算法注册为 G6 命名自定义布局。

typescript
import { registerGraphrsLayouts } from '@graphrs/g6';
import { register } from '@antv/g6';

registerGraphrsLayouts(register);
// 现在可用: 'graphrs-fruchterman-reingold', 'graphrs-kamada-kawai' 等

executeLayout(data, options?)

在 G6 数据上运行布局算法,返回按节点 id 键入的位置。

typescript
import { executeLayout } from '@graphrs/g6';

const positions = await executeLayout(g6Data, {
  algorithm: 'kamada-kawai',
  center: [400, 300],
  width: 800,
  height: 600,
});
// positions: { 'node-a': { x: 120, y: 80 }, 'node-b': { x: 350, y: 220 }, ... }

返回: Promise<NodePositionMap>

GraphrsLayoutOptions

typescript
interface GraphrsLayoutOptions {
  algorithm?: LayoutAlgorithm;
  iterations?: number;
  center?: [number, number];
  width?: number;
  height?: number;
}

type LayoutAlgorithm =
  | 'fruchterman-reingold'
  | 'kamada-kawai'
  | 'circle'
  | 'grid'
  | 'star'
  | 'sugiyama'
  | 'random';

分析

detectCommunities(data, algorithm?)

在 G6 图数据上运行社区检测。

typescript
import { detectCommunities } from '@graphrs/g6';

const result = await detectCommunities(data, 'louvain');
// result.communities: Map<string, number>  — 节点 id → 社区索引
// result.modularity: number
// result.clusterCount: number
算法说明
'louvain'模块度优化(默认)
'leiden'改进版 Louvain,保证连通性
'infomap'信息论方法(随机游走压缩)
'label-propagation'快速,非确定性
'walktrap'短随机游走
'fast-greedy'贪心模块度合并

返回: Promise<CommunityMapping>

computeCentrality(data, algorithm?)

在 G6 图数据上计算中心性分数。

typescript
import { computeCentrality } from '@graphrs/g6';

const result = await computeCentrality(data, 'pagerank');
// result.scores: Map<string, number>  — 节点 id → 分数
算法说明
'pagerank'基于链接的重要性(默认)
'betweenness'桥接节点
'closeness'到所有其他节点的平均距离
'eigenvector'连接到重要节点

返回: Promise<CentralityMapping>

适配器

g6ToGraph(data)

将 G6 图数据转换为带数字索引的 graphrs Graph

typescript
import { g6ToGraph } from '@graphrs/g6';

const { graph, idToIndex, indexToId } = g6ToGraph(data);
  • graph — 带数字节点 ID (0, 1, 2, ...) 的 Graph 实例
  • idToIndexMap<string, number>(G6 字符串 id → 数字索引)
  • indexToIdMap<number, string>(数字索引 → G6 字符串 id)

引用未知节点的边会被静默跳过。

graphToG6(graph, indexToId, layout?)

将 graphrs Graph 转换回 G6 格式。

typescript
import { graphToG6 } from '@graphrs/g6';

const g6Data = graphToG6(graph, indexToId, layoutResult);

提供 layout 时,位置会作为 style.xstyle.y 应用到每个节点。

返回: G6GraphData

layoutResultToPositions(layout, nodeIds, center?, width?, height?)

将原始布局坐标归一化为定位映射。

typescript
import { layoutResultToPositions } from '@graphrs/g6';

const positions = layoutResultToPositions(
  layoutResult,
  ['a', 'b', 'c'],
  [400, 300],  // 中心
  800,         // 宽度
  600,         // 高度
);

返回: NodePositionMap{ [nodeId: string]: { x: number; y: number } }

类型

typescript
interface G6NodeData {
  id: string;
  style?: { x?: number; y?: number; [key: string]: unknown };
  data?: Record<string, unknown>;
}

interface G6EdgeData {
  id?: string;
  source: string;
  target: string;
  data?: Record<string, unknown>;
}

interface G6GraphData {
  nodes: G6NodeData[];
  edges: G6EdgeData[];
}

interface CommunityMapping {
  communities: Map<string, number>;
  modularity: number;
  clusterCount: number;
}

interface CentralityMapping {
  scores: Map<string, number>;
}

interface NodePositionMap {
  [nodeId: string]: { x: number; y: number };
}

MIT License (TS code) · GPL-2.0 (WASM binary)