ES5 getting started with EJ2 JavaScript Diagram control
30 Jan 202624 minutes to read
Essential® JS 2 (global script) is an ES5-formatted, pure JavaScript framework that can be used directly in modern web browsers.
Ready to streamline your Syncfusion® JavaScript development? Discover the full potential of Syncfusion® JavaScript controls with Syncfusion® AI Coding Assistant. Effortlessly integrate, configure, and enhance your projects with intelligent, context-aware code suggestions, streamlined setups, and real-time insights—all seamlessly integrated into your preferred AI-powered IDEs like VS Code, Cursor, Syncfusion® CodeStudio and more. Explore Syncfusion® AI Coding Assistant
Dependencies
The following list of dependencies are required to use the Diagram component in your application.
|-- @syncfusion/ej2-diagrams
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-data
|-- @syncfusion/ej2-navigations
|-- @syncfusion/ej2-inputs
|-- @syncfusion/ej2-popups
|-- @syncfusion/ej2-buttons
|-- @syncfusion/ej2-lists
|-- @syncfusion/ej2-splitbuttonsComponent Initialization
The Essential® JS 2 components can be initialized in two ways:
- Using local script and style references in an HTML page.
- Using CDN links for scripts and styles.
Using local script and style references in a HTML page
Step 1:Create an application folder named my-diagram-app for Essential® JS 2 JavaScript components.
Step 2: You can get the global scripts and styles from the Essential Studio® JavaScript (Essential® JS 2) build installed location.
Syntax:
Script:
**(installed location)**/Syncfusion/Essential Studio/{RELEASE_VERSION}/Web(Essential JS 2)/javascript/{PACKAGE_NAME}/dist/global/{PACKAGE_NAME}.min.jsStyles:
**(installed location)**/Syncfusion/Essential Studio/{RELEASE_VERSION}/Web(Essential JS 2)/javascript/{PACKAGE_NAME}/styles/tailwind3.css
Example:
Script:
C:/Program Files (x86)/Syncfusion/Essential Studio/32.1.19/Web(Essential JS 2)/javascript/ej2-diagrams/dist/global/ej2-diagrams.min.jsStyles:
C:/Program Files (x86)/Syncfusion/Essential Studio/32.1.19/Web(Essential JS 2)/javascript/ej2-diagrams/styles/tailwind3.css
Step 3: Create a my-diagram-app/resources folder and copy the global scripts and styles from the installation location to this resources folder.
Step 4: Create an HTML file named index.html in the my-diagram-app folder and add references to the Essentials® JS 2 script and style references.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Essential® JS 2</title>
<!-- Essential® JS 2 tailwind3 theme -->
<link href="resources/tailwind3.css" rel="stylesheet" type="text/css"/>
<!-- Essential JS 2 Diagram's dependent scripts -->
<script src="resources/ej2-base.min.js" type="text/javascript"></script>
<!-- Essential® JS 2 Diagram's global script -->
<script src="resources/ej2.min.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>Step 5: Now, add the Diagram element and initiate the Essential® JS 2 Diagram component in the index.html by using following code
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Essential® JS 2</title>
<!-- Essential® JS 2 tailwind3 theme -->
<link href="resources/tailwind3.css" rel="stylesheet" type="text/css"/>
<!-- Essential JS 2 Diagram's dependent scripts -->
<script src="resources/ej2-base.min.js" type="text/javascript"></script>
<!-- Essential® JS 2 Diagram's global script -->
<script src="resources/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<!-- Add the HTML <div> element -->
<div id="element">Diagram</div>
<script>
// initialize diagram component
var diagram = new ej.diagrams.Diagram({
width:'100%',height:'700px'
});
// Render initialized diagram.
diagram.appendTo('#element')
</script>
</body>
</html>Step 6: Now, run the index.html in web browser, it will render the Essential® JS 2 Diagram component.
Using CDN links for scripts and styles
Step 1: Create an app folder my-diagram-app for the Essential® JS 2 JavaScript components.
Step 2: Essential® JS 2 global scripts and styles are hosted on Syncfusion’s CDN. Reference the required packages using the following patterns.
Syntax:
Script:
https://cdn.syncfusion.com/ej2/{RELEASE_VERSION}/dist/global/{PACKAGE_NAME}.min.jsStyles:
https://cdn.syncfusion.com/ej2/{PACKAGE_NAME}/styles/tailwind3.css
Example:
Script:
https://cdn.syncfusion.com/ej2/32.1.19/dist/ej2.min.jsStyles:
https://cdn.syncfusion.com/ej2/ej2-diagrams/styles/Tailwind3.css
Step 3: Create an HTML page (index.html) in my-diagram-app and add the CDN link references. Then add a container element (for example, <div id="element">) and initialize the Essential® JS 2 Diagram component using the example code below.
ej.diagrams.Diagram.Inject(ej.diagrams.DataBinding, ej.diagrams.HierarchicalTree);
var data = [
{ Name: "Elizabeth", Role: "Director" },
{ Name: "Christina", ReportingPerson: "Elizabeth", Role: "Manager" },
{ Name: "Yoshi", ReportingPerson: "Christina", Role: "Lead" },
{ Name: "Philip", ReportingPerson: "Christina", Role: "Lead" },
{ Name: "Yang", ReportingPerson: "Elizabeth", Role: "Manager" },
{ Name: "Roland", ReportingPerson: "Yang", Role: "Lead" },
{ Name: "Yvonne", ReportingPerson: "Yang", Role: "Lead" }
];
var codes = {
Director: "rgb(0, 139,139)",
Manager: "rgb(30, 30,113)",
Lead: "rgb(0, 100,0)"
};
function getNodeTemplate(node) {
node.annotations[0].content = node.data.Name;
node.style.fill = codes[node.data.Role];
}
var items = new ej.data.DataManager(data, new ej.data.Query().take(7));
var diagram = new ej.diagrams.Diagram({
width: '100%', height: '350px',
mode: 'SVG', snapSettings: { constraints: ej.diagrams.SnapConstraints.None },
dataSourceSettings: {
id: 'Name', parentId: 'ReportingPerson', dataManager: items
},
layout: {
type: 'OrganizationalChart',
margin: { left: 10, top: 10 },
horizontalSpacing: 50,
verticalSpacing: 50,
orientation: 'TopToBottom',
getLayoutInfo: function (node, tree) {
if (!tree.hasSubTree) {
tree.orientation = 'Vertical';
tree.type = 'Alternate';
}
}
},
getNodeDefaults: function (obj, diagram) {
obj.height = 30;
obj.width = 70;
obj.shape = { type: 'Basic', shape: 'Rectangle' };
obj.annotations = [{
id: "label1",
style: {
fontSize: 11,
bold: true,
fontFamily: "Segoe UI",
color: "white"
}
}];
return obj;
},
getConnectorDefaults: function (connector, diagram) {
connector.targetDecorator.shape = 'Arrow';
connector.type = 'Orthogonal';
return connector;
},
setNodeTemplate: function (node) {
return getNodeTemplate(node);
}
});
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/32.1.19/ej2-base/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-navigations/styles/tailwind3.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/32.1.19/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>Step 4: Now, run the index.html in web browser, it will render the Essential® JS 2 Diagram component.
Module Injection
The Diagram component is divided into individual feature-wise modules. In order to use a particular feature, inject the required module. The following list describes the module names and their description.
-
BpmnDiagrams: Inject this provider to add built-in BPMN shapes to diagrams. -
ConnectorBridging: Inject this provider to add bridges to connectors. -
ConnectorEditing: Inject this provider to edit the segments for connector. -
ComplexHierarchicalTree: Inject this provider to complex hierarchical tree like structure. -
DataBinding: Inject this provider to populate nodes from given data source. -
DiagramContextMenu: Inject this provider to manipulate context menu. -
HierarchicalTree: Inject this provider to use hierarchical tree like structure. -
LayoutAnimation: Inject this provider animation to layouts. -
MindMap: Inject this provider to use mind map. -
PrintAndExport: Inject this provider to print or export the objects. -
RadialTree: Inject this provider to use radial tree like structure. -
Snapping: Inject this provider to snap the objects. -
SymmetricLayout: Inject this provider to render layout in symmetrical method. -
UndoRedo: Inject this provider to revert and restore the changes. -
Ej1Serialization- Inject this provider to load ej1 diagram json in ej2 diagram.
These modules should be imported and injected into the Diagram component using ej.diagrams.Diagram.Inject method as follows.
ej.diagrams.Diagram.Inject(
ej.diagrams.BpmnDiagrams,
ej.diagrams.ConnectorBridging,
ej.diagrams.ConnectorEditing,
ej.diagrams.ComplexHierarchicalTree,
ej.diagrams.DataBinding,
ej.diagrams.DiagramContextMenu,
ej.diagrams.HierarchicalTree,
ej.diagrams.LayoutAnimation,
ej.diagrams.MindMap,
ej.diagrams.PrintAndExport,
ej.diagrams.RadialTree,
ej.diagrams.Snapping,
ej.diagrams.SymmetricLayout,
ej.diagrams.UndoRedo
);
var diagram = new ej.diagrams.Diagram({
width: '100%',
height: '350px',
});
diagram.appendTo('#element');Basic Diagram elements
-
Node: Visualizes any graphical object using nodes, which can also be arranged and manipulated on a diagram page. -
Connector: Represents the relationship between two nodes. Three types of connectors provided as follows:
1) Orthogonal
2) Bezier
3) Straight
-
Port: Acts as the connection points of node or connector and allows you to create connections with only specific points. -
Annotation: Shows additional information by adding text or labels on nodes and connectors.
Flow Diagram
Create and add node to the diagram
Create and add a node (JSON data) with specific position, size.
var nodes = [
{
id: 'Start', width: 140, height: 50, offsetX: 300, offsetY: 50,
}
];
var diagram = new ej.diagrams.Diagram({
width: '100%', height: '600px', nodes: nodes
}, '#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/32.1.19/ej2-base/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-navigations/styles/tailwind3.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/32.1.19/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>Apply shape and style to node
Syncfusion® diagram control provides support to render many built-in shapes in diagram.
Please refer to Shapes to know about built-in Shapes.
- The appearance of a node can be customized by changing its
fillcolor,strokeColor,strokeWidth,borderColor,borderWidth,strokeDashArray,opacity, andshadow.
var nodes = [
{
id: 'Start', width: 140, height: 50, offsetX: 300, offsetY: 50,
annotations: [{
id: 'label1',
content: 'Start'
}],
shape: { type: 'Flow', shape: 'Terminator'},
//To define the shape of the node
shape: { type: 'Flow', shape: 'Terminator'},
//To define border style for the node.
borderColor:'orange',borderWidth:10,
//To define style for the node.
style:{fill:'red',strokeColor:'green',strokeWidth:5,strokeDashArray:'2 2'}
}
];
var diagram = new ej.diagrams.Diagram({
width: '100%', height: '600px', nodes: nodes
}, '#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/32.1.19/ej2-base/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-navigations/styles/tailwind3.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/32.1.19/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 other flowchart nodes to the diagram
Add multiple nodes with different shapes to create a complete flowchart. Each node can represent different process steps such as decisions, processes, or terminators.
var nodes = [
{
id: 'Start',
offsetY: 50,
annotations: [{ content: 'Start' }],
shape: { type: 'Flow', shape: 'Terminator' },
},
{
id: 'Init',
offsetY: 140,
annotations: [{ content: 'var i = 0;' }],
shape: { type: 'Flow', shape: 'Process' },
},
{
id: 'Condition',
offsetY: 230,
annotations: [{ content: 'i < 10?' }],
shape: { type: 'Flow', shape: 'Decision' },
},
];
var diagram = new ej.diagrams.Diagram(
{
width: '100%',
height: '600px',
nodes: nodes,
getNodeDefaults: (node) => {
node.height = 50;
node.width = 140;
node.offsetX = 300;
node.style = { fill: 'skyblue', strokeColor: 'skyblue' };
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/32.1.19/ej2-base/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-navigations/styles/tailwind3.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/32.1.19/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>Connect flow chart nodes
Connect these nodes by adding a connector using the connectors property of diagram and refer the source and target end by using the sourceID and targetID properties.
The required nodes and connectors can be added to form a complete flow diagram.
var nodes = [
{
id: 'Start',
offsetX: 300,
offsetY: 50,
annotations: [{ content: 'Start' }],
shape: { type: 'Flow', shape: 'Terminator' },
style: { fill: '#d0f0f1', strokeColor: '#797979' },
},
{
id: 'Init',
offsetX: 300,
offsetY: 140,
annotations: [{ content: 'var i = 0;' }],
shape: { type: 'Flow', shape: 'Process' },
style: { fill: '#fbfdc5', strokeColor: '#797979' },
},
{
id: 'Condition',
offsetX: 300,
offsetY: 230,
annotations: [{ content: 'i < 10?' }],
shape: { type: 'Flow', shape: 'Decision' },
style: { fill: '#c5efaf', strokeColor: '#797979' },
},
{
id: 'Print',
offsetX: 300,
offsetY: 320,
annotations: [{ content: "print('Hello!!');" }],
shape: { type: 'Flow', shape: 'PreDefinedProcess' },
style: { fill: '#f8eee5', strokeColor: '#797979' },
},
{
id: 'Increment',
offsetX: 300,
offsetY: 410,
annotations: [{ content: 'i++;' }],
shape: { type: 'Flow', shape: 'Process' },
style: { fill: '#fbfdc5', strokeColor: '#797979' },
},
{
id: 'End',
offsetX: 300,
offsetY: 500,
annotations: [{ content: 'End' }],
shape: { type: 'Flow', shape: 'Terminator' },
style: { fill: '#d0f0f1', strokeColor: '#797979' },
},
];
var connector = [
{ id: 'connector1', sourceID: 'Start', targetID: 'Init' },
{ id: 'connector2', sourceID: 'Init', targetID: 'Condition' },
{
id: 'connector3',
sourceID: 'Condition',
targetID: 'Print',
annotations: [{ content: 'Yes' }],
},
{
id: 'connector4',
sourceID: 'Condition',
targetID: 'End',
annotations: [{ content: 'No' }],
type: 'Orthogonal',
segments: [
{ type: 'Orthogonal', length: 30, direction: 'Right' },
{ type: 'Orthogonal', length: 300, direction: 'Bottom' },
],
},
{ id: 'connector5', sourceID: 'Print', targetID: 'Increment' },
{
id: 'connector6',
sourceID: 'Increment',
targetID: 'Condition',
type: 'Orthogonal',
segments: [
{ type: 'Orthogonal', length: 30, direction: 'Left' },
{ type: 'Orthogonal', length: 200, direction: 'Top' },
],
},
];
var diagram = new ej.diagrams.Diagram(
{
width: '100%',
height: '600px',
nodes: nodes,
connectors: connector,
getNodeDefaults: (node) => {
node.height = 50;
node.width = 140;
node.offsetX = 300;
return node;
},
getConnectorDefaults: (obj) => {
obj.type = 'Orthogonal';
obj.targetDecorator = { shape: 'Arrow', width: 10, height: 10 };
return obj;
},
},
'#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/32.1.19/ej2-base/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-navigations/styles/tailwind3.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/32.1.19/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>Default values for all nodes and connectors can be set using the getNodeDefaults and getConnectorDefaults properties, respectively. For example, if all nodes have the same width and height, such properties can be moved into getNodeDefaults.
Automatic organization chart
In the ‘Flow Diagram’ section, how to create a diagram manually was discussed. This section explains how to create and position the diagram automatically.
Create Business object (Employee information)
Define employee information as JSON data. The following code example shows an employee array whose, Name is used as an unique identifier and ReportingPerson property identifies each employee’s manager in the organizational hierarchy.
//Initialize data source...
let data = [{Name: "Elizabeth", Role: "Director" },
{ Name: "Christina", ReportingPerson: "Elizabeth", Role: "Manager" },
{ Name: "Yoshi", ReportingPerson: "Christina", Role: "Lead" },
{ Name: "Philip", ReportingPerson: "Christina", Role: "Lead" },
{ Name: "Yang", ReportingPerson: "Elizabeth", Role: "Manager" },
{ Name: "Roland", ReportingPerson: "Yang", Role: "Lead" },
{ Name: "Yvonne", ReportingPerson: "Yang", Role: "Lead" }
];Map data source to diagram
You can configure the above “Employee Information” with diagram, so that the nodes and connectors are automatically generated using the mapping properties. The following code example show how dataSourceSettings is used to map ID and parent with property name identifiers for employee information.
//Initialize data source...
let data = [
{ Name: 'Elizabeth', Role: 'Director' },
{ Name: 'Christina', ReportingPerson: 'Elizabeth', Role: 'Manager' },
{ Name: 'Yoshi', ReportingPerson: 'Christina', Role: 'Lead' },
{ Name: 'Philip', ReportingPerson: 'Christina', Role: 'Lead' },
{ Name: 'Yang', ReportingPerson: 'Elizabeth', Role: 'Manager' },
{ Name: 'Roland', ReportingPerson: 'Yang', Role: 'Lead' },
{ Name: 'Yvonne', ReportingPerson: 'Yang', Role: 'Lead' },
];
let items = new ej.data.DataManager(data, new ej.data.Query().take(7));
var diagram = new ej.diagrams.Diagram({
width: '100%',
height: '350px',
});
diagram.appendTo('#element');Rendering layout with Datasource
To create an organizational chart, the type of layout should be set as an OrganizationalChart. The following code example shows how DataManager is used to generate Layout based on the DataSourceSettings of the Diagram.
ej.diagrams.Diagram.Inject(
ej.diagrams.DataBinding,
ej.diagrams.HierarchicalTree
);
var data = [
{ Name: 'Elizabeth', Role: 'Director' },
{ Name: 'Christina', ReportingPerson: 'Elizabeth', Role: 'Manager' },
{ Name: 'Yoshi', ReportingPerson: 'Christina', Role: 'Lead' },
{ Name: 'Philip', ReportingPerson: 'Christina', Role: 'Lead' },
{ Name: 'Yang', ReportingPerson: 'Elizabeth', Role: 'Manager' },
{ Name: 'Roland', ReportingPerson: 'Yang', Role: 'Lead' },
{ Name: 'Yvonne', ReportingPerson: 'Yang', Role: 'Lead' },
];
var codes = {
Director: 'rgb(0, 139,139)',
Manager: 'rgb(30, 30,113)',
Lead: 'rgb(0, 100,0)',
};
function getNodeTemplate(node) {
node.annotations[0].content = node.data.Name;
node.style.fill = codes[node.data.Role];
}
var items = new ej.data.DataManager(data, new ej.data.Query().take(7));
var diagram = new ej.diagrams.Diagram({
width: '100%',
height: '350px',
mode: 'SVG',
snapSettings: { constraints: ej.diagrams.SnapConstraints.None },
dataSourceSettings: {
id: 'Name',
parentId: 'ReportingPerson',
dataManager: items,
},
layout: {
type: 'OrganizationalChart',
margin: { left: 10, top: 10 },
horizontalSpacing: 50,
verticalSpacing: 50,
orientation: 'TopToBottom',
getLayoutInfo: function (node, tree) {
if (!tree.hasSubTree) {
tree.orientation = 'Vertical';
tree.type = 'Alternate';
}
},
},
getNodeDefaults: function (obj, diagram) {
obj.height = 30;
obj.width = 70;
obj.shape = { type: 'Basic', shape: 'Rectangle' };
obj.annotations = [
{
id: 'label1',
style: {
fontSize: 11,
bold: true,
fontFamily: 'Segoe UI',
color: 'white',
},
},
];
return obj;
},
getConnectorDefaults: function (connector, diagram) {
connector.targetDecorator.shape = 'Arrow';
connector.cornerRadius = 7;
connector.type = 'Orthogonal';
return connector;
},
setNodeTemplate: function (node) {
return getNodeTemplate(node);
},
});
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/32.1.19/ej2-base/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-navigations/styles/tailwind3.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/32.1.19/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>Customize employee appearance
The following code examples indicate how to define the default appearance of nodes and connectors. The setNodeTemplate is used to update each node based on employee data.
ej.diagrams.Diagram.Inject(
ej.diagrams.DataBinding,
ej.diagrams.HierarchicalTree
);
var data = [
{ Name: 'Elizabeth', Role: 'Director' },
{ Name: 'Christina', ReportingPerson: 'Elizabeth', Role: 'Manager' },
{ Name: 'Yoshi', ReportingPerson: 'Christina', Role: 'Lead' },
{ Name: 'Philip', ReportingPerson: 'Christina', Role: 'Lead' },
{ Name: 'Yang', ReportingPerson: 'Elizabeth', Role: 'Manager' },
{ Name: 'Roland', ReportingPerson: 'Yang', Role: 'Lead' },
{ Name: 'Yvonne', ReportingPerson: 'Yang', Role: 'Lead' },
];
var codes = {
Director: 'rgb(0, 139,139)',
Manager: 'rgb(30, 30,113)',
Lead: 'rgb(0, 100,0)',
};
function getNodeTemplate(node) {
node.annotations[0].content = node.data.Name;
node.style.fill = codes[node.data.Role];
}
var items = new ej.data.DataManager(data, new ej.data.Query().take(7));
var diagram = new ej.diagrams.Diagram({
width: '100%',
height: '350px',
mode: 'SVG',
snapSettings: { constraints: ej.diagrams.SnapConstraints.None },
dataSourceSettings: {
id: 'Name',
parentId: 'ReportingPerson',
dataManager: items,
},
layout: {
type: 'OrganizationalChart',
margin: { left: 10, top: 10 },
horizontalSpacing: 50,
verticalSpacing: 50,
orientation: 'TopToBottom',
getLayoutInfo: function (node, tree) {
if (!tree.hasSubTree) {
tree.orientation = 'Vertical';
tree.type = 'Alternate';
}
},
},
getNodeDefaults: function (obj, diagram) {
obj.height = 30;
obj.width = 70;
obj.shape = { type: 'Basic', shape: 'Rectangle' };
obj.annotations = [
{
id: 'label1',
style: {
fontSize: 11,
bold: true,
fontFamily: 'Segoe UI',
color: 'white',
},
},
];
return obj;
},
getConnectorDefaults: function (connector, diagram) {
connector.targetDecorator.shape = 'Arrow';
connector.type = 'Orthogonal';
return connector;
},
setNodeTemplate: function (node) {
let codes = {
Director: 'rgb(0, 139,139)',
Manager: 'rgb(30, 30,113)',
Lead: 'rgb(0, 100,0)',
};
let content = new ej.diagrams.StackPanel();
content.id = node.id + '_outerstack';
content.orientation = 'Horizontal';
content.style.strokeColor = 'gray';
content.style.fill = codes[node.data.Role];
content.padding = { left: 5, right: 5, top: 5, bottom: 5 };
let innerContent = new ej.diagrams.ImageElement();
innerContent.style.strokeColor = 'blue';
innerContent.id = node.id + '_innerstack';
innerContent.style.fill = 'skyblue';
innerContent.width = 50;
innerContent.height = 50;
let text = new ej.diagrams.TextElement();
text.id = node.id + '_text';
text.content = node.data.Name;
text.margin = { left: 15, right: 5, top: 5, bottom: 5 };
text.style.color = 'black';
content.children = [innerContent, text];
return content;
},
});
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/32.1.19/ej2-base/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/tailwind3.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-navigations/styles/tailwind3.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/32.1.19/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>