Layers in Angular Diagram component

10 Dec 202424 minutes to read

Layer organizes related shapes within a diagram control as named categories. Assigning shapes to different layers enables selective viewing, removal, and locking of distinct shape categories.

In a diagram, Layers facilitate the modification of properties for all shapes assigned to a specific layer. Key properties that can be configured include:

  • Objects
  • Visible
  • Lock
  • AddInfo

Objects

The layer’s objects property specifies which diagram elements belong to that layer. This property contains a collection where you can define the categories of nodes and connectors that the layer encompasses.

In the following example, the basic shapes are categorized in layer 1, and the flow shapes are categorized in layer 2.

import { Component, ViewEncapsulation, OnInit, ViewChild } from '@angular/core';
import { DiagramModule,DiagramComponent,LayerModel,BasicShapeModel,FlowShapeModel } from '@syncfusion/ej2-angular-diagrams';


@Component({
imports: [
             DiagramModule
    ],
providers: [ ],
standalone: true,
    selector: "app-container",
    template: `
    <ejs-diagram #diagram id="diagram" width="100%" height="580px" [layers]="layers">
    <e-nodes>
            <e-node id='node1' [width]=100 [height]=100 [offsetX]=100 [offsetY]=100 [shape]='shape1' >
                <e-node-annotations>
                    <e-node-annotation content="Basic Rectangle">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node2' [width]=100 [height]=100 [offsetX]=250 [offsetY]=100 [shape]='shape2'>
                <e-node-annotations>
                    <e-node-annotation content="Basic Ellipse">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node3' [width]=100 [height]=100 [offsetX]=400 [offsetY]=100 [shape]='shape3' >
                <e-node-annotations>
                    <e-node-annotation content="Basic RightTriangle">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node4' [width]=100 [height]=100 [offsetX]=550 [offsetY]=100 [shape]='shape4' >
                <e-node-annotations>
                    <e-node-annotation content="Basic Plus">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node5' [width]=100 [height]=100 [offsetX]=100 [offsetY]=300 [shape]='shape5' >
                <e-node-annotations>
                    <e-node-annotation content="Flow Terminator">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node6' [width]=100 [height]=100 [offsetX]=250 [offsetY]=300 [shape]='shape6' >
                <e-node-annotations>
                    <e-node-annotation content="Flow Process">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node7' [width]=100 [height]=100 [offsetX]=400 [offsetY]=300 [shape]='shape7' >
                <e-node-annotations>
                    <e-node-annotation content="Flow Decision">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node8' [width]=100 [height]=100 [offsetX]=550 [offsetY]=300 [shape]='shape8' >
                <e-node-annotations>
                    <e-node-annotation content="Flow Document">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node> 
    </e-nodes>
    </ejs-diagram>`,
    encapsulation: ViewEncapsulation.None
})
export class AppComponent {
    @ViewChild("diagram")
    public diagram?: DiagramComponent;
    public layers: LayerModel[] =[];

