Organizational charts visualize team structures, departments, and reporting lines. They help managers and employees see hierarchy and role relationships. Automating chart creation in C# lets developers generate visuals from HR databases, project teams, or corporate directories without manual design in Visio or other tools.
This tutorial explains how to create an organizational chart in C# programmatically using a diagramming library. You will add hierarchical nodes, connect shapes with connectors, and apply the CompactTree layout for a clean, professional look.
Why Create an Organizational Chart Programmatically?
- Automate HR workflows to generate and update charts when personnel or structure changes.
- Produce clear, standardized hierarchy diagrams without manual drawing in Visio.
- Save time by integrating chart generation into employee management or reporting dashboards.
- Ensure consistent design by applying shape styles, connectors, and layout rules in code.
- Export charts directly to Visio (VSDX) format for sharing and further editing.
Create Organizational Chart in C#
- Add the diagramming library to your C# project to access Visio creation and layout APIs.
- Initialize a new
Diagramobject and load basic shapes and connectors from a Visio stencil file. - Define a hierarchical structure or dataset representing the organizational relationships.
- Add shapes dynamically for each node in the hierarchy and store their IDs for connection mapping.
- Use connectors to link child shapes to their parent nodes, forming the hierarchical tree.
- Apply an automatic layout algorithm (such as CompactTree) to organize nodes neatly.
- Save the resulting diagram in Visio VSDX format.
// Load masters from any existing diagram, stencil or template
string visioStencil = dataDir + "BasicShapes.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
System.Collections.Hashtable shapeIdMap = new System.Collections.Hashtable();
// Create a new diagram
Aspose.Diagram.Diagram diagram = new Aspose.Diagram.Diagram(visioStencil);
diagram.Pages[pageNumber].PageSheet.PageProps.PageWidth.Value = 11;
// Add shapes for each hierarchy node
foreach (string orgnode in listPos)
{
long rectangleId = diagram.AddShape(pinX++, pinY++, width, height, rectangleMaster, pageNumber);
Aspose.Diagram.Shape shape = diagram.Pages[pageNumber].Shapes.GetShape(rectangleId);
shape.Text.Value.Add(new Aspose.Diagram.Txt(orgnode));
shape.Name = orgnode;
shapeIdMap.Add(orgnode, rectangleId);
}
// Create connections between parent and child 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];
Aspose.Diagram.Shape connector1 = new Aspose.Diagram.Shape();
long connecter1Id = diagram.AddShape(connector1, connectorMaster, pageNumber);
diagram.Pages[pageNumber].ConnectShapesViaConnector(parentId,
Aspose.Diagram.Manipulation.ConnectionPointPlace.Right,
shapeId, Aspose.Diagram.Manipulation.ConnectionPointPlace.Left,
connecter1Id);
}
}
// Auto layout CompactTree chart
Aspose.Diagram.AutoLayout.LayoutOptions compactTreeOptions = new Aspose.Diagram.AutoLayout.LayoutOptions
{
LayoutStyle = Aspose.Diagram.AutoLayout.LayoutStyle.CompactTree,
Direction = Aspose.Diagram.AutoLayout.LayoutDirection.DownThenRight,
EnlargePage = false
};
diagram.Pages[pageNumber].Layout(compactTreeOptions);
// Save diagram
diagram.Save(dataDir + "ORGChart_out.vsdx", Aspose.Diagram.SaveFileFormat.Vsdx);
The example builds an organizational hierarchy, connects parent and child nodes with dynamic connectors, and applies the CompactTree layout for a tidy structure. Each shape is labeled according to the hierarchy list, and the final diagram is saved as a Visio VSDX file.
Conclusion
Creating organizational charts in C# lets you visualize team structures, reporting lines, and departmental relationships without manual drawing. Automatic layout and connector management produce clean, readable charts directly from data sources. This approach simplifies HR reporting, ensures consistency, and integrates with business automation workflows.
