Getting started in EJ2 TypeScript Diagram control

31 Jan 202624 minutes to read

This guide shows how to create a basic Diagram component and configure key features in TypeScript using the Essential® JS 2 quickstart seed repository.

This application is configured with webpack.config.js and the latest webpack-cli. It requires Node v14.15.0 or later. For more about webpack, see the official documentation.

Dependencies

The following packages 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-splitbuttons

Set up the development environment

Open a command prompt in your working directory and clone the Syncfusion® JavaScript (Essential® JS 2) quickstart project from GitHub:

git clone https://github.com/SyncfusionExamples/ej2-quickstart-webpack- ej2-quickstart

After cloning, navigate to the ej2-quickstart folder:

cd ej2-quickstart

Add Syncfusion® JavaScript packages

Syncfusion® JavaScript (Essential® JS 2) packages are available on the npmjs.com public registry. You can install all Syncfusion® JavaScript (Essential® JS 2) controls in a single @syncfusion/ej2 package or individual packages for each control.

The quickstart is preconfigured with the @syncfusion/ej2 dependency in ~/package.json. Install the npm packages:

npm install

Import Syncfusion® CSS styles

To render the Diagram component, import the Diagram and dependent component styles in ~/src/styles/styles.css:

@import '../../node_modules/@syncfusion/ej2-diagrams/styles/tailwind3.css';
@import "../../node_modules/@syncfusion/ej2-base/styles/tailwind3.css";
@import "../../node_modules/@syncfusion/ej2-popups/styles/tailwind3.css";
@import "../../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind3.css";
@import "../../node_modules/@syncfusion/ej2-navigations/styles/tailwind3.css";

Module injection

The Diagram component is modular. Inject feature modules to enable specific capabilities. The following modules are commonly used:

  • BpmnDiagrams: Adds built-in BPMN shapes.
  • ConnectorBridging: Draws bridges where connectors cross.
  • ConnectorEditing: Enables editing of connector segments.
  • ComplexHierarchicalTree: Renders complex hierarchical tree layouts.
  • DataBinding: Populates nodes from a data source.
  • DiagramContextMenu: Enables a context menu.
  • HierarchicalTree: Renders hierarchical tree layouts.
  • LayoutAnimation: Animates layout updates.
  • MindMap: Enables mind map layouts.
  • PrintAndExport: Prints or exports diagram objects.
  • RadialTree: Renders radial tree layouts.
  • Snapping: Snaps objects to alignment guides.
  • SymmetricLayout: Renders symmetric layouts.
  • UndoRedo: Reverts and restores changes.
  • Ej1Serialization: Loads EJ1 Diagram JSON into EJ2 Diagram.

Import and inject modules using Diagram.Inject:

import { Diagram, HierarchicalTree, MindMap, RadialTree, ComplexHierarchicalTree, DataBinding, Snapping, PrintAndExport, BpmnDiagrams, SymmetricLayout, ConnectorBridging, UndoRedo, LayoutAnimation, DiagramContextMenu, ConnectorEditing, Ej1Serialization } from '@syncfusion/ej2-diagrams';

Diagram.Inject(
    BpmnDiagrams,
    ConnectorBridging,
    ConnectorEditing,
    ComplexHierarchicalTree,
    DataBinding,
    DiagramContextMenu,
    HierarchicalTree,
    LayoutAnimation,
    MindMap,
    PrintAndExport,
    RadialTree,
    Snapping,
    SymmetricLayout,
    UndoRedo,
    Ej1Serialization
);

Add a diagram to the project

Add a container element for the Diagram to your index.html. src/index.html

<!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 rel="shortcut icon" href="resources/favicon.ico" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
</head>

<body>
    <!-- Container that renders the Diagram -->
    <div id='container'>
    </div>
</body>

</html>

Now import the Diagram component in app.ts, create an instance, and append it to #container. src/app/app.ts

The following example shows a basic Diagram:

 import { Diagram } from '@syncfusion/ej2-diagrams';
let diagram: Diagram = new Diagram({
    width: '100%', height: '600px'
});
diagram.appendTo('#container');

Run the application in the browser:

npm run start

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.

import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let nodes: NodeModel[] = [{
    // Unique name for the node
    id: "Start",
    // Position of the node
    offsetX: 300,
    offsetY: 50,
    // Size of the node
    width: 140,
    height: 50,
}];
// initialize Diagram component
let diagram: Diagram = new Diagram({
    width: '100%', height: '600px',
    // Add node
    nodes: nodes
});
// 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/32.1.19/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/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>

