Nodes in EJ2 JavaScript Diagram control
7 Aug 202424 minutes to read
Nodes are graphical objects used to visually represent the geometrical information, process flow, internal business procedure, entity, or any other kind of data, and it represents the functions of a complete system regarding to how it interacts with external entities.
Create node
A node can be created and added to the diagram either programmatically or interactively. The id
property of a node is used to define its unique identifier and can later be used to find the node at runtime for customization. Nodes are stacked on the diagram area from bottom to top in the order they are added.
NOTE
Note: There should not be any white-spaces in the ID string while setting the ID.
Add node through nodes collection
To create a node, define the node
object and add that to nodes
collection of the diagram model
. The following code example illustrates how to add a node to the diagram.
var node = {
// Position of the node
offsetX: 250,
offsetY: 250,
// Size of the node
width: 100,
height: 100,
style: { fill: '#6BA5D7', strokeColor: 'white' },
// Text(label) added to the node
};
// initialize Diagram component
var diagram = new ej.diagrams.Diagram({
width: '100%', height: '600px', nodes: [node]
}, '#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">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-diagrams/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-navigations/styles/fabric.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.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>
NOTE
Node id should not begin with numbers(should begin with a letter). Node Id should be unique for all the shapes and connectors.
Add/Remove node at runtime
Nodes can be added at runtime by using public method, add
and can be removed at runtime by using public method, remove
. On adding/removing node at runtime, the nodes collection is changed and the collectionChange
event will trigger.
The following code illustrates how to add a node and remove a selected node.
var node = {
// Position of the node
offsetX: 250,
offsetY: 250,
// Size of the node
width: 100,
height: 100,
style: { fill: '#6BA5D7', strokeColor: 'white' },
// Text(label) added to the node
};
// initialize Diagram component
var diagram = new ej.diagrams.Diagram({
width: '100%', height: '600px'
}, '#element');
document.getElementById('addNode').onclick = function() {
diagram.add(node);
};
document.getElementById('deleteNode').onclick = function() {
diagram.remove();
};
<!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="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-diagrams/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-navigations/styles/fabric.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<input type="button" value="add Node" id="addNode" />
<input type="button" value="delete Node" id="deleteNode" />
<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 collection of nodes at runtime
The collection of 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 nodes collection at runtime.
//initialize node collection
var nodesCollection = [
{ id: 'node16', offsetX: 35, offsetY: 260 },
{ id: 'node17', offsetX: 140, offsetY: 260 },
{ id: 'node18', offsetX: 240, offsetY: 260 }
];
// initialize Diagram component
var diagram = new ej.diagrams.Diagram({
width: '100%', height: '600px'
}, '#element');
//Add collection of nodes
diagram.addElements(nodesCollection);
<!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="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-diagrams/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-navigations/styles/fabric.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.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 node from palette
Nodes can be predefined and added to the palette, and can be dropped into the diagram when needed. For more information about adding nodes from symbol palette, refer to Symbol Palette
.
let diagram;
let nodes = [
{
// 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 ej.diagrams.Diagram({
width: '70%',
height: '645px',
nodes: nodes,
getNodeDefaults: function (obj) {
obj.style = { fill: 'skyblue' };
obj.width = 75;
obj.height = 50;
return obj;
},
created: created,
});
diagram.appendTo('#element');
function created() {
diagram.fitToPage({ mode: 'Width' });
}
let palette = new ej.diagrams.SymbolPalette({
symbolHeight: 70,
symbolWidth: 70,
palettes: [
{
id: 'palette1',
title: 'Basic Shapes',
symbols: [
{
id: 'symbol1',
height: 50,
width: 50,
style: { fill: 'skyblue' },
},
],
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">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-diagrams/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-navigations/styles/fabric.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="palette" style="float: left">
</div>
<div id="element" style="float: right"></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>
Create node through data source
Nodes can be generated automatically with the information provided through dataSource property. The default properties for these nodes are fetched from default settings (getNodeDefaults
). For more information about data source, refer to DataBinding
.
ej.diagrams.Diagram.Inject(ej.diagrams.DataBinding);
var data = [
{
id: 'data1',
parent: null,
},
];
// initialize Diagram component
var diagram = new ej.diagrams.Diagram(
{
width: '100%',
height: '600px',
dataSourceSettings: {
id: 'id',
parentId: 'parent',
dataSource: new ej.data.DataManager(data),
},
getNodeDefaults: function (node) {
node.height = 100;
node.width = 100;
node.offsetX = 300;
node.offsetY = 200;
node.style = { fill: 'yellow', strokeColor: 'yellow' };
return node;
},
},
'#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">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-diagrams/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-navigations/styles/fabric.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.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>
Draw nodes
Nodes can be interactively drawn by clicking and dragging the diagram surface.
To draw a shape, you have to activate the drawing tool by setting DrawOnce
or ContinuousDraw
to the tool
property and you need to set the node
object by using the drawingObject
property. The following code example illustrates how to draw a rectangle at runtime.
var diagram = new ej.diagrams.Diagram({
width: 700,
height: 700,
//Drawing object to draw basic rectangle
drawingObject: { shape: { type: 'Basic', shape: 'Rectangle' } },
//To maintain the drawing tool continuously
tool: ej.diagrams.DiagramTools.ContinuousDraw,
});
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">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-diagrams/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-navigations/styles/fabric.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.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>
Update node at runtime
You can modify any node properties at runtime, and the changes will be instantly reflected. For example, here you can change the size and color of the node.
var node = {
// 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
};
// initialize Diagram component
var diagram = new ej.diagrams.Diagram({
width: '100%', height: '600px', nodes: [node]
}, '#element');
document.getElementById('color').onclick = () =>{
diagram.nodes[0].style.fill = 'orange';
diagram.dataBind();
}
document.getElementById('size').onclick = () =>{
diagram.nodes[0].width = 200;
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">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-diagrams/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-navigations/styles/fabric.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<input type="button" value="change color" id="color"/>
<input type="button" value="change size" id="size"/>
<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>
NOTE
Once the property is updated, you should call the
dataBind
to reflect the changes instantly.
Clone node at runtime
Cloning a node creates a new node instance with identical properties and attributes. You can clone a node using the copy
and paste
public methods of the diagram model.
The following code example illustrates how to clone node at runtime
var node = {
// 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
};
// initialize Diagram component
var diagram = new ej.diagrams.Diagram({
width: '100%', height: '600px', nodes: [node]
}, '#element');
document.getElementById('clone').onclick = () =>{
let selectedNode = diagram.selectedItems.nodes.length > 0 ? diagram.selectedItems.nodes[0]: diagram.nodes[0];
diagram.select([selectedNode]);
diagram.copy();
diagram.paste();
}
<!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="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-diagrams/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-navigations/styles/fabric.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<input type="button" value="clone Node" id="clone"/>
<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 nodes from tree view
By customizing the dragEnter
functionality, you can allow elements from other components, such as the tree view, to be converted into nodes based on the data of the dragged element.
The following code example demonstrates how to convert a tree view element into a node while dragging it onto the diagram canvas
let diagram;
let nodes = [
{
// 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 ej.diagrams.Diagram({
width: '70%',
height: '645px',
nodes: nodes,
getNodeDefaults: function (obj) {
obj.style = { fill: 'skyblue' };
obj.width = 75;
obj.height = 50;
return obj;
},
dragEnter: dragEnter,
created: created,
});
diagram.appendTo('#element');
function created() {
diagram.fitToPage({ mode: 'Width' });
}
let productTeam1 = [
{
id: 't1',
name: 'ASP.NET MVC Team',
expanded: true,
child: [
{ id: 't2', pid: 't1', name: 'Smith' },
{ id: 't3', pid: 't1', name: 'Johnson' },
{ id: 't4', pid: 't1', name: 'Anderson' },
],
},
{
id: 't5',
name: 'Windows Team',
expanded: true,
child: [
{ id: 't6', pid: 't5', name: 'Clark' },
{ id: 't7', pid: 't5', name: 'Wright' },
{ id: 't8', pid: 't5', name: 'Lopez' },
],
},
];
let fields = {
dataSource: productTeam1,
id: 'id',
text: 'name',
child: 'child',
};
//Initialize ListView component
let treeObj = new ej.navigations.TreeView({
allowDragAndDrop: true,
//Set defined data to dataSource property
fields: fields,
});
//Render initialized ListView component
treeObj.appendTo('#treeView');
function dragEnter(args) {
let lable = '';
if (args.dragData) {
lable = args.dragData.text;
}
let node = {
width: 70,
height: 50,
offsetX: 100,
offsetY: 100,
data: { branch: 'Left' },
annotations: [{ content: lable }],
};
args.dragItem = 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">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-diagrams/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-navigations/styles/fabric.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="flat-list" style="float: left">
<h4>Tree List</h4>
<div id="treeView" tabindex="1"></div>
</div>
<div id="element" style="float: right"></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>