Skip to content

Layout Integration

To perform layouts on in ngDiagram, integrate with external layout libraries to automatically position and arrange diagram elements.

The implementation below shows:

  • How to use built-in layout using NgDiagramService
  • How to use external layout libraries like ELK.js
  • How to use the selectionMoved event to detect when nodes are moved by the user
  • How to manage edge routing modes (manual vs auto):
    • Setting edges to manual mode to preserve custom layout points from ELK
    • Resetting edges to auto mode when connected nodes are moved manually
  • How to use batch update methods (updateNodes and updateEdges) for better performance
import { type Edge, type Node } from 'ng-diagram';
export const diagramModel: { nodes: Node[]; edges: Edge[] } = {
nodes: [
{ id: '1', position: { x: 0, y: 0 }, data: { label: 'Root' } },
{ id: '2', position: { x: 567, y: 122 }, data: { label: 'B' } },
{ id: '3', position: { x: 320, y: 301 }, data: { label: 'A' } },
{ id: '4', position: { x: 412, y: 254 }, data: { label: 'B 1' } },
{ id: '5', position: { x: 611, y: 487 }, data: { label: 'B 2' } },
{ id: '6', position: { x: 97, y: 178 }, data: { label: 'A 1' } },
{ id: '7', position: { x: 235, y: 85 }, data: { label: 'A 2' } },
{ id: '8', position: { x: 528, y: 320 }, data: { label: 'B 1' } },
{ id: '9', position: { x: 378, y: 465 }, data: { label: 'B 2' } },
{ id: '10', position: { x: 165, y: 387 }, data: { label: 'A 1.1' } },
],
edges: [
{
id: 'e1-2',
source: '1',
sourcePort: 'port-right',
targetPort: 'port-left',
target: '2',
data: {},
},
{
id: 'e1-3',
source: '1',
sourcePort: 'port-right',
targetPort: 'port-left',
target: '3',
data: {},
},
{
id: 'e2-4',
source: '2',
sourcePort: 'port-right',
targetPort: 'port-left',
target: '4',
data: {},
},
{
id: 'e2-5',
source: '2',
sourcePort: 'port-right',
targetPort: 'port-left',
target: '5',
data: {},
},
{
id: 'e3-6',
source: '3',
sourcePort: 'port-right',
targetPort: 'port-left',
target: '6',
data: {},
},
{
id: 'e3-7',
source: '3',
sourcePort: 'port-right',
targetPort: 'port-left',
target: '7',
data: {},
},
{
id: 'e5-8',
source: '5',
sourcePort: 'port-right',
targetPort: 'port-left',
target: '8',
data: {},
},
{
id: 'e5-9',
source: '5',
sourcePort: 'port-right',
targetPort: 'port-left',
target: '9',
data: {},
},
{
id: 'e6-10',
source: '6',
sourcePort: 'port-right',
targetPort: 'port-left',
target: '10',
data: {},
},
],
};