Nodes in EJ2 TypeScript Diagram control
4 Dec 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.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let node: NodeModel = {
// 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
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
// Add node
nodes: [node]
});
// 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/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.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>
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.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let node: NodeModel = {
// Position of the node
offsetX: 250,
offsetY: 250,
// Size of the node
width: 100,
height: 100,
style: {
fill: '#6BA5D7',
strokeColor: 'white'
},
};
// initialize diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px'
});
// render initialized diagram
diagram.appendTo('#element');
(document.getElementById('addNode') as HTMLInputElement).onclick = function() {
diagram.add(node);
};
(document.getElementById('deleteNode') as HTMLInputElement).onclick = function() {
diagram.remove(diagram.nodes[0]);
};
<!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/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.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="add Node" id="addNode" />
<input type="button" value="delete Node" id="deleteNode" />
<div id='element'></div>
</div>
</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.
import { Diagram, NodeModel,ConnectorModel } from '@syncfusion/ej2-diagrams';
//initialize node collection
var nodesCollection:NodeModel = [
{ id: 'node16', offsetX: 35, offsetY: 260 },
{ id: 'node17', offsetX: 140, offsetY: 260 },
{ id: 'node18', offsetX: 240, offsetY: 260 }
];
// initialize diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px'
});
// render initialized diagram
diagram.appendTo('#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" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.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 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
.
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,
getNodeDefaults: function (obj: NodeModel) {
obj.style = { fill: 'skyblue' };
obj.width = 75;
obj.height = 50;
return obj;
},
created: created,
});
diagram.appendTo('#element');
function created(): void {
diagram.fitToPage({ mode: 'Width' });
}
let palette = new 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" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.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>
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
.
import { DataManager } from '@syncfusion/ej2-data';
import { DataBinding, Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
Diagram.Inject(DataBinding);
let data = [
{
id: 'data1',
parent: null,
},
];
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
dataSourceSettings: {
id: 'id',
parentId: 'parent',
dataSource: new DataManager(data),
},
getNodeDefaults: function (node: NodeModel) {
node.height = 100;
node.width = 100;
node.offsetX = 300;
node.offsetY = 200;
node.style = { fill: 'yellow', strokeColor: 'yellow' };
return node;
},
});
// 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/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.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>
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.
import { Diagram, NodeModel, DiagramTools } from '@syncfusion/ej2-diagrams';
let diagram: Diagram = new Diagram({
width: '100%',
height: 700,
//Drawing object to draw basic rectangle
drawingObject: { shape: { type: 'Basic', shape: 'Rectangle' } } as NodeModel,
//To maintain the drawing tool continuously
tool: 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" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.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>
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.
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let node: 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
};
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
// Add node
nodes: [node]
});
// render initialized Diagram
diagram.appendTo('#element');
(document.getElementById('color') as HTMLInputElement).onclick = () =>{
diagram.nodes[0].style.fill = 'orange';
diagram.dataBind();
}
(document.getElementById('size') as HTMLInputElement).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" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.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="change color" id="color"/>
<input type="button" value="change size" id="size"/>
<div id='element'></div>
</div>
</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
import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let node: 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
};
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
// Add node
nodes: [node]
});
// render initialized Diagram
diagram.appendTo('#element');
(document.getElementById('clone') as HTMLInputElement).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" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.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="clone Node" id="clone"/>
<div id='element'></div>
</div>
</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
import { Diagram, NodeModel,IDragEnterEventArgs } from '@syncfusion/ej2-diagrams';
import { TreeView } from '@syncfusion/ej2-navigations';
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,
getNodeDefaults: function (obj: NodeModel) {
obj.style = { fill: 'skyblue' };
obj.width = 75;
obj.height = 50;
return obj;
},
dragEnter: dragEnter,
created: created,
});
diagram.appendTo('#element');
function created(): void {
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: Object = {
dataSource: productTeam1,
id: 'id',
text: 'name',
child: 'child',
};
//Initialize ListView component
let treeObj: TreeView = new TreeView({
allowDragAndDrop: true,
//Set defined data to dataSource property
fields: fields,
});
//Render initialized ListView component
treeObj.appendTo('#treeView');
function dragEnter(args: IDragEnterEventArgs): void {
let lable = '';
if (args.dragData) {
lable = (args.dragData as any).text;
}
let node: NodeModel = {
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" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.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="flat-list" style="float: left">
<h4>Tree List</h4>
<div id="treeView" tabindex="1"></div>
</div>
<div id="element" style="float: right"></div>
</div>
</body>
</html>