Organizational charts are essential for visualizing the structure of teams, departments, and reporting relationships within a company. They help management and employees understand the hierarchy and relationships between roles in an organization. Automating the creation of these charts in C# allows developers to dynamically generate visuals from HR databases, project teams, or corporate directories without manual design effort in Visio or other diagramming tools.

This tutorial explains how to create an organizational chart in C# programmatically using a diagramming library. You will learn to add hierarchical nodes, connect shapes using connectors, and automatically layout the chart using the CompactTree algorithm for a clean and professional appearance.

Why Create an Organizational Chart Programmatically?

  • Automate HR workflows to generate and update charts when personnel or structure changes occur.
  • Produce clear and standardized hierarchy diagrams without needing to manually draw them in Visio.
  • Save time by integrating chart generation into existing systems such as employee management or reporting dashboards.
  • Ensure consistent design by programmatically applying shape styles, connectors, and layout rules.
  • Export charts directly to Visio (VSDX) format for sharing and further editing if needed.

Create Organizational Chart in C#

  1. Add the diagramming library to your C# project to access Visio creation and layout APIs.
  2. Initialize a new Diagram object and load basic shapes and connectors from a Visio stencil file.
  3. Define a hierarchical structure or dataset representing the organizational relationships.
  4. Add shapes dynamically for each node in the hierarchy and store their IDs for connection mapping.
  5. Use connectors to link child shapes to their parent nodes, forming the hierarchical tree.
  6. Apply an automatic layout algorithm (such as CompactTree) to organize nodes neatly.
  7. 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);

This code example creates an organizational hierarchy, connects parent and child nodes using dynamic connectors, and applies an automatic CompactTree layout for a clean structure. Each shape represents an organizational node labeled according to the hierarchy list, and the final output is saved as a Visio VSDX file.

Conclusion

Creating organizational charts in C# provides a dynamic way to visualize team structures, reporting hierarchies, and departmental relations without manually drawing in Visio. With automatic layout and connection management, you can generate clean, readable charts directly from data sources. The approach shown above simplifies HR reporting, improves consistency, and enables integration with business automation workflows.

See Also