组织结构图对于可视化公司内部团队、部门以及汇报关系的结构至关重要。它们帮助管理层和员工了解组织中角色之间的层级和关系。使用 C# 自动化创建这些图表,使开发人员能够从人力资源数据库、项目团队或企业目录中动态生成可视化内容,而无需在 Visio 或其他绘图工具中进行手动设计。
本教程说明如何使用图表库以编程方式 在 C# 中创建组织结构图。您将学习添加层级节点、使用连接器连接形状,并使用 CompactTree 算法自动布局图表,以获得整洁且专业的外观。
为什么要以编程方式创建组织结构图?
- 自动化人力资源工作流,在人员或组织结构变更时生成和更新图表。
- 在无需手动在 Visio 中绘制的情况下,生成清晰且标准化的层级图。
- 通过将图表生成集成到现有系统(如员工管理或报告仪表板),节省时间。
- 通过编程方式应用形状样式、连接器和布局规则,确保设计一致性。
- 将图表直接导出为 Visio (VSDX) 格式,以便共享和进一步编辑(如有需要)。
在 C# 中创建组织结构图
- 将图表库添加到您的 C# 项目中,以访问 Visio 创建和布局 API。
- 初始化一个新的
Diagram对象,并从 Visio 模板文件中加载基本形状和连接器。 - 定义表示组织关系的层次结构或数据集。
- 为层次结构中的每个节点动态添加形状,并存储其 ID 以进行连接映射。
- 使用连接器将子形状链接到其父节点,形成层次树。
- 应用自动布局算法(例如 CompactTree)来整齐地组织节点。
- 将生成的图表保存为 Visio VSDX 格式。
// 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);
此代码示例创建了组织层次结构,使用动态连接器连接父子节点,并应用自动的 CompactTree 布局以获得整洁的结构。每个形状代表一个组织节点,标签根据层次列表进行标记,最终输出保存为 Visio VSDX 文件。
结论
在 C# 中创建组织结构图提供了一种动态方式来可视化团队结构、汇报层级和部门关系,而无需手动在 Visio 中绘制。借助自动布局和连接管理,您可以直接从数据源生成清晰、易读的图表。上述方法简化了人力资源报告,提高了一致性,并实现了与业务自动化工作流的集成。