    ngOnInit(): void {
        this.layers = [
            {
                id: 'layer1',
                visible: true,
                //Layer 1 objects
                objects: ['node1','node2','node3','node4'],
                lock: false
            },
            {
                id: 'layer2',
                visible: true,
                //Layer 2 objects
                objects: ['node5','node6','node7','node8'],
                lock: false
            }
        ]
    }
    public shape1: BasicShapeModel = {
        type: 'Basic',
        shape: 'Rectangle',
    };
    public shape2: BasicShapeModel = {
        type: 'Basic',
        shape: 'Ellipse',
    };
    public shape3: BasicShapeModel = {
        type: 'Basic',
        shape: 'RightTriangle',
    };
    public shape4: BasicShapeModel = {
        type: 'Basic',
        shape: 'Plus',
    };
    public shape5: FlowShapeModel = {
        type: 'Flow',
        shape: 'Terminator',
    };
    public shape6: FlowShapeModel = {
        type: 'Flow',
        shape: 'Process',
    };
    public shape7: FlowShapeModel = {
        type: 'Flow',
        shape: 'Decision',
    };
    public shape8: FlowShapeModel = {
        type: 'Flow',
        shape: 'Document',
    };
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Visible

The layer’s visible property is used to control the visibility of the elements assigned to the layer. You can hide objects in one layer while showing objects in another layer.

In the following example, the visibility of layer one is set to false. By default, the visible property of a layer is set to true.

import { Component, ViewEncapsulation, OnInit, ViewChild } from '@angular/core';
import { DiagramModule,DiagramComponent,PointModel,LayerModel} from '@syncfusion/ej2-angular-diagrams';

@Component({
imports: [
             DiagramModule
    ],
providers: [ ],
standalone: true,
    selector: "app-container",
    template: `
    <ejs-diagram #diagram id="diagram" width="100%" height="580px" [layers]="layers">
    <e-nodes>
            <e-node id='node1' [width]=100 [height]=100 [offsetX]=100 [offsetY]=100 >
                <e-node-annotations>
                    <e-node-annotation content="Layer 1 object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node2' [width]=100 [height]=100 [offsetX]=300 [offsetY]=100>
                <e-node-annotations>
                    <e-node-annotation content="Layer 2 object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-connectors>
                <e-connector id='connector' type='Straight' [sourcePoint]='sourcePoint' [targetPoint]='targetPoint'>
                <e-connector-annotations>
                    <e-connector-annotation content="Layer 2 object">
                    </e-connector-annotation>
                </e-connector-annotations>
                </e-connector>
            </e-connectors>
    </e-nodes>
    </ejs-diagram>`,
    encapsulation: ViewEncapsulation.None
})
export class AppComponent {
    @ViewChild("diagram")
    public diagram?: DiagramComponent;
    public layers: LayerModel[] =[];
    public sourcePoint?: PointModel;
    public targetPoint?: PointModel;

    ngOnInit(): void {
        this.sourcePoint = { x: 100, y: 300 };
        this.targetPoint = { x: 200, y: 400 };
        this.layers = [
            {
                id: 'layer1',
                //Layer 1 visibility set as false.
                visible: false,
                objects: ['node1']
            },
            {
                id: 'layer2',
                //Layer 1 visibility set as true.
                visible: true,
                objects: ['node2', 'connector'],
            },
        ]
    }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Lock

The layer’s lock property is used to prevent or allow changes to the element’s dimensions and positions. Locking a layer prevents any interactions with the objects in that layer, such as selecting, dragging, rotating, and connecting.

In the following example the objects in layer one is locked. By default, the lock property of a layer is set to false.

import { Component, ViewEncapsulation, OnInit, ViewChild } from '@angular/core';
import { DiagramModule,DiagramComponent,PointModel,LayerModel } from '@syncfusion/ej2-angular-diagrams';

@Component({
imports: [
             DiagramModule
    ],
providers: [ ],
standalone: true,
    selector: "app-container",
    template: `
    <ejs-diagram #diagram id="diagram" width="100%" height="580px" [layers]="layers">
    <e-nodes>
            <e-node id='node1' [width]=100 [height]=100 [offsetX]=100 [offsetY]=100 >
                <e-node-annotations>
                    <e-node-annotation content="Layer 1 object locked">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node2' [width]=100 [height]=100 [offsetX]=300 [offsetY]=100>
                <e-node-annotations>
                    <e-node-annotation content="Layer 2 object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-connectors>
                <e-connector id='connector' type='Straight' [sourcePoint]='sourcePoint' [targetPoint]='targetPoint'>
                <e-connector-annotations>
                    <e-connector-annotation content="Layer 2 object">
                    </e-connector-annotation>
                </e-connector-annotations>
                </e-connector>
            </e-connectors>
    </e-nodes>
    </ejs-diagram>`,
    encapsulation: ViewEncapsulation.None
})
export class AppComponent {
    @ViewChild("diagram")
    public diagram?: DiagramComponent;
    public layers: LayerModel[] =[];
    public sourcePoint?: PointModel;
    public targetPoint?: PointModel;

    ngOnInit(): void {
        this.sourcePoint = { x: 100, y: 300 };
        this.targetPoint = { x: 200, y: 400 };
        this.layers = [
            {
                id: 'layer1',
                visible: true,
                objects: ['node1'],
                //Locks the layer1 and prevents any interactions to the objects in layer1
                lock: true,
            },
            {
                id: 'layer2',
                visible: true,
                objects: ['node2', 'connector'],
                lock: false
            },
        ]
    }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

AddInfo

The addInfo property of layers allow you to maintain additional information to the layers.

The following code illustrates how to add additional information to the layers.

import { Component, ViewEncapsulation, OnInit, ViewChild } from '@angular/core';
import { DiagramComponent, Diagram, NodeModel, ConnectorModel, PointModel, LayerModel } from '@syncfusion/ej2-angular-diagrams';

@Component({
    selector: "app-container",
    template: `<ejs-diagram #diagram id="diagram" width="100%" height="580px" [layers]="layers">
    <e-connectors>
            <e-connector id='connector' type='Straight' [sourcePoint]='sourcePoint' [targetPoint]='targetPoint'>
            </e-connector>
    </e-connectors>
    <e-nodes>
            <e-node id='node1' [offsetX]=150 [offsetY]=150></e-node>
            <e-node id='node2' [offsetX]=350 [offsetY]=350></e-node>
    </e-nodes>
    </ejs-diagram>`,
    encapsulation: ViewEncapsulation.None
})
export class AppComponent {
    @ViewChild("diagram")
    public diagram: DiagramComponent;
    public sourcePoint: PointModel;
    public targetPoint: PointModel;
    public layers: LayerModel[];
    public addInfo: Object = { Description: 'Layer1' };
    ngOnInit(): void {
        this.sourcePoint = { x: 100, y: 100 };
        this.targetPoint = { x: 200, y: 200 };
        this.layers = [{
            id: 'layer1',
            visible: true,
            objects: ['node1', 'connector'],
            lock: true,
            addInfo: this.addInfo

        },
        {
            id: 'layer2',
            visible: true,
            objects: ['node2'],
            lock: false
        }];
    }
}

Add layer at runtime

Layers can be added at runtime by using the addLayer public method.

The layer’s ID property defines the ID of the layer, which is used to find the layer at runtime and apply any customizations. You can also add new objects to the new layer using the addLayer method.

The following code illustrates how to add a layer.

import { Component, ViewEncapsulation, OnInit, ViewChild } from '@angular/core';
import { DiagramModule, DiagramComponent,LayerModel } from '@syncfusion/ej2-angular-diagrams';

@Component({
imports: [
             DiagramModule
    ],
providers: [ ],
standalone: true,
    selector: "app-container",
    template: `
    <button (click)="addLayer()">addLayer</button>
    <ejs-diagram #diagram id="diagram" width="100%" height="580px" [layers]="layers">
    <e-nodes>
            <e-node id='node1' [width]=100 [height]=100 [offsetX]=100 [offsetY]=100 >
                <e-node-annotations>
                    <e-node-annotation content="Layer 1 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node2' [width]=100 [height]=100 [offsetX]=100 [offsetY]=300>
                <e-node-annotations>
                    <e-node-annotation content="Layer 2 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
    </e-nodes>
    </ejs-diagram>`,
    encapsulation: ViewEncapsulation.None
})
export class AppComponent {
    @ViewChild("diagram")
    public diagram?: DiagramComponent;
    public layers: LayerModel[] =[];
    ngOnInit(): void {
        this.layers = [
            {
                id: 'layer1',
                objects: ['node1']
            },
            {
                id: 'layers2',
                objects:['node2']
            }
        ]
    }

    /**
     * Add the layers to the existing diagram layer collection
     * newLayer - representing the layer to be added to the diagram.
     * layerObject -  An optional array of objects associated with the layer.
     */
    addLayer() {
        (this.diagram as any).addLayer({
            id: 'newlayer',
            visible: true,
            lock: false,
        }, [{
            type: 'Straight',
            sourceID: 'node1',
            targetID: 'node2',
            
        }]);
    }
   
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Remove layer at runtime

Layers can be removed at runtime by using the removeLayer public method.

To remove a layer, pass the ID of the layer you want to remove as a parameter to the removeLayer method.

The following code illustrates how to remove a layer.

import { Component, ViewEncapsulation, OnInit, ViewChild } from '@angular/core';
import { DiagramModule, DiagramComponent,LayerModel } from '@syncfusion/ej2-angular-diagrams';

@Component({
imports: [
             DiagramModule
    ],
providers: [ ],
standalone: true,
    selector: "app-container",
    template: `
    <button (click)="removeLayer()">removeLayer</button>
    <ejs-diagram #diagram id="diagram" width="100%" height="580px" [layers]="layers">
    <e-nodes>
            <e-node id='node1' [width]=100 [height]=100 [offsetX]=100 [offsetY]=100 >
                <e-node-annotations>
                    <e-node-annotation content="Layer 1 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node2' [width]=100 [height]=100 [offsetX]=100 [offsetY]=300>
                <e-node-annotations>
                    <e-node-annotation content="Layer 2 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
    </e-nodes>
    </ejs-diagram>`,
    encapsulation: ViewEncapsulation.None
})
export class AppComponent {
    @ViewChild("diagram")
    public diagram?: DiagramComponent;
    public layers: LayerModel[] =[];

