@graphrs/g6
High-level integration layer for AntV G6 5.x. Provides layout engines, community detection, and centrality analysis that work directly with G6 data formats.
npm install @graphrs/g6Layout
createGraphrsLayout(options?)
Creates a G6-compatible layout object with an execute method.
import { createGraphrsLayout } from '@graphrs/g6';
const layout = createGraphrsLayout({
algorithm: 'fruchterman-reingold',
iterations: 500,
center: [400, 300],
width: 800,
height: 600,
});
// Use as: new G6.Graph({ layout, ... })Returns: { type: string; execute: (data: G6GraphData) => Promise<NodePositionMap> }
registerGraphrsLayouts(register)
Registers all graphrs layout algorithms as named G6 custom layouts.
import { registerGraphrsLayouts } from '@graphrs/g6';
import { register } from '@antv/g6';
registerGraphrsLayouts(register);
// Now available: 'graphrs-fruchterman-reingold', 'graphrs-kamada-kawai', etc.executeLayout(data, options?)
Runs a layout algorithm on G6 data, returning positions keyed by node id.
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 }, ... }Returns: Promise<NodePositionMap>
GraphrsLayoutOptions
interface GraphrsLayoutOptions {
algorithm?: LayoutAlgorithm;
iterations?: number;
center?: [number, number];
width?: number;
height?: number;
}
type LayoutAlgorithm =
| 'fruchterman-reingold'
| 'kamada-kawai'
| 'circle'
| 'grid'
| 'star'
| 'sugiyama'
| 'random';Analysis
detectCommunities(data, algorithm?)
Runs community detection on G6 graph data.
import { detectCommunities } from '@graphrs/g6';
const result = await detectCommunities(data, 'louvain');
// result.communities: Map<string, number> — node id → community index
// result.modularity: number
// result.clusterCount: number| Algorithm | Description |
|---|---|
'louvain' | Modularity optimization (default) |
'leiden' | Improved Louvain with guaranteed connectivity |
'infomap' | Information-theoretic (random walk compression) |
'label-propagation' | Fast, non-deterministic |
'walktrap' | Short random walks |
'fast-greedy' | Greedy modularity merge |
Returns: Promise<CommunityMapping>
computeCentrality(data, algorithm?)
Computes centrality scores on G6 graph data.
import { computeCentrality } from '@graphrs/g6';
const result = await computeCentrality(data, 'pagerank');
// result.scores: Map<string, number> — node id → score| Algorithm | Description |
|---|---|
'pagerank' | Link-based importance (default) |
'betweenness' | Bridge nodes |
'closeness' | Average distance to all others |
'eigenvector' | Connected to important nodes |
Returns: Promise<CentralityMapping>
Adapters
g6ToGraph(data)
Converts G6 graph data to a graphrs Graph with numeric indices.
import { g6ToGraph } from '@graphrs/g6';
const { graph, idToIndex, indexToId } = g6ToGraph(data);graph—Graphinstance with numeric node IDs (0, 1, 2, ...)idToIndex—Map<string, number>(G6 string id → numeric index)indexToId—Map<number, string>(numeric index → G6 string id)
Edges referencing unknown nodes are silently skipped.
graphToG6(graph, indexToId, layout?)
Converts a graphrs Graph back to G6 format.
import { graphToG6 } from '@graphrs/g6';
const g6Data = graphToG6(graph, indexToId, layoutResult);When layout is provided, positions are applied as style.x and style.y on each node.
Returns: G6GraphData
layoutResultToPositions(layout, nodeIds, center?, width?, height?)
Normalizes raw layout coordinates into a positioned map.
import { layoutResultToPositions } from '@graphrs/g6';
const positions = layoutResultToPositions(
layoutResult,
['a', 'b', 'c'],
[400, 300], // center
800, // width
600, // height
);Returns: NodePositionMap — { [nodeId: string]: { x: number; y: number } }
Types
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 };
}