Click here to Skip to main content
11,484,162 members (69,165 online)
Click here to Skip to main content

Creating VISIO Organigrams using C#

, 16 Sep 2010 CPOL 61.8K 7.2K 37
Rate this:
Please Sign up or sign in to vote.
This article shows how to create an organigram (organizational charts) in VISIO using C#.

Introduction

This article discusses how to create organigrams also known as organograms or organizational charts. An organigram is the departmental structure of an organization and their hierarchal relations. In this article, we will discuss how to dynamically generate a VISIO file containing the organizational chart of your organization while reading data from a data source (supposedly you have your organization structure stored in a database or other data source).

Background

To know how to automate VISIO 2007, the best thing to do is to install VISIO 2007 SDK. You can download the SDK from Microsoft site from this link.

VISIO SDK contains the sample code in VB and C# on how to use VISIO objects and automate VISIO drawings.

Using the Code

The source code is in the Class Library Ids.OrganogramDesigner. This library contains the class Box. The class Box represents one department or entity in your organization. The class Box contains properties that can be manipulated and reflected on the orgnigram drawing. The following is the list of properties:

  • Name: The name to appear on the organigram box
  • Parent: The parent Box to create a hierarchy between entities
  • LevelNumber: The level number of the box. Level Number 0 is the top level (the root). As the level number increases, the box goes down to the hierarchy.
  • DisplayOrder: Used to order boxes on the same level
  • BackColor: The back color (or fill color) used for the box
  • ForeColor: The fore color (or pen color) used for the text in the box
  • Width: The width of the box (can be changed if you want to use very small boxes in case of large organigrams)
  • Height: The height of the box (can be changed if you want to use very small boxes in case of large organigrams)
  • Hyperlink: A URL that when clicking on a box will direct you to the page

Then we have the class Designer. The class Designer when instantiated needs to be assigned the following properties:

  • VisioTemplatePath: which should be in program files folder --> Microsoft Office --> Office12 --> 1033 --> ORGCH_M.VST
  • SaveFolder: The path where to Save the generated Visio files. The path is only the folder path. The files are generated with a GUID.
//generate the VISIO file

List<Box> boxes = new List<Box>();

//adding a new box to the list of boxes
boxes.Add(new Box("1", "General Manager"));

boxes[0].Type = BoxTypes.Executive;
boxes.Add(new Box("2", "Research & Development", "1", 1));
boxes.Add(new Box("3", "Customer Support", "1", 1));
boxes.Add(new Box("4", "Sales", "1", 1));
boxes.Add(new Box("5", "Marketing", "1", 1));
boxes.Add(new Box("6", "Accounting", "1", 1));

//setting some properties such as fore color, back color
boxes[1].Type = BoxTypes.Manager;
boxes[1].ForeColor = Colors.Red;

boxes[2].BackColor = System.Drawing.Color.LightGreen;

boxes.Add(new Box("9", "Project Management", "2", 2));
boxes.Add(new Box("10", "Quality Assurance", "2", 2));
boxes.Add(new Box("11", "System Analyst", "2", 2));
boxes.Add(new Box("12", "Development", "2", 2)); 

Ids.OrganogramDesigner.Designer designer = new Designer();
designer.Boxes = boxes;
designer.SaveFolder = @"d:\Projects\DrawVisio\"; 
designer.VisioTemplatePath = @"C:\Program Files 
	(x86)\Microsoft Office\Office12\1033\ORGCH_M.VST"; 
designer.GenerateDiagram();    

The output of GenerateDiagram is the following:

Click to enlarge image

Further Explanation

Here I would like to explain how the GenerateDiagram() method works. The most important thing to know is the following:

How to Create a New VISIO Document

Microsoft.Office.Interop.Visio.Application application = 
		new Microsoft.Office.Interop.Visio.Application();  
application.Visible = false; 
Microsoft.Office.Interop.Visio.Document doc = application.Documents.Add(templatePath);
Microsoft.Office.Interop.Visio.Page page = application.Documents[1].Pages[1];  

How to Get the Width and Height of the Sheet You are Working On

double xPosition = page.PageSheet.get_CellsU("PageWidth").ResultIU;
double yPosition = page.PageSheet.get_CellsU("PageHeight").ResultIU; 

We are using this information about the sheet width and height to know where to place the boxes. We are putting the root boxes in the middle of the sheet by dividing the sheet width by the number of roots. Also we are subtracting from the yPosition the level number so that the boxes with increasing level number will get a lower position on the chart.

How to Create a Shape and Place it on the Chart (Drop it)

//creating the type of shape in the organizational chart it could be: "Position", 
//"Executive", "Manager",  "Assistant" and others according 
//to what you have in your stencil. 
Microsoft.Office.Interop.Visio.Master position = doc.Masters.get_ItemU("Position"); 
//placing the shape in the xPosition and yPosition coordinates
Microsoft.Office.Interop.Visio.Shape shape = page.Drop(position, xPosition, yPosition);  

