Skip to content
D
Documentation

HybridLayoutAlgorithm

reference
1 min readUpdated

Import it from @grafloria/engine.

Also has every member of BaseLayoutAlgorithm, listed on its own entry.

HybridLayoutAlgorithm

Intelligent layout algorithm that:

  • Analyzes the diagram structure
  • Detects patterns (grid, tree, network, sparse)
  • Automatically selects the best algorithm
  • Delegates to the chosen algorithm

Pattern Detection:

  • Grid: Few or no edges, uniform structure
  • Tree/Hierarchy: Acyclic, directional flow
  • Network: Dense connections, cycles
  • Sparse: Few nodes, simple connections

Best for:

  • Dynamic diagrams where structure changes
  • Mixed diagram types
  • When user doesn't want to choose algorithm
ts
class HybridLayoutAlgorithm extends BaseLayoutAlgorithm implements ILayoutAlgorithm

Methods

  • constructor(options?: HybridOptions)
  • getName(): string — Get the name of the layout algorithm
  • getType(): 'hybrid' — Get the type of the layout algorithm
  • override configure(config: LayoutConfiguration): void — Configure the layout algorithm
  • override getConfiguration(): LayoutConfiguration — Get current configuration
  • override canApply(diagram: DiagramModel): { valid: boolean; reason?: string } — Validate if this algorithm can be applied to the given diagram

For example:

  • Hierarchical layout requires a DAG (no cycles)
  • Force-directed works better with connected nodes
  • calculatePlacement(options: PlacementOptions): PlacementResult — Calculate position for a single new node

This is called when a node is added to the diagram. The algorithm should return a position that:

  • Doesn't overlap with existing nodes
  • Follows the layout strategy
  • Fits within the viewport (or is close to existing content)
  • reLayout(diagram: DiagramModel, config?: LayoutConfiguration): Map<string, Point> — Re-layout all nodes in the diagram

This is called when the user explicitly requests a re-layout (e.g., clicks "Re-Arrange" button). The algorithm should calculate new positions for ALL nodes.

  • onActivate(): void — Called when the algorithm is activated Use this to initialize any state or caches
  • onDeactivate(): void — Called when the algorithm is deactivated Use this to clean up state or caches
  • getLastDetectedPattern(): DetectedPattern | undefined — Get the last detected pattern (useful for debugging/UI)

Was this page helpful?

HybridLayoutAlgorithm — Grafloria