    ngOnInit(): void {
        this.layers = [
            {
                id: 'layer1',
                objects: ['node1']
            },
            {
                id: 'layer2',
                objects:['node2']
            },
        ]
    }

    /**
     * Remove the layer from the existing diagram layer collection
     * layerId - representing the id of the layer to be removed from the diagram.
     */
     removeLayer() {
        (this.diagram as any).removeLayer('layer1');
    }
    
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

moveObjects

You can move objects from one layer to another dynamically using the moveObjects public method of the diagram control. This can be useful for managing complex diagrams with multiple layers where you need to update the categorization of elements based on user interaction or other dynamic conditions.

The following code illustrates how to move objects from one layer to another layer from the diagram.

import { Component, ViewEncapsulation, OnInit, ViewChild } from '@angular/core';
import { DiagramModule,DiagramComponent, LayerModel } from '@syncfusion/ej2-angular-diagrams';

@Component({
imports: [
             DiagramModule
    ],
providers: [ ],
standalone: true,
    selector: "app-container",
    template: `
    <button (click)="moveObjects()">moveObjects</button>
    <ejs-diagram #diagram id="diagram" width="100%" height="580px" [layers]="layers">
    <e-nodes>
            <e-node id='node1' [width]=100 [height]=100 [offsetX]=100 [offsetY]=100 >
                <e-node-annotations>
                    <e-node-annotation content="Layer 1 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node2' [width]=100 [height]=100 [offsetX]=100 [offsetY]=300>
                <e-node-annotations>
                    <e-node-annotation content="Layer 1 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node3' [width]=100 [height]=100 [offsetX]=300 [offsetY]=300>
                <e-node-annotations>
                    <e-node-annotation content="Layer 2 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
    </e-nodes>
    </ejs-diagram>`,
    encapsulation: ViewEncapsulation.None
})
export class AppComponent {
    @ViewChild("diagram")
    public diagram?: DiagramComponent;
    public layers: LayerModel[] =[];

    ngOnInit(): void {
        this.layers = [
            {
                id: 'layer1',
                objects: ['node1','node2']
            },
            {
                id: 'layers2',
                objects:['node3']
            },
        ]
    }

    /**
     * Move objects from one layer to another layer
     * Parameter 1 - An array of object IDs represented as strings to be moved
     * parameter 2 -  The ID of the target layer to which the objects should be moved.
     */
     moveObjects() {
        (this.diagram as any).moveObjects(['node1'], 'layer2');
        console.log(this.layers);
    }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Z-Index

zIndex property of a layer defines its Z order within the diagram. This property allows you to control the layer’s position in the stacking order. You can adjust the layer’s z-index by moving it forward or backward relative to other layers in the diagram.

Bring Layer Forward

Layers can be moved forward at runtime by using the bringLayerForward public method.

// move the layer forward
this.diagram.bringLayerForward('layer1');

Send Layer Backward

Layers can be moved backward at runtime by using the sendLayerBackward public method.

// move the layer backward
this.diagram.sendLayerBackward('layer1');

The following code illustrates how to send the layer forward/backward to another layer.

import { Component, ViewEncapsulation, OnInit, ViewChild } from '@angular/core';
import { DiagramModule, DiagramComponent, LayerModel } from '@syncfusion/ej2-angular-diagrams';

@Component({
imports: [
             DiagramModule
    ],
providers: [ ],
standalone: true,
    selector: "app-container",
    template: `
    <button (click)="bringLayerForward()">bringLayerForward</button>
    <button (click)="sendLayerBackward()">sendLayerBackward</button>
    <ejs-diagram #diagram id="diagram" width="100%" height="580px" [layers]="layers">
    <e-nodes>
            <e-node id='node1' [width]=100 [height]=100 [offsetX]=100 [offsetY]=100 >
                <e-node-annotations>
                    <e-node-annotation content="Layer 1 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node2' [width]=100 [height]=100 [offsetX]=130 [offsetY]=130>
                <e-node-annotations>
                    <e-node-annotation content="Layer 2 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node3' [width]=100 [height]=100 [offsetX]=160 [offsetY]=160>
                <e-node-annotations>
                    <e-node-annotation content="Layer 3 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
    </e-nodes>
    </ejs-diagram>`,
    encapsulation: ViewEncapsulation.None
})
export class AppComponent {
    @ViewChild("diagram")
    public diagram?: DiagramComponent;
    public layers: LayerModel[] =[];
    ngOnInit(): void {
        this.layers = [
            {
                id: 'layer1',
                objects: ['node1']
            },
            {
                id: 'layers2',
                objects:['node2']
            },
            {
                id: 'layers3',
                objects:['node3']
            }
        ]
    }

