Layers in EJ2 TypeScript Diagram control
16 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 { ConnectorModel, Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let nodes: NodeModel[] = [
// Layer 1 Nodes
{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
shape: { type: 'Basic', shape: 'Rectangle' },
annotations: [{ content: 'Basic rectangle' }],
},
{
id: 'node2',
width: 100,
height: 100,
offsetX: 250,
offsetY: 100,
shape: { type: 'Basic', shape: 'Ellipse' },
annotations: [{ content: 'Basic Ellipse' }],
},
{
id: 'node3',
width: 100,
height: 100,
offsetX: 400,
offsetY: 100,
shape: { type: 'Basic', shape: 'RightTriangle' },
annotations: [{ content: 'Basic RightTriangle' }],
},
{
id: 'node4',
width: 100,
height: 100,
offsetX: 550,
offsetY: 100,
shape: { type: 'Basic', shape: 'Plus' },
annotations: [{ content: 'Basic Plus' }],
},
// Layer 2 Nodes
{
id: 'node5',
width: 100,
height: 100,
offsetX: 100,
offsetY: 300,
shape: { type: 'Flow', shape: 'Terminator' },
annotations: [{ content: 'Flow Terminator' }],
},
{
id: 'node6',
width: 100,
height: 100,
offsetX: 250,
offsetY: 300,
shape: { type: 'Flow', shape: 'Process' },
annotations: [{ content: 'Flow Process' }],
},
{
id: 'node7',
width: 100,
height: 100,
offsetX: 400,
offsetY: 300,
shape: { type: 'Flow', shape: 'Decision' },
annotations: [{ content: 'Flow Decision' }],
},
{
id: 'node8',
width: 100,
height: 100,
offsetX: 550,
offsetY: 300,
shape: { type: 'Flow', shape: 'Document' },
annotations: [{ content: 'Flow Document' }],
},
];
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
nodes: nodes,
layers: [
{
id: 'layer1',
//Layer 1 objects
objects: ['node1', 'node2', 'node3', 'node4'],
},
{
id: 'layer2',
//Layer 2 objects
objects: ['node5', 'node6', 'node7', 'node8'],
},
],
});
// render initialized Diagram
diagram.appendTo('#element');
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
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 { ConnectorModel, Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let nodes: NodeModel[] = [
{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
annotations: [{ content: 'Layer 1 Object' }],
},
{
id: 'node2',
width: 100,
height: 100,
offsetX: 300,
offsetY: 100,
annotations: [{ content: 'Layer 2 Object' }],
},
];
let connectors: ConnectorModel[] = [
{
id: 'connector1',
type: 'Straight',
sourcePoint: {
x: 100,
y: 300,
},
targetPoint: {
x: 200,
y: 400,
},
annotations: [{ content: 'Layer 2 Object' }],
},
];
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
nodes: nodes,
connectors: connectors,
layers: [
{
id: 'layer1',
//Layer 1 visibility set as false.
visible: false,
objects: ['node1'],
},
{
id: 'layer2',
visible: true,
objects: ['node2', 'connector1'],
},
],
});
// render initialized Diagram
diagram.appendTo('#element');
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
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 { ConnectorModel, Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let nodes: NodeModel[] = [
{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
annotations: [{ content: 'Layer 1 Object Locked' }],
},
{
id: 'node2',
width: 100,
height: 100,
offsetX: 300,
offsetY: 100,
annotations: [{ content: 'Layer 2 Object' }],
},
];
let connectors: ConnectorModel[] = [
{
id: 'connector1',
type: 'Straight',
sourcePoint: {
x: 100,
y: 300,
},
targetPoint: {
x: 200,
y: 400,
},
annotations: [{ content: 'Layer 2 Object' }],
},
];
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
nodes: nodes,
connectors: connectors,
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', 'connector1'],
lock: false,
},
],
});
// render initialized Diagram
diagram.appendTo('#element');
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
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.
let nodes:NodeModel[] = [{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
},
{
id: 'node2',
width: 100,
height: 100,
offsetX: 300,
offsetY: 100,
}
];
let connectors:ConnectorModel[] = [{
id: 'connector1',
type: 'Straight',
sourcePoint: {
x: 100,
y: 300
},
targetPoint: {
x: 200,
y: 400
},
}];
//Additional informations of layer
let addInfo = { Description: 'Layer1' };
// initialize Diagram component
let diagram = new Diagram({
width: '100%',
height: '600px',
connectors: connectors,
nodes: nodes,
layers: [{
id: 'layer1',
visible: true,
objects: ['node1', 'node2'],
addInfo: addInfo
},
{
id: 'layer2',
visible: true,
objects: ['connector1']
}
]
});
Add layer at runtime
Layers can be added at runtime 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 new layer with new connectors stored in an object array of the new layer:
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let nodes: NodeModel[] = [
// Layer 1 Nodes
{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
annotations: [{ content: 'Layer 1 Object' }],
},
// Layer 2 Nodes
{
id: 'node2',
width: 100,
height: 100,
offsetX: 100,
offsetY: 300,
annotations: [{ content: 'Layer 2 Object' }],
},
];
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
nodes: nodes,
layers: [
{
id: 'layer1',
//Layer 1 objects
objects: ['node1'],
},
{
id: 'layer2',
//Layer 2 objects
objects: ['node2'],
},
],
});
// render initialized Diagram
diagram.appendTo('#element');
(document.getElementById('addLayer') as HTMLInputElement).onclick = () => {
let newLayer = {
id: 'newlayer',
visible: true,
lock: false,
};
let layerObject = [
{
id: 'con1',
type: 'Straight',
sourceID: 'node1',
targetID: '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.
*/
diagram.addLayer(newLayer, layerObject);
};
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<input type="button" value="addLayer" id="addLayer" />
<div id='element'></div>
</div>
</body>
</html>
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 { ConnectorModel, Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let nodes: NodeModel[] = [
// Layer 1 Nodes
{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
annotations: [{ content: 'Layer 1 Object' }],
},
// Layer 2 Nodes
{
id: 'node2',
width: 100,
height: 100,
offsetX: 100,
offsetY: 300,
annotations: [{ content: 'Layer 2 Object' }],
},
];
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
nodes: nodes,
layers: [
{
id: 'layer1',
//Layer 1 objects
objects: ['node1'],
},
{
id: 'layer2',
//Layer 2 objects
objects: ['node2'],
},
],
});
// render initialized Diagram
diagram.appendTo('#element');
(document.getElementById('removeLayer') as HTMLInputElement).onclick = () => {
let layerId = 'layer1';
/**
* Remove the layer from the existing diagram layer collection
* layerId - representing the id of the layer to be removed from the diagram.
*/
diagram.removeLayer(layerId);
};
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<input type="button" value="removeLayer" id="removeLayer" />
<div id='element'></div>
</div>
</body>
</html>
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.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let nodes: NodeModel[] = [
// Layer 1 Nodes
{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
annotations: [{ content: 'Layer 1 Object' }],
},
{
id: 'node2',
width: 100,
height: 100,
offsetX: 100,
offsetY: 300,
annotations: [{ content: 'Layer 1 Object' }],
},
// Layer 2 Nodes
{
id: 'node3',
width: 100,
height: 100,
offsetX: 300,
offsetY: 300,
annotations: [{ content: 'Layer 2 Object' }],
},
];
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
nodes: nodes,
layers: [
{
id: 'layer1',
//Layer 1 objects
objects: ['node1', 'node2'],
},
{
id: 'layer2',
//Layer 2 objects
objects: ['node3'],
},
],
});
// render initialized Diagram
diagram.appendTo('#element');
(document.getElementById('moveObjects') as HTMLInputElement).onclick = () => {
/**
* 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.
*/
diagram.moveObjects(['node1'], 'layer2');
};
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<input type="button" value="moveObjects" id="moveObjects" />
<div id='element'></div>
</div>
</body>
</html>
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
diagram.bringLayerForward('layer1');
Send Layer Backward
Layers can be moved backward at runtime by using the sendLayerBackward
public method.
// move the layer backward
diagram.sendLayerBackward('layer1');
The following code illustrates how to send the layer forward/backward to another layer.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let nodes: NodeModel[] = [
// Layer 1 Nodes
{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
annotations: [{ content: 'Layer 1 Object' }],
},
{
id: 'node2',
width: 100,
height: 100,
offsetX: 130,
offsetY: 130,
annotations: [{ content: 'Layer 2 Object' }],
},
// Layer 2 Nodes
{
id: 'node3',
width: 100,
height: 100,
offsetX: 160,
offsetY: 160,
annotations: [{ content: 'Layer 3 Object' }],
},
];
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
nodes: nodes,
layers: [
{
id: 'layer1',
//Layer 1 objects
objects: ['node1'],
},
{
id: 'layer2',
//Layer 2 objects
objects: ['node2'],
},
{
id: 'layer3',
//Layer 2 objects
objects: ['node3'],
},
],
});
// render initialized Diagram
diagram.appendTo('#element');
(document.getElementById('bringLayerForward') as HTMLInputElement).onclick =
() => {
/**
* Move the layer forward
* Parameter 1 -A string representing the id of the layer to be moved forward.
*/
diagram.bringLayerForward('layer1');
};
(document.getElementById('sendLayerBackward') as HTMLInputElement).onclick =
() => {
/**
* Move the layer Backward
* Parameter 1 - A string representing the id of the layer to be moved backward.
*/
diagram.sendLayerBackward('layer1');
};
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<input
type="button"
value="bringLayerForward"
id="bringLayerForward"
/>
<input
type="button"
value="sendLayerBackward"
id="sendLayerBackward"
/>
<div id='element'></div>
</div>
</body>
</html>
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 { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let nodes: NodeModel[] = [
// Layer 1 Nodes
{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
annotations: [{ content: 'Layer 1 Object' }],
},
{
id: 'node2',
width: 100,
height: 100,
offsetX: 300,
offsetY: 130,
annotations: [{ content: 'Layer 1 Object' }],
},
// Layer 2 Nodes
{
id: 'node3',
width: 100,
height: 100,
offsetX: 160,
offsetY: 360,
annotations: [{ content: 'Layer 2 Object' }],
},
];
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
nodes: nodes,
layers: [
{
id: 'layer1',
//Layer 1 objects
objects: ['node1', 'node2'],
},
{
id: 'layer2',
//Layer 2 objects
objects: ['node3'],
},
],
});
// render initialized Diagram
diagram.appendTo('#element');
(document.getElementById('cloneLayer') as HTMLInputElement).onclick = () => {
/**
* To Clone the layer
* Parameter 1 - A string representing the name of the layer to be cloned.
*/
diagram.cloneLayer('layer1');
};
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<input type="button" value="cloneLayer" id="cloneLayer" />
<div id='element'></div>
</div>
</body>
</html>
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 fetch active layer from the diagram
// gets the active layer of diagram
let activeLayer = 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
diagram.setActiveLayer('layer2');