Skip to content

API Reference

Complete reference for the TypeScript interfaces and data structures used in the Force Graph Library.

Constructor

typescript
constructor(
  container: HTMLElement,
  initialData: GraphData = { nodes: [], links: [] },
  options: GraphOptions = {}
)

Parameters:

  • container: HTML element where the graph will be rendered
  • initialData: Initial graph data (optional)
  • options: Configuration options (optional)

Configuration Options

GraphOptions Interface

OptionTypeDefaultDescription
widthnumber800Graph canvas width
heightnumber400Graph canvas height
labelThresholdnumber1.5Minimum zoom level to show node labels
nodeSizenumber | function1Node radius or function returning radius
nodeColorstring | functionundefinedNode fill color
nodeBorderColorstring | function'#333'Node border color
nodeBorderWidthnumber | function0Node border width
nodeLabelstring | functionNode label or idNode label text
nodeLabelColorstring | function'#555'Node label color
linkWidthnumber | function1Link stroke width
linkCurvaturenumber | string | function0Link curvature amount
keepDragPositionbooleanfalseFix nodes after dragging
nodeClickHandlerfunctionundefinedNode click event handler
clusterfunctionundefinedClustering function
collidefunctionundefinedCollision detection function
OptionTypeDefaultDescription
linkDirectionalParticlesnumber | function0Number of particles moving along links
linkDirectionalParticleSpeednumber | function0Speed of directional particles
linkDirectionalParticleWidthnumber | function0Width of directional particles
linkDirectionalParticleColorstring | function'#aaa'Color of directional particles

Group Visualization Options

OptionTypeDefaultDescription
showGroupsbooleanfalseEnable group visualization
groupBystring | function'topic'Property or function to group nodes by
groupBorderColorstring | function'#666'Group border color
groupBorderWidthnumber2Group border width
groupBorderOpacitynumber0.3Group border opacity
groupLabelColorstring | function'#333'Group label color
groupLabelSizenumber16Group label font size
groupLabelThresholdnumber0.8Zoom level threshold for group labels
groupPaddingnumber20Padding around group boundaries

Core Interfaces

GraphData Interface

The main data structure that contains all graph information:

typescript
import type { NodeData, LinkData } from '@logixode/force-graph-lib'

interface GraphData {
  nodes: NodeData[]
  links: LinkData[]
}

Example:

typescript
const graphData: GraphData = {
  nodes: [
    { id: '1', label: 'Node 1' },
    { id: '2', label: 'Node 2' },
  ],
  links: [{ source: '1', target: '2' }],
}

NodeData Interface

Represents individual nodes/vertices in the graph.

Required Properties

PropertyTypeDescription
idstring | numberUnique identifier for the node

Standard Properties

PropertyTypeDescriptionExample
labelstringDisplay text for the node"Alice Smith"
colorstringNode fill color (CSS color)"#e74c3c", "red"
valuenumberNumeric value for sizing/weighting25
typestringNode category/classification"user", "admin"
topicstringGrouping category"engineering", "sales"
platformstringPlatform identifier"web", "mobile"

Position Properties

PropertyTypeDescription
xnumberCurrent X coordinate
ynumberCurrent Y coordinate
fxnumberFixed X position (stops physics)
fynumberFixed Y position (stops physics)

Complete NodeData Example

typescript
const nodeExample: NodeData = {
  id: 'user_123',
  label: 'Alice Smith',
  color: '#3498db',
  value: 85,
  type: 'admin',
  topic: 'engineering',
  platform: 'web',

  // Position (usually set by physics engine)
  x: 150.5,
  y: 200.3,
}

LinkData Interface

Represents connections/edges between nodes.

Required Properties

PropertyTypeDescription
sourcestring | number | NodeDataSource node reference
targetstring | number | NodeDataTarget node reference

Standard Properties

PropertyTypeDescriptionExample
weightnumberConnection strength/importance3.5
colorstringLink color (CSS color)"#34495e"
curvaturenumberLink curve amount (0 = straight)0.2

Complete LinkData Example

typescript
const linkExample: LinkData = {
  source: 'user_123', // Can be ID string
  target: 'user_456', // Can be ID string
  // source: nodeObject,  // Or direct node reference

  weight: 4.2,
  color: '#e74c3c',
  curvature: 0.15,
}

Links can reference nodes in multiple ways:

typescript
// By ID (most common)
const linkById: LinkData = {
  source: 'node1',
  target: 'node2',
}

// By numeric ID
const linkByNumericId: LinkData = {
  source: 1,
  target: 2,
}

// By direct node reference
const node1: NodeData = { id: 'node1', label: 'First' }
const node2: NodeData = { id: 'node2', label: 'Second' }

const linkByReference: LinkData = {
  source: node1,
  target: node2,
}

Example Data Usage

Hierarchical Data

typescript
// Organization chart data
const orgData: GraphData = {
  nodes: [
    {
      id: 'ceo',
      label: 'CEO',
      type: 'executive',
      level: 1,
      color: '#c0392b',
    },
    {
      id: 'cto',
      label: 'CTO',
      type: 'executive',
      level: 2,
      color: '#8e44ad',
      reportsTo: 'ceo',
    },
    {
      id: 'dev1',
      label: 'Senior Dev',
      type: 'developer',
      level: 3,
      color: '#3498db',
      reportsTo: 'cto',
    },
  ],
  links: [
    { source: 'ceo', target: 'cto', type: 'reports_to' },
    { source: 'cto', target: 'dev1', type: 'manages' },
  ],
}

Network Analysis Data

typescript
// Social network data
const socialData: GraphData = {
  nodes: [
    {
      id: 'alice',
      label: 'Alice',
      type: 'person',
      topic: 'friends',

      // Social metrics
      connections: 45,
      influence: 8.2,
      activity: 'high',

      // Demographics
      age: 28,
      location: 'San Francisco',
      interests: ['tech', 'music', 'travel'],
    },
    {
      id: 'bob',
      label: 'Bob',
      type: 'person',
      topic: 'friends',

      connections: 23,
      influence: 5.7,
      activity: 'medium',

      age: 32,
      location: 'New York',
      interests: ['sports', 'cooking'],
    },
  ],
  links: [
    {
      source: 'alice',
      target: 'bob',

      // Relationship data
      type: 'friendship',
      strength: 0.8,
      duration: 5, // years
      interactions: 150, // monthly

      // Communication channels
      channels: ['text', 'social_media', 'in_person'],
      lastContact: '2024-01-14',
    },
  ],
}

Time-Series Data

typescript
// Data with temporal aspects
interface TimeseriesNodeData extends NodeData {
  timestamps: Array<{
    date: string
    value: number
    status: string
  }>
  createdAt: string
  updatedAt: string
}

const timeseriesData: GraphData = {
  nodes: [
    {
      id: 'server1',
      label: 'Web Server',
      type: 'infrastructure',

      timestamps: [
        { date: '2024-01-01', value: 85, status: 'healthy' },
        { date: '2024-01-02', value: 92, status: 'warning' },
        { date: '2024-01-03', value: 78, status: 'healthy' },
      ],

      createdAt: '2023-06-15',
      updatedAt: '2024-01-03',
    } as TimeseriesNodeData,
  ],
  links: [
    {
      source: 'server1',
      target: 'database1',

      // Connection metrics over time
      latencyHistory: [
        { date: '2024-01-01', latency: 12 },
        { date: '2024-01-02', latency: 15 },
        { date: '2024-01-03', latency: 11 },
      ],

      currentLatency: 11,
      averageLatency: 12.7,
    },
  ],
}