    /**
     * Move the layer forward
     * Parameter 1 -A string representing the id of the layer to be moved forward.
     */
    bringLayerForward() {
        (this.diagram as any).bringLayerForward('layer1');
    }

    /**
     * Move the layer Backward
     * Parameter 1 - A string representing the id of the layer to be  moved backward.
     */
    sendLayerBackward() {
        (this.diagram as any).sendLayerBackward('layer1');
    }
    
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Layer and objects rendering order

The rendering of diagram elements with layer properties involves grouping them within a diagram_diagramLayer for basic shape nodes and diagram_nativeLayer_svg for SVG-native elements. Even if different types of nodes are added within the same layer, the rendering at the DOM level occurs in separate layers. Therefore, when executing layering commands like bringLayerForward and sendLayerBackward, the native SVG elements will always render above the basic shape elements.

The order of rendering is as follows: HTML shapes -> SVG shapes -> Path data shapes & Basic shapes.

Clone Layer

Layers can be cloned with its object by using the cloneLayer public method.

The following code illustrates how to clone the layer.

import { Component, ViewEncapsulation, OnInit, ViewChild } from '@angular/core';
import { DiagramModule , LayerModel, DiagramComponent } from '@syncfusion/ej2-angular-diagrams'

@Component({
imports: [
             DiagramModule
],
providers: [ ],
standalone: true,
    selector: "app-container",
    template: `
    <button (click)="clonelayer()">clonelayer</button>
    <ejs-diagram #diagram id="diagram" width="100%" height="580px" [layers]="layers">
    <e-nodes>
            <e-node id='node1' [width]=100 [height]=100 [offsetX]=100 [offsetY]=100 >
                <e-node-annotations>
                    <e-node-annotation content="Layer 1 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node2' [width]=100 [height]=100 [offsetX]=160 [offsetY]=360>
                <e-node-annotations>
                    <e-node-annotation content="Layer 2 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
            <e-node id='node3' [width]=100 [height]=100 [offsetX]=300 [offsetY]=100>
                <e-node-annotations>
                    <e-node-annotation content="Layer 1 Object">
                    </e-node-annotation>
                </e-node-annotations>
            </e-node>
    </e-nodes>
    </ejs-diagram>`,
    encapsulation: ViewEncapsulation.None
})
export class AppComponent {
    @ViewChild("diagram")
    public diagram?: DiagramComponent;
    public layers: LayerModel[] =[];
    ngOnInit(): void {
        this.layers = [
            {
                id: 'layer1',
                objects: ['node1', 'node3']
            },
            {
                id: 'layers2',
                objects:['node2']
            }
        ]
    
    }
    /**
     * To Clone the layer
     * Parameter 1 - A string representing the name of the  layer to be cloned.
     */
    clonelayer() {
        (this.diagram as any).cloneLayer('layer1');
    }
    
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Active layer

Active Layer refers to the layer with the highest z-index in a diagram compared to other layers. When adding objects at runtime, they are stored in this active layer. If no layers are explicitly defined in the diagram, a default layer is created and automatically set as the active layer. However, when multiple layers are defined, the layer with the highest z-index takes precedence as the active layer.

Public methods are available to get and set the active layer, which are explained below.

Get ActiveLayer

Active layer of the diagram can be retrieved by using the getActiveLayer public method.

The following code illustrates how to fetch active layer from the diagram.

// gets the active layer back
this.diagram.getActiveLayer();

Set ActiveLayer

You can set any layer to be the active layer of the diagram by using the setActiveLayer public method.

The following code illustrates how to set active layer for diagram.

// set the active layer
//@param layerName defines the name of the layer which is to be active layer
this.diagram.setActiveLayer('layer2');