Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#

Creating VISIO Organigrams using C#

Rate me:
Please Sign up or sign in to vote.
4.73/5 (12 votes)
16 Sep 2010CPOL3 min read 141.9K   11.6K   45   25
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.
C#
//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

C#
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

C#
double xPosition = page.PageSheet.get_CellsU("PageWidth").ResultIU;
C#
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)

C#
//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

C#
//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() + ")"; 
C#
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)


Written By
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

 
Praiseexcellent! Pin
Southmountain9-Oct-22 17:56
Southmountain9-Oct-22 17:56 
QuestionHow to change format Shape, Pin
Jorge Mtz25-Jun-19 8:51
Jorge Mtz25-Jun-19 8:51 
GeneralVery Unclear Pin
heszar22-Nov-18 21:12
heszar22-Nov-18 21:12 
QuestionAwesome work Pin
dmg00127-May-18 10:04
dmg00127-May-18 10:04 
QuestionHow to make one person have more than a parent ? Pin
Member 1197008213-Sep-15 22:44
Member 1197008213-Sep-15 22:44 
QuestionVisio object double click Pin
Member 1183862329-Jul-15 18:58
Member 1183862329-Jul-15 18:58 
QuestionHow can I set the color of connector Pin
PeterChu16-Jun-14 21:03
PeterChu16-Jun-14 21:03 
Questioni can't open the project Pin
Deya Aldeeb Khatib29-Jan-14 20:18
Deya Aldeeb Khatib29-Jan-14 20:18 
Questionget_CellsSRC function throws error in visual c++ windows forms application Pin
veerabhadraswamy31-Oct-13 0:09
veerabhadraswamy31-Oct-13 0:09 
QuestionHow to make one person have two fathers Pin
Armando_Adalid21-Jun-13 10:24
Armando_Adalid21-Jun-13 10:24 
QuestionRe: How to make one person have two fathers Pin
Member 1197008213-Sep-15 22:28
Member 1197008213-Sep-15 22:28 
Questionvisio installation required? Pin
rajkaty1-Jan-13 18:30
rajkaty1-Jan-13 18:30 
BugIssue with large Organigrams Pin
Pasand21-Aug-12 5:01
Pasand21-Aug-12 5:01 
QuestionInsert Image into shape Pin
Dominik O.18-May-12 4:13
Dominik O.18-May-12 4:13 
GeneralMy vote of 4 Pin
jmd0229-Feb-12 20:59
jmd0229-Feb-12 20:59 
QuestionHow to assign shape's property Pin
Member 34159517-Mar-11 16:55
Member 34159517-Mar-11 16:55 
GeneralDifferent Shape layout every box level ... help Pin
Malik Rosady2-Dec-10 23:54
Malik Rosady2-Dec-10 23:54 
GeneralRe: Different Shape layout every box level ... help Pin
ariegaon4-Dec-14 0:57
ariegaon4-Dec-14 0:57 
GeneralPlease help Pin
EnDee32114-Oct-10 22:12
EnDee32114-Oct-10 22:12 
GeneralConvert Visio chart to image Pin
zSegundo17-Sep-10 5:33
zSegundo17-Sep-10 5:33 
GeneralRe: Convert Visio chart to image Pin
jcastellanosg17-Mar-11 7:34
jcastellanosg17-Mar-11 7:34 
Use SaveAsWeb
Jorge Castellanos - Bogota - Colombia

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

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