Search results

Group in JavaScript (ES5) Diagram control

08 May 2023 / 6 minutes to read

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.

Create group

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.

  • The padding property of a group node defines the spacing between the group node’s edges and its children.
  • While creating group, its child node need to be declared before the group declaration.
  • Add a node to the existing group child by using the diagram.group method.
  • The group’s diagram.unGroup method is used to define whether the group can be ungrouped or not.
  • A group can be added into a child of another group.
Source
Preview
index.js
index.html
Copied to clipboard
var nodes = [{
        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'],
        padding:{left:10,right:10,top:10,bottom:10}
    },

];

var diagram = new ej.diagrams.Diagram({
    width: '1500px',
    height: '600px',
    getNodeDefaults: function(node) {
        node.height = 100;
        node.width = 100;
        node.style.fill = '#6BA5D7';
        node.style.strokeColor = 'white';
        return node;
    },
    nodes: nodes,
}, '#element');
Copied to clipboard
<!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">
    
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-diagrams/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet">
    
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="element"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

The following code illustrates how a ungroup at runtime.

Source
Preview
index.js
index.html
Copied to clipboard
var nodes = [{
        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']
    },

];

var diagram = new ej.diagrams.Diagram({
    width: '1500px',
    height: '600px',
    nodes: nodes,
    getNodeDefaults: function(node) {
        node.height = 100;
        node.width = 100;
        node.style.fill = '#6BA5D7';
        node.style.strokeColor = 'white';
        return node;
    },
}, '#element');
diagram.selectAll();
diagram.unGroup();
Copied to clipboard
<!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">
    
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-diagrams/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet">
    
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="element"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Add group at runtime

A group node can be added at runtime by using the client-side method diagram.add.

The following code illustrates how a group node is added at runtime.

Source
Preview
index.js
index.html
Copied to clipboard
var nodes = [{
        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'
        }]
    },
];

var group = {
    id: 'group',
    children: ['rectangle1', 'rectangle2']
};

var diagram = new ej.diagrams.Diagram({
    width: '1500px',
    height: '600px',
    getNodeDefaults: function(node) {
        node.height = 100;
        node.width = 100;
        node.style.fill = '#6BA5D7';
        node.style.strokeColor = 'white';
        return node;
    },
    nodes: nodes,
}, '#element');
diagram.add(group);
Copied to clipboard
<!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">
    
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-diagrams/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet">
    
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="element"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</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.

Source
Preview
index.js
index.html
Copied to clipboard
var diagram;
 var canvas;
 var child1;
 var child2;
 canvas = new ej.diagrams.Canvas();
 canvas.pivot = {
     x: 0,
     y: 0
 };
 canvas.offsetX = 200;
 canvas.offsetY = 100;
 canvas.style.fill = '#6BA5D7';
 child1 = new ej.diagrams.DiagramElement();
 child1.width = 100;
 child1.height = 100;
 child1.margin.left = child1.margin.top = 10;
 child2 = new ej.diagrams.DiagramElement();
 child2.width = 100;
 child2.height = 100;
 child2.margin.left = 190;
 child2.margin.top = 190;
 canvas.children = [child1, child2];
 diagram = new ej.diagrams.Diagram({
     mode: 'SVG',
     width: '1000px',
     height: '600px',
     basicElements: [canvas]
 }, '#element');
Copied to clipboard
<!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">
    
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-diagrams/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet">
    
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="element"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</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.

Source
Preview
index.js
index.html
Copied to clipboard
var diagram;
var nodes = [{
    id: "node5",
    width: 100,
    height: 100,
    offsetX: 100,
    offsetY: 100,
    style: {
      strokeColor: "#6BA5D7",
      fill: "#6BA5D7"
    },
    annotations: [{
        content: "Custom Template",
        offset: {
          y: 1
        },
        verticalAlignment: "Top"
      }]
  }];
var count = 11;
var getTextElement = function(text) {
  var textElement = new ej.diagrams.TextElement();
  textElement.id = "text" + count;
  textElement.width = 50;
  textElement.height = 20;
  textElement.content = text;
  count++;
  return textElement;
};
var addRows = function(column) {
  column.children.push(getTextElement("Row1"));
  column.children.push(getTextElement("Row2"));
  column.children.push(getTextElement("Row3"));
  column.children.push(getTextElement("Row4"));
};

//Initializes diagram control
diagram = new ej.diagrams.Diagram({
  width: 900,
  height: 900,
  nodes: nodes,
  setNodeTemplate: function(obj, diagram) {
    if (obj.id.indexOf("node5") !== -1) {
      var table = new ej.diagrams.StackPanel();
      table.orientation = "Horizontal";
      var column1 = new ej.diagrams.StackPanel();
      column1.children = [];
      column1.children.push(getTextElement("Column1"));
      addRows(column1);
      var column2 = new ej.diagrams.StackPanel();
      column2.children = [];
      column2.children.push(getTextElement("Column2"));
      addRows(column2);
      table.children = [column1, column2];
      return table;
    }
    return null;
    },
}, '#element');
Copied to clipboard
<!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">
    
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-diagrams/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet">
    
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="element"></div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</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 Padding, 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

You can edit the group and its children at runtime. For more information about how to interact with a group, refer to Edit Groups.

See Also