How to Set the Shapes Properties

//set the text
shape.Text = box.Name;

//set hyperlink
if (!String.IsNullOrEmpty(box.HyperLink.Trim()))
{
     Hyperlink link = shape.Hyperlinks.Add();
     link.Address = box.HyperLink;
}

//set the shape width
shape.get_CellsSRC(
                (short)Microsoft.Office.Interop.Visio.VisSectionIndices.
                visSectionObject,
                (short)Microsoft.Office.Interop.Visio.VisRowIndices.
                visRowXFormIn,
                (short)Microsoft.Office.Interop.Visio.VisCellIndices.
                visXFormWidth).ResultIU = box.Width;

//set the shape height
shape.get_CellsSRC(
               (short)Microsoft.Office.Interop.Visio.VisSectionIndices.
               visSectionObject,
               (short)Microsoft.Office.Interop.Visio.VisRowIndices.
               visRowXFormIn,
               (short)Microsoft.Office.Interop.Visio.VisCellIndices.
               visXFormHeight).ResultIU = box.Height;

//set the shape fore color
shape.Characters.set_CharProps(
                (short)Microsoft.Office.Interop.Visio.
                    VisCellIndices.visCharacterColor,
                (short)Utilities.GetVisioColor(box.ForeColor));

//set the shape back color
shape.get_CellsSRC((short)VisSectionIndices.visSectionObject,
         (short)VisRowIndices.visRowFill, 
	(short)VisCellIndices.visFillForegnd).FormulaU = 
	"RGB(" + box.BackColor.R.ToString() + "," + box.BackColor.G.ToString() + "," 
	+ box.BackColor.B.ToString() + ")"; 
connectWithDynamicGlueAndConnector(shape, childShape);   

Connecting the shapes is done using the method connectWithDynamicGlueAndConnector(). This method accepts two parameters, the parent shape and the childShape and will create the connector between. This method is the exact one found in VISIO SDK.

Publishing

After creating and setting the Properties of the Shape, now it's time to connect the shapes.

After creating the VISIO file, you need to publish it. I am publishing it on web using VISIO Viewer. You can download VISIO Viewer 2007. The Visio 2007 Viewer allows anyone to view Visio drawings and diagrams (created with Visio 5.0, 2000, 2002, 2003, or 2007) inside their Microsoft Internet Explorer version 5.0 or later Web browser.

History

  • 16th September, 2010: Initial post

License

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

Share

About the Author

Ali Hamdar
Software Developer (Senior) Integrated Digital Systems - IDS
Lebanon Lebanon
Been programming since 2001 interested in finance, security, workflows, SharePoint and algorithms. He is an MCSD, MCDBA, MCAD, MCSD, (again), MCTS, MCPD and MCT.
My Blog: www.alihamdar.com

Comments and Discussions

 
QuestionHow can I set the color of connector Pin
PeterChu16-Jun-14 22:03
memberPeterChu16-Jun-14 22:03 
Questioni can't open the project Pin
Member 450856529-Jan-14 21:18
memberMember 450856529-Jan-14 21:18 
Questionget_CellsSRC function throws error in visual c++ windows forms application Pin
veerabhadraswamy31-Oct-13 1:09
memberveerabhadraswamy31-Oct-13 1:09 
QuestionHow to make one person have two fathers Pin
Armando_Adalid21-Jun-13 11:24
memberArmando_Adalid21-Jun-13 11:24 
Questionvisio installation required? Pin
rajkaty1-Jan-13 19:30
memberrajkaty1-Jan-13 19:30 
BugIssue with large Organigrams Pin
Pasand21-Aug-12 6:01
memberPasand21-Aug-12 6:01 
QuestionInsert Image into shape Pin
Dominik O.18-May-12 5:13
memberDominik O.18-May-12 5:13 
GeneralMy vote of 4 Pin
jmd0229-Feb-12 21:59
memberjmd0229-Feb-12 21:59 
QuestionHow to assign shape's property Pin
Member 34159517-Mar-11 17:55
memberMember 34159517-Mar-11 17:55 
GeneralDifferent Shape layout every box level ... help Pin
Member 36851613-Dec-10 0:54
memberMember 36851613-Dec-10 0:54 
GeneralRe: Different Shape layout every box level ... help Pin
ariegaon4-Dec-14 1:57
memberariegaon4-Dec-14 1:57 
GeneralPlease help Pin
EnDee32114-Oct-10 23:12
memberEnDee32114-Oct-10 23:12 
GeneralConvert Visio chart to image Pin
zSegundo17-Sep-10 6:33
memberzSegundo17-Sep-10 6:33 
GeneralRe: Convert Visio chart to image Pin
jcastellanosg17-Mar-11 8:34
memberjcastellanosg17-Mar-11 8:34 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 16 Sep 2010
Article Copyright 2010 by Ali Hamdar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid