Click here to Skip to main content
Click here to Skip to main content

Organization Chart Generator

By , 4 Mar 2008
 
Screenshot - Orgchart.jpg

Introduction

Due to various issues, this version was rewritten. The article for it can be found here.

Many organizations (or sub organizations, such as departments, teams, etc.) have Intranet sites which supply details regarding the specific organization.
It is usually required to display an organization chart. There are a few solutions:

  1. Use a static image, from PowerPoint, etc. - any change will require regeneration of image
  2. Use a commercial product - these cost money
  3. Design your own product - this is what I did

The article discusses a DLL, which accepts a datatable and returns an image with the organization chart.
The DLL can be called from Windows Forms or ASP.NET.
Most of the parameters (sizes, colors) can be controlled by the developer using the DLL - they are exposed as properties.

A Personal Note: As this is my first article, I would greatly appreciate comments on both the article and the code itself. I realize that the code still needs to be optimized. When I do so, I'll update the article.

Using the Code

The download zip file contains the C# solution, which has two projects:

  1. OrgChartGenerator, which contains the code for the DLL that generates the Org Chart.
  2. TestOrgchart, which contains a Windows Forms application that demonstrates the abilities of OrgChartGenerator and its usage.

Constructor

The OrgChartGenerator accepts a Typed DataSet in the constructor, with the employee list, in order to generate the Org Chart. The OrgChartGenerator.OrgData.OrgDetailsDataTable is the DataTable that the class will accept in the constructor. It can contain the whole organization structure or just parts of it. If it contains the whole organization, multiple Org Charts can be created from the same instance of the object. The structure of the DataTable is as follows:

  • string EmployeeID - Primary Key - the ID of the employee - employee number, email, login code or anything else that uniquely identifies the employee
  • string EmployeeName - The employee name
  • string BossEmployeeID - The employee ID of this Employees' Boss. This defines the organization structure.
  • string EmployeeTitle - The title/Job description for the current employee.

Methods

There is just one method - GenerateOrgChart, which is overloaded. The difference between the overloads is that one method accepts as parameters the required size of the resulting image and resizes it before returning it. The other returns it in the size as determined by calculation - the number of boxes (both x & y) times the number of employees.
This method returns a System.IO.Stream, which can be used to display an image, save to file, or any other activity. Another important parameter for this method is the BossEmployeeID. This parameter determines which employee will be at the top of the Org Chart.

This class/DLL can be used from a Windows Forms application in the following way:

//First - create the object
private void TestForm_Load(object sender, EventArgs e)
{
   //Build the data for the org chart
   //This will be done differently in real life. 
   //The DataTable will be filled from a database, probably.
   OrgChartGenerator.OrgData.OrgDetailsDataTable myOrgData = 
     new OrgChartGenerator.OrgData.OrgDetailsDataTable();
   myOrgData.AddOrgDetailsRow("1111", "Alon", "", "Manager");
   myOrgData.AddOrgDetailsRow("1112", "Yoram", "1111", "Team Leader");
   myOrgData.AddOrgDetailsRow("1113", "Dana", "1111", "Team Leader");
   myOrgData.AddOrgDetailsRow("1114", "Moshe", "1113", "SW Engineer");
   myOrgData.AddOrgDetailsRow("1115", "Oren", "1113", "SW Engineer");
   myOrgData.AddOrgDetailsRow("1116", "Noa", "1113", "SW Engineer");
   myOrgData.AddOrgDetailsRow("1117", "Mor", "1112", "Consultant");
   myOrgData.AddOrgDetailsRow("1118", "Omer", "1112", "Consultant");
   //instantiate the object
   myOrgChart = new OrgChartGenerator.OrgChart(myOrgData);
}
//Then - Generate the Org Chart
/// <summary>
/// Generate the chart and link it to the picturebox control
/// </summary>
private void ShowChart()
{
   picOrgChart.Image = 
     Image.FromStream(
      myOrgChart.GenerateOrgChart("1111",
         System.Drawing.Imaging.ImageFormat.Bmp));
}

Properties

The image that is returned has default properties. In order to control the sizes and colors of the Org Chart, these properties are can be changed (all sizes are in pixels):

  • BoxFillColor - Which color will fill the boxes
  • BoxWidth - The width of each box
  • BoxHeight - The height of each box
  • Margin - The margin from the edges of the image to the boxes
  • HorizontalSpace - The space between the 2 employee boxes on the same level
  • VerticalSpace - The space between 2 levels of employees
  • FontSize - The font size for the employee details
  • LineColor - The color of both the box line and the connecting line
  • LineWidth - The width of both the box line and the connecting line
  • BGColor - The color in the Background of the image
  • FontColor - The font color
  • EmployeeData - A SortedDictionary, which contains data regarding the position of each employee on the chart. This can be used for checking if a specific Employee was clicked.

Changing these properties could generate weird results, so care should be taken by the user. For example - it would not look good if both the BoxFillColor and the FontColor were the same.

ASP.NET Usage

In order to use the DLL in ASP.NET, create a Web form which returns the response as a stream. Let's call it OrgChartImage.aspx. The codebehind file should look like this:

protected void Page_Load(object sender, EventArgs e)
{
  // Change the response headers to output a JPEG image.
  this.Response.Clear();
  this.Response.ContentType = "image/jpeg";
  //Build the image 
  //In real life this can be done using parameters
  OrgChartGenerator.OrgChart myOrgChart;
  OrgChartGenerator.OrgData.OrgDetailsDataTable myOrgData = 
    new OrgChartGenerator.OrgData.OrgDetailsDataTable();
  myOrgData.AddOrgDetailsRow("1111", "Alon", "", "Manager");
  myOrgData.AddOrgDetailsRow("1112", "Yoram", "1111", "Team Leader");
  myOrgData.AddOrgDetailsRow("1113", "Dana", "1111", "Team Leader");
  myOrgData.AddOrgDetailsRow("1114", "Moshe", "1113", "SW Engineer");
  myOrgData.AddOrgDetailsRow("1115", "Oren", "1113", "SW Engineer");
  myOrgData.AddOrgDetailsRow("1116", "Noa", "1113", "SW Engineer");
  myOrgData.AddOrgDetailsRow("1117", "Mor", "1112", "Consultant");
  myOrgData.AddOrgDetailsRow("1118", "Omer", "1112", "Consultant");

  //instantiate the object
  myOrgChart = new OrgChartGenerator.OrgChart(myOrgData);
  System.Drawing.Image OC = 
     System.Drawing.Image.FromStream(myOrgChart.GenerateOrgChart("1111", 
        System.Drawing.Imaging.ImageFormat.Bmp));
  // Write the image to the response stream in JPEG format.
  OC.Save(this.Response.OutputStream, ImageFormat.Jpeg);
  OC.Dispose();
}

Calling the OrgChartImage.aspx can be done from any IMG tag:

<img src="OrgChartImage.aspx"/>

Trapping MouseClick Events

The DLL exposes the EmployeeData property, which is a System.Collections.Generic.SortedDictionary. The code calling the DLL can use this property in order to see if a specific employee was clicked. This can be used for various purposes, such as linking to an employee image, showing other employee data or showing the next level of the organization chart. In ASP.NET, this can be done by defining an ImageMap control. Here's an example of how it is used in the WinFroms sample application:

private void picOrgChart_MouseClick(object sender, MouseEventArgs e)
{
 //determine if the mouse clicked on a box, if so, show the employee name.
 string SelectedEmployee = "No employee";
 foreach (OrgChartGenerator.OrgChartRecord EmpRec in  
                             myOrgChart.EmployeeData.Values)
   {
     if (e.X >= EmpRec.EmployeePos.Left &&
         e.X <= EmpRec.EmployeePos.Right &&
         e.Y >= EmpRec.EmployeePos.Top &&
         e.Y <= EmpRec.EmployeePos.Bottom)
         {
           SelectedEmployee = EmpRec.EmployeeData.EmployeeName;
           break;
         }
   }
   MessageBox.Show(SelectedEmployee);
}

Inside the Black Box

OrgChart.cs includes the main functionality for the application. The first method, BuildTree, is used recursively to build an XmlDocument of employee IDs and calculate the box positions. Another recursive method, DrawChart(), draws the boxes and writes the texts inside them. Then it draws the lines connecting all the boxes and completes the organization chart. If necessary, the image is then resized and a stream is returned to the caller.

History

  • April 12, 2007
    • Initial release
  • April 18, 2007 
    • Fixed a bug in the calculation of image size
    • Added functionality for exposing the location of each employee, thus enabling to trap clicks on specific employees
  • April 19, 2007
    • Fixed some more bugs in location calculation
  • April 28, 2007
    • Following the great tips of Seishin#, rebuilt part of the mechanism. Now using an XmlDocument as a tree and drawing the tree recursively. This also fixed the overlapping box bugs that were reported.
  • April 30, 2007
    • Another small box positioning issue fixed
  • September 16, 2007
    • Due to various issues, this version was rewritten. The article for it can be found here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rotem Sapir
