- Create group
- Add group when initializing diagram
- Group nodes at runtime
- UnGroup nodes at runtime
- Add group node at runtime
- Add collection of group nodes at runtime
- Add/Remove children from group
- Group padding
- Group flip
- Group flip mode
- Nested group
- Add Group in palette
- Update group node at runtime
- Container
- Difference between a basic group and containers
- Interaction
- Events
- See Also
Contact Support
Group in EJ2 TypeScript Diagram control
31 Jan 202524 minutes to read
Create group
Group is used to cluster multiple nodes and connectors into a single element. It acts like a container for its children (nodes, groups, and connectors). Every change made to the group also affects the children. Child elements can be edited individually.
Add group when initializing diagram
A group can be added to the diagram model through nodes
collection. To define an object as group, add the child objects to the children
collection of the group. The following code illustrates how to create a group node.
- While creating group, its child node need to be declared before the group declaration.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
let nodes: NodeModel[] = [
{
id: 'rectangle1',
offsetX: 100,
offsetY: 100,
width: 100,
height: 100,
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
},
annotations: [
{
content: 'rectangle1',
},
],
},
{
id: 'rectangle2',
offsetX: 200,
offsetY: 200,
width: 100,
height: 100,
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
},
annotations: [
{
content: 'rectangle2',
},
],
},
{
id: 'group',
children: ['rectangle1', 'rectangle2'],
},
];
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
nodes: nodes,
getNodeDefaults: (node: NodeModel) => {
node.height = 100;
node.width = 100;
node.style.strokeColor = 'white';
return node;
},
});
diagram.appendTo('#element');
diagram.select([diagram.nameTable['group']]);
<!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>
Group nodes at runtime
Groups can be dynamically created during runtime in the diagram by invoking the diagram.group
method. To initiate this process, first, select the nodes that you intend to include within the group. Subsequently, by utilizing the diagram.group
method, the selected nodes will be encapsulated within a newly formed group node.
The following code illustrates how a group at runtime.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
let node = {
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
annotations: [{ content: 'Node1' }],
};
let node2 = {
id: 'node2',
width: 100,
height: 100,
offsetX: 200,
offsetY: 200,
annotations: [{ content: 'Node2' }],
};
let node3 = {
id: 'node3',
width: 100,
height: 100,
offsetX: 300,
offsetY: 300,
annotations: [{ content: 'Node3' }],
};
let diagram: Diagram = new Diagram({
width: '100%',
height: 900,
nodes: [node, node2, node3],
});
diagram.appendTo('#element');
diagram.selectAll();
(document.getElementById('group') as HTMLInputElement).onclick = function () {
diagram.group();
};
<!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="group selected nodes" id="group"/>
<div id='element'></div>
</div>
</body>
</html>
UnGroup nodes at runtime
Group node can be unGrouped dynamically, by using the diagram.unGroup
method. The following code example shows how to unGroup group node at runtime.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
let nodes: NodeModel[] = [
{
id: 'rectangle1',
offsetX: 100,
offsetY: 100,
width: 100,
height: 100,
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
},
annotations: [
{
content: 'rectangle1',
},
],
},
{
id: 'rectangle2',
offsetX: 200,
offsetY: 200,
width: 100,
height: 100,
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
},
annotations: [
{
content: 'rectangle2',
},
],
},
{
id: 'group',
children: ['rectangle1', 'rectangle2'],
},
];
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
nodes: nodes,
});
diagram.appendTo('#element');
diagram.select([diagram.nameTable['group']]);
// Ungroup the selected group into nodes
(document.getElementById('unGroup') as HTMLInputElement).onclick = () => {
diagram.unGroup();
};
<!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="unGroup" id="unGroup" />
<div id='element'></div>
</div>
</body>
</html>
Add group node at runtime
A group node can be added at runtime by using the diagram method add
.
The following code illustrates how a group node is added at runtime.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
let nodes: NodeModel[] = [
{
id: 'rectangle1',
offsetX: 100,
offsetY: 100,
width: 100,
height: 100,
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
},
annotations: [
{
content: 'rectangle1',
},
],
},
{
id: 'rectangle2',
offsetX: 200,
offsetY: 200,
width: 100,
height: 100,
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
},
annotations: [
{
content: 'rectangle2',
},
],
},
];
let group: NodeModel = {
id: 'group2',
children: ['rectangle1', 'rectangle2'],
style: { strokeColor: 'green' },
};
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
nodes: nodes,
});
diagram.appendTo('#element');
// Add the group into the diagram
diagram.add(group);
<!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>
Add collection of group nodes at runtime
- The collection of group nodes can be dynamically added using ‘addElements’ method.Each time an element is added to the diagram canvas, the ‘collectionChange’ event will be triggered.
The following code illustrates how to add a group nodes collection at runtime.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
var node:NodeModel = [
{ id: "rectangle1", offsetX: 100, offsetY: 100, width: 100, height: 100, annotations: [{ content: 'node1' }] },
{ id: "rectangle2", offsetX: 200, offsetY: 200, width: 100, height: 100, annotations: [{ content: 'node2' }] },
{ id: 'group', children: ['rectangle1', 'rectangle2'] },
{ id: "rectangle3", offsetX: 400, offsetY: 400, width: 100, height: 100, annotations: [{ content: 'node1' }] },
{ id: "rectangle4", offsetX: 500, offsetY: 500, width: 100, height: 100, annotations: [{ content: 'node2' }] },
{ id: 'group2', children: ['rectangle3', 'rectangle4'], padding: { left: 10, right: 10, top: 10, bottom: 10 } },
];
let diagram: Diagram = new Diagram({
width: '100%',
height: 900,
});
diagram.appendTo('#element');
//Add collection of group nodes
diagram.addElements(node);
<!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>
Add/Remove children from group
Add children To group at runtime
A child node can be added to the specified Group at runtime by utilizing the diagram method addChildToGroup
.
This functionality is achieved by passing the group and existing children as arguments to the method.
The following code illustrates how a child node and a group node can be passed as arguments to the method and executed at runtime.
diagram.addChildToGroup(groupNode, childNode);
Remove children from group at runtime
A specific child from a group node can be removed at runtime by utilizing the diagram method removeChildFromGroup
.
This functionality is achieved by passing the group and its children as arguments to the method.
The following code illustrates how a child node is removed from a group at runtime.
diagram.removeChildFromGroup (groupNode, childNode);
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
let node: NodeModel = {
id: 'node1',
width: 150,
height: 100,
offsetX: 100,
offsetY: 100,
annotations: [{ content: 'Node1' }],
};
var node2: NodeModel = {
id: 'node2',
width: 80,
height: 130,
offsetX: 200,
offsetY: 200,
annotations: [{ content: 'Node2' }],
};
var group: NodeModel = {
id: 'group1',
children: ['node1', 'node2'],
};
var node3: NodeModel = {
id: 'node3',
width: 100,
height: 100,
offsetX: 300,
offsetY: 300,
annotations: [{ content: 'Node3' }],
};
let diagram: Diagram = new Diagram({
width: '100%',
height: 900,
nodes: [node, node2, group, node3],
});
diagram.appendTo('#element');
//To Add child to specifc group at Runtime
(document.getElementById('addChildToGroup') as HTMLInputElement).onclick = () => {
diagram.addChildToGroup(group, 'node3');
};
//To remove the specific children from group at runtime
(document.getElementById('removeChildFromGroup') as HTMLInputElement).onclick = () => {
diagram.removeChildFromGroup(group, 'node3');
};
<!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="addChildToGroup" id="addChildToGroup" />
<input type="button" value="removeChildFromGroup" id="removeChildFromGroup" />
<div id='element'></div>
</div>
</body>
</html>
Group padding
The Padding
property of a group node defines the spacing between the group node’s edges and its children.
The following code illustrates how to add Padding to the node group.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
let node: NodeModel = {
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
annotations: [{ content: 'Node1' }],
};
var node2: NodeModel = {
id: 'node2',
width: 100,
height: 100,
offsetX: 200,
offsetY: 200,
annotations: [{ content: 'Node2' }],
};
var node3: NodeModel = {
id: 'node3',
width: 100,
height: 100,
offsetX: 300,
offsetY: 300,
annotations: [{ content: 'Node3' }],
};
var group: NodeModel = {
id: 'group',
children: ['node1', 'node2', 'node3'],
padding: { left: 20, right: 20, top: 20, bottom: 20 },
};
let diagram: Diagram = new Diagram({
width: '100%',
height: 900,
nodes: [node, node2, node3, group],
});
diagram.appendTo('#element');
diagram.select([diagram.nameTable['group']]);
<!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>
Group flip
The flip functionality for a group node works similarly to that of normal nodes. However, when flipping a group node, the flip of its child nodes is combined with the group’s flip. This combination ensures that the child nodes inherit the group’s flip while retaining their own individual flips.
Example
:
- If a child node’s flip is set to Vertical and the group node’s flip is set to Horizontal, the resulting flip for the child node will be a combination of Vertical and Horizontal (effectively a “both” flip).
- This ensures that the child nodes’ orientations adapt dynamically based on the group’s flip while maintaining their unique flip settings.
The following example shows how to apply flip for group node.
import { Diagram, NodeModel, FlipDirection } from '@syncfusion/ej2-diagrams';
//Initialize nodes
let nodes: NodeModel[] = [
{
id: 'node1',
width: 70,
height: 70,
offsetX: 100,
offsetY: 100,
//Sets the flip as Vertical
flip: FlipDirection.Vertical,
shape: {
type: 'Basic',
shape: 'RightTriangle',
},
style: {
fill: '#6BA5D7',
},
},
{
id: 'node2',
width: 70,
height: 70,
offsetX: 180,
offsetY: 180,
shape: {
type: 'Basic',
shape: 'RightTriangle',
},
style: {
fill: '#6BA5D7',
},
},
{
id: 'group',
children: ['node1', 'node2',],
padding: { left: 20, right: 20, top: 20, bottom: 20 },
//Sets the flip as Horizontal
flip: FlipDirection.Horizontal,
}
];
let diagram: Diagram = new Diagram({
width: 900,
height: 900,
nodes: nodes,
});
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>
Group flip mode
The flipMode
of a group node also behave similarly to those of normal nodes. However,When you apply a flip mode to a group node, it takes precedence over any flip mode set on its child nodes, overriding their individual settings.
For example, in the below code,
the flipMode for the child node Node1
is set to LabelText
.
The flipMode for the group node is set to Label
.
As a result, the effective flipMode for both the child node and the group node will be Label, as the group node’s flipMode overrides the child’s.
import { Diagram, NodeModel, FlipDirection } from '@syncfusion/ej2-diagrams';
//Initialize nodes
let nodes: NodeModel[] = [
{
id: 'node1',
width: 70,
height: 70,
offsetX: 100,
offsetY: 100,
//Sets the flip mode as LabelText
flipMode:'LabelText',
annotations:[{content:'Node1',offset:{x:0,y:0.8}}],
shape: {
type: 'Basic',
shape: 'RightTriangle',
},
style: {
fill: '#6BA5D7',
},
},
{
id: 'node2',
width: 70,
height: 70,
offsetX: 180,
offsetY: 180,
shape: {
type: 'Basic',
shape: 'RightTriangle',
},
style: {
fill: '#6BA5D7',
},
},
{
id: 'group',
children: ['node1', 'node2',],
padding: { left: 20, right: 20, top: 20, bottom: 20 },
annotations:[{content:'Group',offset:{x:0,y:0.8}}],
//Sets the flip as Horizontal
flip: FlipDirection.Horizontal,
//Sets the flip mode as Label
flipMode:'Label',
}
];
let diagram: Diagram = new Diagram({
width: 900,
height: 900,
nodes: nodes,
});
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>
Nested group
Nested groups are essentially groups within groups, where a group can contain other groups as its children, creating a hierarchy that helps manage complexity and relationships between different elements. The following code illustrates how to create nested group node.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
let node: NodeModel = {
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
annotations: [{ content: 'Node1' }],
};
var node2: NodeModel = {
id: 'node2',
width: 100,
height: 100,
offsetX: 200,
offsetY: 200,
annotations: [{ content: 'Node2' }],
};
var node3: NodeModel = {
id: 'node3',
width: 100,
height: 100,
offsetX: 300,
offsetY: 350,
annotations: [{ content: 'Node3' }],
};
var node4: NodeModel = {
id: 'node4',
width: 100,
height: 100,
offsetX: 500,
offsetY: 350,
annotations: [{ content: 'Node4' }],
};
var group1: NodeModel = {
id: 'group1',
children: ['node1', 'node2'],
padding: { left: 20, right: 20, top: 20, bottom: 20 },
style: { fill: 'green' },
};
var group2: NodeModel = {
id: 'group2',
children: ['node3', 'node4'],
padding: { left: 20, right: 20, top: 20, bottom: 20 },
style: { fill: 'skyblue' },
};
var group3: NodeModel = {
id: 'group3',
children: ['group1', 'group2'],
padding: { left: 20, right: 20, top: 20, bottom: 20 },
style: { fill: 'yellow' },
};
let diagram: Diagram = new Diagram({
width: '100%',
height: 900,
nodes: [node, node2, node3, node4, group1, group2, group3],
});
diagram.appendTo('#element');
diagram.select([diagram.nameTable['group']]);
<!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>
Add Group in palette
Group node can be added in symbol palette like the normal nodes. The following code illustrates how to render group node in palette.
import { Diagram, NodeModel, SymbolPalette } from '@syncfusion/ej2-diagrams';
let diagram: Diagram;
let nodes: NodeModel[] = [
{
// Position of the node
offsetX: 250,
offsetY: 250,
// Size of the node
width: 100,
height: 100,
visible: true,
style: { fill: '#6AA8D7', strokeWidth: 1 },
// Text(label) added to the node
},
];
//Initializes diagram control
diagram = new Diagram({
width: '70%',
height: '645px',
nodes: nodes,
created: created,
});
diagram.appendTo('#element');
function created(): void {
diagram.fitToPage({ mode: 'Width' });
}
function getSymbols() {
let shapes = [
{
id: 'n1',
height: 50,
width: 50,
offsetX: 50,
offsetY: 50,
style: { fill: 'green' },
},
{
id: 'n2',
height: 50,
width: 50,
offsetX: 100,
offsetY: 100,
style: { fill: 'yellow' },
},
{
id: 'group1',
children: ['n1', 'n2'],
padding: { left: 10, right: 10, top: 10, bottom: 10 },
},
{
id: 'n3',
height: 50,
width: 50,
offsetX: 200,
offsetY: 100,
style: { fill: 'pink' },
},
{
id: 'n4',
height: 50,
width: 50,
offsetX: 200,
offsetY: 170,
style: { fill: 'orange' },
},
{
id: 'group2',
children: ['n3', 'n4'],
padding: { left: 10, right: 10, top: 10, bottom: 10 },
},
];
return shapes;
}
let palette = new SymbolPalette({
palettes: [
{
id: 'palette1',
title: 'Basic Shapes',
symbols: getSymbols(),
expanded: true,
},
],
height: 500,
width: '30%',
});
palette.appendTo('#palette');
<!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="palette" style="float: left">
</div>
<div id="element" style="float: right"></div>
</div>
</body>
</html>
Update group node at runtime
Group can be updated dynamically similar to the normal nodes. The following code illustrates how to update group node at runtime.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
let nodes: NodeModel[] = [
{
id: 'rectangle1',
offsetX: 100,
offsetY: 100,
width: 100,
height: 100,
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
},
annotations: [
{
content: 'rectangle1',
},
],
},
{
id: 'rectangle2',
offsetX: 200,
offsetY: 200,
width: 100,
height: 100,
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7',
},
annotations: [
{
content: 'rectangle2',
},
],
},
{
id: 'group',
children: ['rectangle1', 'rectangle2'],
style: { strokeColor: 'green' },
},
];
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
nodes: nodes,
});
diagram.appendTo('#element');
// Update the group node at runtime
(document.getElementById('updateGroupPadding') as HTMLInputElement).onclick =
() => {
let group = diagram.nameTable['group'];
(group.padding = { left: 10, right: 10, top: 10, bottom: 10 }), group.style;
diagram.dataBind();
};
(document.getElementById('updateGroupStyle') as HTMLInputElement).onclick =
() => {
let group = diagram.nameTable['group'];
group.style = { fill: 'yellow' };
diagram.dataBind();
};
<!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="updateGroupPadding" id="updateGroupPadding" />
<input type="button" value="updateGroupStyle" id="updateGroupStyle" />
<div id='element'></div>
</div>
</body>
</html>
Container
Containers are used to automatically measure and arrange the size and position of the child elements in a predefined manner. There are two types of containers available.
Canvas
-
The canvas panel supports absolute positioning and provides the least layout functionality to its contained diagram elements.
-
Canvas allows you to position its contained elements by using the margin and alignment properties.
-
Rendering alone possible in canvas container.
-
It allows elements to be either vertically or horizontally aligned.
-
Child can be defined with the collection
canvas.children
property. -
Basic element can be defined with the collection of
basicElements
.
The following code illustrates how to add canvas panel.
import { Diagram, DiagramElement, Canvas } from '@syncfusion/ej2-diagrams';
let diagram: Diagram;
let canvas: Canvas;
let child1: DiagramElement;
let child2: DiagramElement;
canvas = new Canvas();
canvas.pivot = {
x: 0,
y: 0,
};
canvas.offsetX = 75;
canvas.offsetY = 75;
canvas.style.fill = '#6BA5D7';
child1 = new DiagramElement();
(child1.id = 'child1'), (child1.width = 100);
child1.height = 100;
child1.margin.left = child1.margin.top = 10;
child2 = new DiagramElement();
(child2.id = 'child2'), (child2.width = 100);
child2.height = 100;
child2.margin.left = 190;
child2.margin.top = 190;
child2.margin.right = 5;
child2.margin.bottom = 5;
canvas.children = [child1, child2];
diagram = new Diagram({
mode: 'SVG',
width: '100%',
height: '600px',
// Defines the basic elements
basicElements: [canvas],
});
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>
Stack
-
Stack panel is used to arrange its children in a single line or stack order, either vertically or horizontally.
-
It controls spacing by setting margin properties of child and padding properties of group. By default, a stack panel’s
orientation
is vertical.
The following code illustrates how to add a stack panel.
import {Diagram,NodeModel,StackPanel,TextElement,} from '@syncfusion/ej2-diagrams';
let nodes: NodeModel[] = [{
id: 'node5',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
style: {
strokeColor: '#6BA5D7',
fill: '#6BA5D7'
},
annotations: [{
content: 'Custom Template',
offset: {
y: 1
},
verticalAlignment: 'Top'
}]
}, ];
let getTextElement: Function = (text: string) => {
let textElement: TextElement = new TextElement();
textElement.width = 50;
textElement.height = 20;
textElement.content = text;
return textElement;
};
let addRows: Function = (column: StackPanel) => {
column.children.push(getTextElement('Row1'));
column.children.push(getTextElement('Row2'));
column.children.push(getTextElement('Row3'));
column.children.push(getTextElement('Row4'));
};
let diagram: Diagram = new Diagram({
width: '100%',
height: 900,
nodes: nodes,
getNodeDefaults: (node: NodeModel) => {
node.height = 100;
node.width = 100;
node.style.fill = '#6BA5D7';
node.style.strokeColor = 'white';
return node;
},
setNodeTemplate: (obj: NodeModel, diagram: Diagram): StackPanel => {
if (obj.id.indexOf('node5') !== -1) {
// It will be replaced with grid panel
let table: StackPanel = new StackPanel();
table.orientation = 'Horizontal';
table.padding.left
let column1: StackPanel = new StackPanel();
column1.children = [];
column1.children.push(getTextElement('Column1'));
addRows(column1);
let column2: StackPanel = new StackPanel();
column2.children = [];
column2.children.push(getTextElement('Column2'));
addRows(column2);
table.children = [column1, column2];
return table;
}
return null;
},
});
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>
Difference between a basic group and containers
Group | Container |
---|---|
It arranges the child elements based on the child elements position and size properties. | Each container has a predefined behavior to measure and arrange its child elements. Canvas and stack containers are supported in the diagram. |
The Min, and Max Size properties are not applicable for basic group. | It is applicable for container. |
The Children’s margin and alignment properties are not applicable for basic group. | It is applicable for container. |
Interaction
Group node interactions can be performed similarly to normal nodes. Fundamental diagram interactions like selecting, dragging, resizing, and rotating apply equally to group nodes. For more informatation refer to the nodes interactions
Selecting a Node Group
When a child element within a node group is clicked, the entire contained node group is selected instead of the individual child element. Subsequent clicks on the selected element change the selection from top to bottom within the hierarchy, moving from the parent node group to its children.
Events
The events triggered when interacting with group nodes are similar to those for individual nodes. For more information, refer to the nodes events