Skip to content

@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.

bash
npm install @graphrs/g6

Layout

createGraphrsLayout(options?)

Creates a G6-compatible layout object with an execute method.

typescript
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.

typescript
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.

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 }, ... }

Returns: 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';

Analysis

detectCommunities(data, algorithm?)

Runs community detection on G6 graph data.

typescript
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
AlgorithmDescription
'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.

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

const result = await computeCentrality(data, 'pagerank');
// result.scores: Map<string, number>  — node id → score
AlgorithmDescription
'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.

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

const { graph, idToIndex, indexToId } = g6ToGraph(data);
  • graphGraph instance with numeric node IDs (0, 1, 2, ...)
  • idToIndexMap<string, number> (G6 string id → numeric index)
  • indexToIdMap<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.

typescript
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.

typescript
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

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)