![Create ORG chart C#](images/Create%20ORG%20chart%20csharp.png#center)
An ORG chart is a visual diagram that represents the hierarchy of employees in an organization. It contains their roles, responsibilities, position, etc. and other relevant information. You can create different types of organizational charts with a few API calls without needing to manually draw shapes or connections. This article covers how to create an ORG chart in C#.
- ORG Chart Maker – C# API Installation
- Create ORG Chart in CompactTree Style in C#
- Create ORG Chart in Flowchart Style in C#
ORG Chart Maker – C# API Installation
Aspose.Diagram for .NET API supports working with different kinds of diagrams and charts. Simply access the API by downloading its DLL file from the New Releases section or with the following NuGet installation command:
PM> Install-Package Aspose.Diagram
Create ORG Chart in CompactTree Style in C#
You can create an ORG chart in CompactTree style by following the steps below:
- Load masters from any existing diagram, stencil or template.
- Define values to construct the hierarchy.
- Add shapes and connections between nodes.
- Save the output diagram.
The code snippet below explains how to create an ORG chart in C#:
// Load masters from any existing diagram, stencil or template | |
string visioStencil = "Basic Shapes.vss"; | |
const string rectangleMaster = "Rectangle"; | |
const string connectorMaster = "Dynamic connector"; | |
const int pageNumber = 0; | |
const double width = 1; | |
const double height = 1; | |
double pinX = 4.25; | |
double pinY = 9.5; | |
// Define values to construct the hierarchy | |
List<string> listPos = new List<string>(new string[] { "0", "0:0", "0:1", "0:2", "0:3", "0:4", "0:5", "0:6", "0:0:0", "0:0:1", "0:3:0", "0:3:1", "0:3:2", "0:6:0", "0:6:1" }); | |
// Define a Hashtable to map the string name to long shape id | |
Hashtable shapeIdMap = new Hashtable(); | |
// Create a new diagram | |
Diagram diagram = new Diagram(visioStencil); | |
diagram.Pages[pageNumber].PageSheet.PageProps.PageWidth.Value = 11; | |
foreach (string orgnode in listPos) | |
{ | |
// Add a new rectangle shape | |
long rectangleId = diagram.AddShape(pinX++, pinY++, width, height, rectangleMaster, pageNumber); | |
// Set the new shape's properties | |
Shape shape = diagram.Pages[pageNumber].Shapes.GetShape(rectangleId); | |
shape.Text.Value.Add(new Txt(orgnode)); | |
shape.Name = orgnode; | |
shapeIdMap.Add(orgnode, rectangleId); | |
} | |
// Create connections between nodes | |
foreach (string orgName in listPos) | |
{ | |
int lastColon = orgName.LastIndexOf(':'); | |
if(lastColon > 0) | |
{ | |
string parendName = orgName.Substring(0, lastColon); | |
long shapeId = (long)shapeIdMap[orgName]; | |
long parentId = (long)shapeIdMap[parendName]; | |
Shape connector1 = new Shape(); | |
long connecter1Id = diagram.AddShape(connector1, connectorMaster, pageNumber); | |
diagram.Pages[pageNumber].ConnectShapesViaConnector(parentId, ConnectionPointPlace.Right, | |
shapeId, ConnectionPointPlace.Left, connecter1Id); | |
} | |
} | |
//auto layout CompactTree chart | |
LayoutOptions compactTreeOptions = new LayoutOptions | |
{ | |
LayoutStyle = LayoutStyle.CompactTree, | |
Direction = LayoutDirection.DownThenRight, | |
EnlargePage = false | |
}; | |
diagram.Pages[pageNumber].Layout(compactTreeOptions); | |
// Save diagram | |
diagram.Save("CompactTreeChart_out.vsdx", SaveFileFormat.VSDX); |
The following screenshot shows the output ORG chart created using the above code snippet:
![Create ORG chart C#](images/Create%20ORG%20CompactTree.png#center)
Create ORG Chart in Flowchart Style in C#
You may need to create different kinds of ORG charts that may follow different templates. Here you will learn how to create an ORG chart in Flowchart style programmatically in C#:
- Load masters from any existing diagram, stencil or template.
- Add organization nodes and connectors.
- Set the layout and save output diagram.
The following sample code shows how to create an ORG chart in flowchart style in C#:
// Load masters from any existing diagram, stencil or template | |
string visioStencil = "Basic Shapes.vss"; | |
const string rectangleMaster = "Rectangle"; | |
const string connectorMaster = "Dynamic connector"; | |
const int pageNumber = 0; | |
const double width = 1; | |
const double height = 1; | |
double pinX = 4.25; | |
double pinY = 9.5; | |
// Define values to construct the hierarchy | |
List<string> listPos = new List<string>(new string[] { "0", "0:0", "0:1", "0:2", "0:3", "0:4", "0:5", "0:6", "0:0:0", "0:0:1", "0:3:0", "0:3:1", "0:3:2", "0:6:0", "0:6:1" }); | |
// Define a Hashtable to map the string name to long shape id | |
Hashtable shapeIdMap = new Hashtable(); | |
// Create a new diagram | |
Diagram diagram = new Diagram(visioStencil); | |
foreach (string orgnode in listPos) | |
{ | |
// Add a new rectangle shape | |
long rectangleId = diagram.AddShape(pinX++, pinY++, width, height, rectangleMaster, pageNumber); | |
// Set the new shape's properties | |
Shape shape = diagram.Pages[pageNumber].Shapes.GetShape(rectangleId); | |
shape.Text.Value.Add(new Txt(orgnode)); | |
shape.Name = orgnode; | |
shapeIdMap.Add(orgnode, rectangleId); | |
} | |
// Create connections between nodes | |
foreach (string orgName in listPos) | |
{ | |
int lastColon = orgName.LastIndexOf(':'); | |
if(lastColon > 0) | |
{ | |
string parendName = orgName.Substring(0, lastColon); | |
long shapeId = (long)shapeIdMap[orgName]; | |
long parentId = (long)shapeIdMap[parendName]; | |
Shape connector1 = new Shape(); | |
long connecter1Id = diagram.AddShape(connector1, connectorMaster, pageNumber); | |
diagram.Pages[pageNumber].ConnectShapesViaConnector(parentId, ConnectionPointPlace.Right, | |
shapeId, ConnectionPointPlace.Left, connecter1Id); | |
} | |
} | |
//auto layout FlowChart | |
LayoutOptions flowChartOptions = new LayoutOptions | |
{ | |
LayoutStyle = LayoutStyle.FlowChart, | |
Direction = LayoutDirection.TopToBottom, | |
EnlargePage = true | |
}; | |
diagram.Pages[pageNumber].Layout(flowChartOptions); | |
// Save diagram | |
diagram.Save("FlowChart_out.vsdx", SaveFileFormat.VSDX); |
Get Free Temporary License
You may request a free temporary license to evaluate the API in its full capacity.
Conclusion
In this article, you have understood how to create an ORG chart programmatically in C#. You can create a diagram from a stencil and create CompactTree or Flowchart style chart based on your requirements. Moreover, you may visit the documentation section to take a look at other features covered under different chapters and sections. In case of any inquiries, please feel free to contact us via forum.