How To Create Organization Chart In Angular Application

Introduction

 
In this article, we will learn how to create an Organization Chart in Angular Application. In this article, we use the PrimeNG component to create Organization Chart. PrimeNG is a free and open-source library of UI components. It is developed by PrimeTek Informatics. PrimeNG provides 80+ UI components in a single library, so there is no need to add any other library for different UIs. Learn more about primeNG from the below links,
Prerequisites
  • Basic Knowledge of Angular 2 or higher
  • Visual Studio Code
  • Node and NPM installed
Let's create an Angular project by using the following command,
  1. ng new KanData  
Open this project in Visual Studio Code and PrimeNG by using the following commands,
  1. npm install primeng --save  
  2. npm install primeicons --save  
Now install bootstrap by using the following command,
  1. npm install bootstrap --save   
Now open the styles.css file and add these lines,
  1. @import '~bootstrap/dist/css/bootstrap.min.css';   
  2. @import '../node_modules/primeng/resources/themes/saga-blue/theme.css';  
  3. @import '../node_modules/primeicons/primeicons.css';  
Now create a new component using the following command,
  1. ng g c Orgchart  
Now open orgchart.component.ts file and add the following code,
  1. import { Component, OnInit } from '@angular/core';  
  2. import { TreeNode } from 'primeng/api';  
  3.   
  4. @Component({  
  5.   selector: 'app-orgchart',  
  6.   templateUrl: './orgchart.component.html',  
  7.   styleUrls: ['./orgchart.component.css']  
  8. })  
  9. export class OrgchartComponent implements OnInit {  
  10.   
  11.   constructor() { }  
  12.   data1: TreeNode[];  
  13.   
  14.     data2: TreeNode[];  
  15.   
  16.     selectedNode: TreeNode;  
  17.   ngOnInit(): void {  
  18.     this.data1 = [{  
  19.       label: 'CEO',  
  20.       type: 'person',  
  21.       styleClass: 'p-person',  
  22.       expanded: true,  
  23.       data: {name:'Walter White''avatar''walter.jpg'},  
  24.       children: [  
  25.           {  
  26.               label: 'CFO',  
  27.               type: 'person',  
  28.               styleClass: 'p-person',  
  29.               expanded: true,  
  30.               data: {name:'Saul Goodman''avatar''saul.jpg'},  
  31.               children:[{  
  32.                   label: 'Tax',  
  33.                   styleClass: 'department-cfo'  
  34.               },  
  35.               {  
  36.                   label: 'Legal',  
  37.                   styleClass: 'department-cfo'  
  38.               }],  
  39.           },  
  40.           {  
  41.               label: 'COO',  
  42.               type: 'person',  
  43.               styleClass: 'p-person',  
  44.               expanded: true,  
  45.               data: {name:'Mike E.''avatar''mike.jpg'},  
  46.               children:[{  
  47.                   label: 'Operations',  
  48.                   styleClass: 'department-coo'  
  49.               }]  
  50.           },  
  51.           {  
  52.               label: 'CTO',  
  53.               type: 'person',  
  54.               styleClass: 'p-person',  
  55.               expanded: true,  
  56.               data: {name:'Jesse Pinkman''avatar''jesse.jpg'},  
  57.               children:[{  
  58.                   label: 'Development',  
  59.                   styleClass: 'department-cto',  
  60.                   expanded: true,  
  61.                   children:[{  
  62.                       label: 'Analysis',  
  63.                       styleClass: 'department-cto'  
  64.                   },  
  65.                   {  
  66.                       label: 'Front End',  
  67.                       styleClass: 'department-cto'  
  68.                   },  
  69.                   {  
  70.                       label: 'Back End',  
  71.                       styleClass: 'department-cto'  
  72.                   }]  
  73.               },  
  74.               {  
  75.                   label: 'QA',  
  76.                   styleClass: 'department-cto'  
  77.               },  
  78.               {  
  79.                   label: 'R&D',  
  80.                   styleClass: 'department-cto'  
  81.               }]  
  82.           }  
  83.       ]  
  84.   }];  
  85.   
  86. }  
  87. }  
Now open orgchart.component.html file and add the following code,
  1. <div class="container" style="margin-top:10px;margin-bottom: 24px;">    
  2.     <div class="col-sm-12 btn btn-success">    
  3.       How To Create Organization Chart in Angular Application    
  4.     </div>    
  5.   </div>   
  6.   <div  class="col-sm-12">  
  7.   <p-organizationChart [value]="data1" selectionMode="single" [(selection)]="selectedNode"   
  8.       styleClass="company">  
  9.       <ng-template let-node pTemplate="person">  
  10.         <div class="node-content row e" style=" width: 225px !important;  
  11.                          margin-top: -11px; margin-bottom: -11px;">  
  12.             
  13.           <div class="relatipn_ship_css_client rel_client_one"><span class="rel_node_name" style="padding-left: 83px;">{{node.name}}</span><br /></div>  
  14.           <div class="name_css_profile name_profile_one"><span class="rel_label_name">{{node.label}}</span></div>  
  15.         </div>  
  16.       </ng-template>  
  17.   </p-organizationChart>  
  18.   </div>  
Now open orgchart.component.css file and add the following css classes,
  1. .node-header, .node-content {  
  2.     padding: .5em .7rem;  
  3. }  
  4.   
  5. .node-header {  
  6.     background-color: #495ebb;  
  7.     color: #ffffff;  
  8. }  
  9.   
  10. .node-content {  
  11.     text-align: center;  
  12.     border: 1px solid #495ebb;  
  13. }  
  14.   
  15. .node-content img {  
  16.     border-radius: 50%;  
  17. }  
  18.   
  19. .department-cfo {  
  20.     background-color: #7247bc;  
  21.     color: #ffffff;  
  22. }  
  23.   
  24. .department-coo {  
  25.     background-color: #a534b6;  
  26.     color: #ffffff;  
  27. }  
  28.   
  29. .department-cto {  
  30.     background-color: #e9286f;  
  31.     color: #ffffff;  
  32. }  
  33.   
  34. :host / .p-organizationchart .p-organizationchart-node-content:hover {  
  35.     background: #DEEDF9 !important;  
  36.     cursor: pointer;  
  37.   }  
  38.   :host /deep/ .p-organizationchart .p-organizationchart-node-content {  
  39.     border: 1px solid #e0e5e6 !important;  
  40.     color: #495057 !important;  
  41.     padding: 0.6rem !important;  
  42.     border-radius: 5px !important;  
  43.     background-color: #ffffff !important;  
  44.     margin-top: -11px;  
  45.     margin-bottom: -11px !important;  
  46.   }  
  47.   :host /deep/ .p-organizationchart .p-organizationchart-line-down {  
  48.     height: 30px !important;  
  49.   }  
  50.   
  51.   :host /deep/ .node-content {  
  52.     text-align: center;  
  53.     border: 0px solid #b0c2d6;  
  54.   }  
  55.   
  56.   :host /deep/ .node-content {  
  57.     text-align: center;  
  58.     border: none !important;  
  59.     /* border: 1px solid #bdc2d6; */  
  60.     border-radius: 2px !important;  
  61.   }  
Now open app.module.ts file and add the following code,
  1. import { NgModule } from '@angular/core';  
  2. import { BrowserModule } from '@angular/platform-browser';  
  3. import { HttpClientModule } from "@angular/common/http";  
  4. import { AppComponent } from './app.component';  
  5. import {OrganizationChartModule} from 'primeng/organizationchart';  
  6. import { BrowserAnimationsModule } from '@angular/platform-browser/animations';  
  7. import { OrgchartComponent } from './orgchart/orgchart.component';  
  8.   
  9. @NgModule({  
  10.   declarations: [  
  11.     AppComponent,  
  12.     OrgchartComponent  
  13.   ],  
  14.   imports: [  
  15.     BrowserModule,HttpClientModule,OrganizationChartModule,BrowserAnimationsModule  
  16.   ],  
  17.   providers: [],  
  18.   bootstrap: [AppComponent]  
  19. })  
  20. export class AppModule { }  
Now open app.component.html file and add the following code,
  1. <app-orgchart></app-orgchart>  
Now, please run the application using the 'npm start' command and check the result.
 
How To Create Organization Chart In Angular Application
How To Create Organization Chart In Angular Application

Summary

 
In this article, we learned how to create an Organization Chart in Angular Applications.