Apply shape and style to a node

Syncfusion® diagram control provides support to render many built-in shapes in diagram.
Please refer to Shapes to know about built-in Shapes.

import { Diagram, NodeModel } from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let nodes: NodeModel[] = [{
        // Unique name for the node
        id: "Start",
        // Position of the node
        offsetX: 300,
        offsetY: 50,
        // Size of the node
        width: 140,
        height: 50,
        // Text(label) added to the node
        annotations: [{
            id: 'label1',
            content: 'Start'
        }],
        // Shape for 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'}
            }];
// initialize Diagram component
let diagram: Diagram = new Diagram({
    width: '100%', height: '600px',
    // Add node
    nodes: nodes
});
// 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/32.1.19/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/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 additional flowchart nodes

Add multiple nodes with different flowchart shapes to the diagram.

import { Diagram, NodeModel, ConnectorModel } from '@syncfusion/ej2-diagrams';
let nodes: NodeModel[] = [
  {
    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' },
  },
];
let diagram: Diagram = new Diagram({
  width: '100%',
  height: '600px',
  nodes: nodes,
  getNodeDefaults: (node: NodeModel) => {
    node.height = 50;
    node.width = 140;
    node.offsetX = 300;
    node.style = { fill: 'skyblue', strokeColor: 'skyblue' };
    return 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" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/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>

Connect flowchart 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.

import { Diagram, NodeModel, ConnectorModel } from '@syncfusion/ej2-diagrams';
let nodes: NodeModel[] = [
  {
    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' },
  },
];
let connector: ConnectorModel[] = [
  { 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' },
    ],
  },
];
let diagram: Diagram = new Diagram({
  width: '100%',
  height: '600px',
  nodes: nodes,
  connectors: connector,
  getNodeDefaults: (node: NodeModel) => {
    node.height = 50;
    node.width = 140;
    node.offsetX = 300;
    return node;
  },
  getConnectorDefaults: (obj: ConnectorModel): ConnectorModel => {
    obj.type = 'Orthogonal';
    obj.targetDecorator = { shape: 'Arrow', width: 10, height: 10 };
    return obj;
  },
});
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/32.1.19/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/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>

Default values for all nodes and connectors can be set using getNodeDefaults and getConnectorDefaults. For example, if all nodes share the same width and height, move those properties into getNodeDefaults.

Automatic organizational chart

The Flowchart section showed how to build a diagram manually. This section explains how to generate and position a diagram automatically.

Create business data (employee information)

Define employee information as JSON. In the data below, Name is the unique identifier and ReportingPerson specifies the manager the employee reports to.

//Initialize data source...
let data: object[] = [{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 the data source to the diagram

Configure the employee data with the diagram so nodes and connectors are generated automatically using mapping properties. The example below shows how dataSourceSettings maps the ID and parent fields.

//Initialize data source...

let data: object[] = [{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: DataManager = new DataManager(data as JSON[], new Query().take(7));
//Initialize data source...
let diagram: Diagram = new Diagram({
    width: '100%', height: '600px',
//Configure data source for diagram
    dataSourceSettings: {
        id: 'Name', parentId: 'ReportingPerson', dataManager: items
    }
});

Render layout with a data source

To create an organizational chart, set the layout type to OrganizationalChart. The example below shows how DataManager generates the layout based on the diagram dataSourceSettings.

import {
    Diagram,
    ConnectorModel,
    Node,
    DataBinding,
    HierarchicalTree,
    TreeInfo,
    SnapConstraints,
  } from '@syncfusion/ej2-diagrams';
  Diagram.Inject(DataBinding, HierarchicalTree);
  import { DataManager, Query } from '@syncfusion/ej2-data';
  export interface EmployeeInfo {
    Name: string;
    Role: string;
  }
  //To represent the roles
  let codes: object[] = {
    Director: 'rgb(0, 139,139)',
    Manager: 'rgb(30, 30,113)',
    Lead: 'rgb(0, 100,0)',
  };
  // Bind custom data with node
  function getNodeTemplate(node) {
    node.annotations[0].content = (node.data as EmployeeInfo).Name;
    node.style.fill = codes[(node.data as EmployeeInfo).Role];
  }
  let data: object[] = [
    { 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: DataManager = new DataManager(data as JSON[], new Query().take(7));
  let diagram: Diagram = new Diagram({
    width: '100%',
    height: '600px',
    snapSettings: { constraints: SnapConstraints.None },
    //Use automatic layout to arrange elements on the page
    layout: {
      type: 'OrganizationalChart',
      margin: { left: 10, top: 10 },
      horizontalSpacing: 50,
      verticalSpacing: 50,
      orientation: 'TopToBottom',
      getLayoutInfo: (node: Node, tree: TreeInfo) => {
        if (!tree.hasSubTree) {
          tree.orientation = 'Vertical';
          tree.type = 'Alternate';
        }
      },
    },
    dataSourceSettings: {
      id: 'Name',
      parentId: 'ReportingPerson',
      dataManager: items,
    },
    getNodeDefaults: (obj: Node, diagram: Diagram) => {
      obj.height = 30;
      obj.width = 70;
      obj.shape = { type: 'Basic', shape: 'Rectangle' };
      obj.annotations = [
        { content: (obj.data as EmployeeInfo).Name, style: { color: 'white' } },
      ];
      obj.style.fill = codes[(obj.data as EmployeeInfo).Role];
      return obj;
    },
    getConnectorDefaults: (connector: ConnectorModel, diagram: Diagram) => {
      connector.targetDecorator.shape = 'Arrow';
      connector.cornerRadius = 7;
      connector.type = 'Orthogonal';
      return connector;
    },
  });
  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/32.1.19/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/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>

Customize employee appearance

The examples below show how to define the default appearance of nodes and connectors. Use setNodeTemplate to update each node based on employee data.

import {
    Diagram,
    ConnectorModel,
    Node,
    DataBinding,
    HierarchicalTree,
    TreeInfo,
    SnapConstraints,
    NodeModel,
    ImageElement,
    TextElement,
    StackPanel,
  } from '@syncfusion/ej2-diagrams';
  Diagram.Inject(DataBinding, HierarchicalTree);
  import { DataManager, Query } from '@syncfusion/ej2-data';
  export interface EmployeeInfo {
    Name: string;
    Role: string;
  }
  //To represent the roles
  let codes: object[] = {
    Director: 'rgb(0, 139,139)',
    Manager: 'rgb(30, 30,113)',
    Lead: 'rgb(0, 100,0)',
  };
  // Bind custom data with node
  function getNodeTemplate(node) {
    node.annotations[0].content = (node.data as EmployeeInfo).Name;
    node.style.fill = codes[(node.data as EmployeeInfo).Role];
  }
  let data: object[] = [
    { 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: DataManager = new DataManager(data as JSON[], new Query().take(7));
  let diagram: Diagram = new Diagram({
    width: '100%',
    height: '600px',
    snapSettings: { constraints: SnapConstraints.None },
    //Use automatic layout to arrange elements on the page
    layout: {
      type: 'OrganizationalChart',
      margin: { left: 10, top: 10 },
      horizontalSpacing: 50,
      verticalSpacing: 50,
      orientation: 'TopToBottom',
      getLayoutInfo: (node: Node, tree: TreeInfo) => {
        if (!tree.hasSubTree) {
          tree.orientation = 'Vertical';
          tree.type = 'Alternate';
        }
      },
    },
    dataSourceSettings: {
      id: 'Name',
      parentId: 'ReportingPerson',
      dataManager: items,
    },
    getNodeDefaults: (obj: Node, diagram: 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: (connector: ConnectorModel, diagram: Diagram) => {
      connector.targetDecorator.shape = 'Arrow';
      connector.type = 'Orthogonal';
      return connector;
    },
    setNodeTemplate: (node: NodeModel) => {
      let codes: Object = {
        Director: 'rgb(0, 139,139)',
        Manager: 'rgb(30, 30,113)',
        Lead: 'rgb(0, 100,0)',
      };
      let content = new StackPanel();
      content.id = node.id + '_outerstack';
      content.orientation = 'Horizontal';
      content.style.strokeColor = 'gray';
      content.style.fill = (codes as any)[
        (node.data as EmployeeInfo).Role
      ] as string;
      content.padding = { left: 5, right: 5, top: 5, bottom: 5 };
      let innerContent = new ImageElement();
      innerContent.style.strokeColor = 'blue';
      innerContent.id = node.id + '_innerstack';
      innerContent.style.fill = 'skyblue';
      innerContent.width = 50;
      innerContent.height = 50;
      let text = new TextElement();
      text.id = node.id + '_text';
      text.content = (node.data as EmployeeInfo).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" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/ej2-diagrams/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.1.19/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

See the JavaScript Diagram feature tour for highlights. Explore the JavaScript Diagram examples to learn how to present and manipulate data.