Architecture
ngDiagram follows a layered architecture that separates concerns and enables extensibility. This design makes it easy to understand, customize, and extend for your specific needs.
Architecture Flow
Section titled “Architecture Flow”When a user interacts with the diagram, here’s what happens:
- User Interaction: User clicks, drags, or presses keys on diagram components
- Event Capture & Processing: User interactions are captured and processed to determine the appropriate action
- Command Emission: Based on the event, one or more commands are emitted to represent the intended operation
- Middleware Pipeline: Commands flow through middleware chain for validation, transformation, or side effects
- Model Update: Final state changes are applied to the model
- UI Reactivity: Angular signals automatically update the UI to reflect new state
This unidirectional flow ensures predictable behavior and makes debugging straightforward.
Architecture Layers
Section titled “Architecture Layers”Components Layer
Section titled “Components Layer”The user-facing components that render the diagram and handle user interactions:
- ng-diagram: Main component that renders the entire diagram, handles user interactions, and orchestrates all functionality
- ng-diagram-background: Background component that renders the diagram’s background grid
- ng-diagram-resize-adornment and ng-diagram-rotate-adornment: Utility components that can be embedded in custom node templates to provide resize and rotation functionality
- ng-diagram-palette-item: Drag and drop component that can be placed anywhere in your application to create palette functionality
- ng-diagram-palette-item-preview: Preview component that shows a visual representation of palette items during drag operations
- ng-diagram-base-edge: Foundation component for building custom edge templates with advanced styling and behavior
- ng-diagram-base-edge-label: Base component for creating custom edge labels with consistent styling and positioning
- ng-diagram-port: Port component that defines connection points on nodes for edge connections
- ng-diagram-base-node-template: Base component for creating custom node templates with default styling and features.
Service Layer
Section titled “Service Layer”Specialized services provide different levels of control and functionality:
- NgDiagramService: Core service providing middleware management, routing management, and transaction handling
- NgDiagramModelService: Handles model operations including node/edge updates, spatial queries, and data access
- NgDiagramViewportService: Manages viewport state with reactive signals for position, scale, and coordinate transformations
- NgDiagramSelectionService: Manages selection state with methods for selecting/deselecting nodes and edges
- NgDiagramNodeService: Provides node-specific operations
- NgDiagramGroupsService: Manages group node operations such as member management
- NgDiagramClipboardService: Manages copy/paste operations for diagram elements
All services are provided through provideNgDiagram(), allowing multiple diagrams on the same page to maintain their own isolated state and behavior. Learn more in the Services documentation.
Event Layer
Section titled “Event Layer”The event layer captures user interactions and translates them into commands that modify the diagram state.
Events are emitted by the NgDiagramComponent and can be handled via Angular outputs.
Available events:
- DiagramInitEvent: Fired when the diagram is fully initialized and all nodes, edges, and their internal parts are measured and positioned.
- EdgeDrawnEvent: Fired when a user manually creates an edge by dragging between nodes.
- SelectionChangedEvent: Fired when nodes or edges are selected or deselected.
- SelectionMovedEvent: Fired when selected nodes are moved (dragged) on the canvas.
- SelectionRemovedEvent: Fired when selected nodes or edges are removed from the diagram.
- SelectionRotatedEvent: Fired when selected nodes are rotated.
- GroupMembershipChangedEvent: Fired when nodes are grouped or ungrouped, changing their group membership.
- ViewportChangedEvent: Fired when the viewport changes due to panning or zooming.
- ClipboardPastedEvent: Fired when nodes and edges are added via paste operations (keyboard shortcut or programmatic paste).
- NodeResizedEvent: Fired when a node or group is resized manually or programmatically.
- PaletteItemDroppedEvent: Fired when a palette item is dropped onto the diagram to create a new node.
Each event provides a strongly-typed payload with relevant details. You can subscribe to these events using Angular outputs (e.g. (selectionChanged)="...") on the <ng-diagram> component.
See DiagramEventMap for more information on available events and their payloads.
Command System
Section titled “Command System”Commands provide precise, structured instructions for state changes in the diagram. After events are processed, handlers emit commands that represent the intended operation:
- Atomic Operations: Each command represents a single, well-defined action (select, moveNode, addEdge, etc.)
- Command Emission: Event handlers analyze user interactions and emit appropriate commands through the CommandHandler
- Transaction Support: Commands can be grouped into transactions for atomic execution, ensuring data consistency
- Extensible: The system supports 40+ built-in commands and can be extended with custom commands
The command system acts as the bridge between user interactions (events) and state changes (model updates), ensuring all modifications go through a controlled, predictable pipeline.
Middleware System
Section titled “Middleware System”Middleware intercepts commands before they reach the model, enabling behavior extension without modifying core code:
- Plugin Architecture: Add, remove, or modify behaviors without changing core code, making the system highly extensible
- Processing Pipeline: Middleware processes commands and state updates in sequence, allowing for validation, transformation, and side effects
- Configurable: Each middleware can be enabled/disabled and configured at runtime, providing flexibility
- Async Support: Middlewares can perform asynchronous operations like API calls or validations
Learn more in the Middlewares documentation.
Data Model
Section titled “Data Model”The foundation that stores diagram state and provides the data layer:
- Nodes: Visual elements with position, data, and behavior that represent entities in your diagram. See Nodes documentation for details.
- Edges: Connections between nodes with routing and styling that represent relationships. See Edges documentation for details.
- Metadata: Viewport state, middleware configurations, and other settings that control diagram behavior
The default Signal Model uses Angular signals for reactive state management, providing excellent performance and real-time updates. For advanced use cases, custom model adapters can integrate with external data sources like databases or APIs. Learn more in the State Management documentation.