Web Developer
Israel Israel
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questioncannot convert to VS2010memberyanyanstar7 May '13 - 18:19 
Questionmultiple employees under multiple managersmemberFaheem Uddin Qureshi12 Mar '13 - 2:15 
QuestionOrganisation chart generator by Rotem Sapirmemberyogesh310517 Feb '13 - 2:38 
GeneralMy vote of 3memberravipendem14 Feb '13 - 4:38 
GeneralMy vote of 5memberbharanisanmdot24 Dec '12 - 1:07 
QuestionExcellent Application . Need help to change Font Style for Node NotememberKishoreSrinu1 Nov '12 - 20:25 
QuestionI want to view my chart by VerticalmemberThanhTrungDo11 Oct '12 - 20:19 
QuestionNeed help to avoid boxes colliding/ overlapping with each othermembervikas sangal6 Feb '12 - 21:58 
BugRe: Need help to avoid boxes colliding/ overlapping with each othermemberhelmidj11 Oct '12 - 22:33 
Questionneed help to add person picture to organizationmembermasomeh19 Nov '11 - 18:40 
GeneralMy vote of 5memberonurag1911 Nov '11 - 0:14 
GeneralMy vote of 5memberKamba_prj31 Oct '11 - 1:52 
QuestionBox sizememberusmanmughal27 Sep '11 - 21:36 
Generalneed help to use it from databasemembersvknair8 May '11 - 23:25 
AnswerRe: need help to use it from databasememberY Jayan17 May '11 - 2:13 
GeneralRe: need help to use it from databasemembersvknair24 Aug '11 - 22:55 
GeneralRe: need help to use it from databasememberY Jayan25 Aug '11 - 22:14 
GeneralMy vote of 5memberjuwariashafique3 May '11 - 22:10 
GeneralProblem with EmployeeData propertymemberMember 779593831 Mar '11 - 4:10 
GeneralRe: Problem with EmployeeData propertymemberRotem Sapir31 Mar '11 - 15:41 
GeneralRepliesmemberRotem Sapir1 Sep '10 - 7:21 
Questionhow to get each item locationmemberMahmoud_Farahat31 Aug '10 - 4:37 
GeneralMy vote of 3membervipul_007127 Aug '10 - 0:49 
GeneralMy vote of 5memberopranzini6 Jul '10 - 6:50 
GeneralGreat Workmemberzaghmout29 Jun '10 - 1:37 
GeneralAdd datamemberapana mahajan4 Jun '10 - 21:08 
QuestionSetting Background Image for NodememberMember 429902921 Apr '10 - 19:33 
AnswerRe: Setting Background Image for NodememberRotem Sapir6 May '10 - 22:40 
GeneralOrg Generator With ASP.NETmembertmakhoul30 Nov '09 - 10:30 
GeneralRe: Org Generator With ASP.NETmemberRotem Sapir5 Dec '09 - 20:35 
GeneralRe: Org Generator With ASP.NETmemberjeffvfren26 Feb '11 - 1:29 
QuestionFireFox (Mozilla) & Safari issuesmemberSridhar Narasimhan29 Jun '09 - 23:04 
AnswerRe: FireFox (Mozilla) & Safari issuesmemberRotem Sapir29 Jun '09 - 23:11 
GeneralGreat Work !!!memberMetallicAnshul15 Apr '09 - 20:44 
GeneralRe: Great Work !!!memberRotem Sapir29 Jun '09 - 23:09 
GeneralRe: Great Work !!! [modified]membercris033411 Nov '09 - 11:51 
GeneralRe: Great Work !!!memberRotem Sapir14 Nov '09 - 22:32 
GeneralMy vote of 1memberMember 368237117 Jan '09 - 2:10 
GeneralRe: My vote of 1memberRotem Sapir29 Jun '09 - 23:09 
GeneralRe: My vote of 1memberdawmail33320 Jun '10 - 13:27 
GeneralRe: My vote of 1memberdawmail33320 Jun '10 - 13:24 
Questionlooking for cyclic graph chartmemberrasana6 Jun '08 - 5:32 
AnswerRe: looking for cyclic graph chartmemberRotem Sapir7 Jun '08 - 7:57 
GeneralRe: looking for cyclic graph chartmemberrasana7 Jun '08 - 19:13 
GeneralRe: looking for cyclic graph chartmemberRotem Sapir7 Jun '08 - 19:17 
GeneralRe: looking for cyclic graph chart [modified]memberrasana10 Jun '08 - 4:43 
Questiongreat workmemberrasana6 Jun '08 - 4:05 
AnswerRe: great workmemberrasana6 Jun '08 - 4:15 
GeneralWPF Projectmembermo_kondori5 Mar '08 - 5:53 
GeneralRe: WPF ProjectmemberRotem Sapir9 Mar '08 - 4:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 4 Mar 2008
Article Copyright 2007 by Rotem Sapir
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid