![Create Organizational chart csharp.png C#](images/Create%20Organizational%20chart%20csharp.png#center)
An Organizational chart is used to depict the structure of a business, government or an organization. They can be disgned for different reasons like making a policy or planning purposes. You may create different type of organizational charts by making different shapes and connectors. This article covers how to create an Organizational chart in C#.
- Organizational Chart Creator API – C# Installation
- Create Organizational Chart in CompactTree Style in C#
- Create Organizational Chart in Flowchart Style in C#
Organizational Chart Maker – C# API Installation
Aspose.Diagram for .NET API can be used to create different Visio diagrams and charts. You can easily configure the API by downloading its reference file from the New Releases page or using the following NuGet installation command:
PM> Install-Package Aspose.Diagram
Create Organizational Chart in CompactTree Style in C#
You can create an organizational 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 Organizational 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 organizational chart generated with the code snippet above:
![Create Organizational chart C#](images/Create%20organizational%20CompactTree.png#center)
Create Organizational Chart in Flowchart Style in C#
You may need to create different kind of organizational charts that may follow different templates. Here you will learn how to create Organizational 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 Organizational 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 Organizational chart programmatically in C#. You can create a diagram from 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 the forum.