图表控件Aspose.Diagram入门教程(6):如何在 C# 中以编程方式创建 ORG 图表

本文介绍如何在 C# 中创建 ORG 图表。欢迎查阅~

ORG 图是一个可视化图表,表示组织中员工的层次结构。它包含他们的角色、职责、职位等和其他相关信息。您可以通过几个 API 调用创建不同类型的组织结构图,而无需手动绘制形状或连接。本文介绍如何在 C# 中创建 ORG 图表。

图表控件Aspose.Diagram入门教程(6):如何在 C# 中以编程方式创建 ORG 图表

(一) ORG Chart Maker – C# API 安装

Aspose.Diagram for .NET API 支持使用不同类型的图表。只需从“新版本”部分下载其 DLL 文件或使用以下NuGet安装命令即可访问 API:

PM> Install-Package Aspose.Diagram
(二) 在 C# 中以 CompactTree 样式创建 ORG 图表

您可以按照以下步骤创建 CompactTree 样式的 ORG 图表:

  1. 从任何现有图表、模板或模板加载母版。
  2. 定义值以构建层次结构。
  3. 在节点之间添加形状和连接。
  4. 保存输出图。

下面的代码片段解释了如何在 C# 中创建 ORG 图表:

// Load masters from any existing diagram, stencil or templatestring 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 hierarchyList<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 idHashtable shapeIdMap = new Hashtable();// Create a new diagramDiagram diagram = new Diagram(visioStencil);diagram.Pages[pageNumber].PageSheet.PageProps.PageWidth.Value = 11;foreach (string orgnode in listPos){// Add a new rectangle shapelong rectangleId = diagram.AddShape(pinX++, pinY++, width, height, rectangleMaster, pageNumber);// Set the new shape's propertiesShape shape = diagram.Pages[pageNumber].Shapes.GetShape(rectangleId);shape.Text.Value.Add(new Txt(orgnode));shape.Name = orgnode;shapeIdMap.Add(orgnode, rectangleId);}// Create connections between nodesforeach (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 chartLayoutOptions compactTreeOptions = new LayoutOptions{LayoutStyle = LayoutStyle.CompactTree,Direction = LayoutDirection.DownThenRight,EnlargePage = false};diagram.Pages[pageNumber].Layout(compactTreeOptions);// Save diagramdiagram.Save("CompactTreeChart_out.vsdx", SaveFileFormat.VSDX);

以下屏幕截图显示了使用上述代码片段创建的输出 ORG 图表:

图表控件Aspose.Diagram入门教程(6):如何在 C# 中以编程方式创建 ORG 图表
(三) 在 C# 中以流程图样式创建 ORG 图表

您可能需要创建可能遵循不同模板的不同类型的 ORG 图表。在这里,您将学习如何在 C# 中以编程方式创建流程图样式的 ORG 图表:

  1. 从任何现有图表、模板或模板加载母版。
  2. 添加组织节点和连接器。
  3. 设置布局并保存输出图。

以下示例代码显示了如何在 C# 中以流程图样式创建 ORG 图表:

// Load masters from any existing diagram, stencil or templatestring 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 hierarchyList<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 idHashtable shapeIdMap = new Hashtable();// Create a new diagramDiagram diagram = new Diagram(visioStencil);foreach (string orgnode in listPos){// Add a new rectangle shapelong rectangleId = diagram.AddShape(pinX++, pinY++, width, height, rectangleMaster, pageNumber);// Set the new shape's propertiesShape shape = diagram.Pages[pageNumber].Shapes.GetShape(rectangleId);shape.Text.Value.Add(new Txt(orgnode));shape.Name = orgnode;shapeIdMap.Add(orgnode, rectangleId);}// Create connections between nodesforeach (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 FlowChartLayoutOptions flowChartOptions = new LayoutOptions{LayoutStyle = LayoutStyle.FlowChart,Direction = LayoutDirection.TopToBottom,EnlargePage = true};diagram.Pages[pageNumber].Layout(flowChartOptions);// Save diagramdiagram.Save("FlowChart_out.vsdx", SaveFileFormat.VSDX);

以上便是如何在 C# 中以编程方式创建 ORG 图表,如您还有产品相关的问题也可以继续流量此系列其他文章,或咨询我们客服获取帮助!


欢迎下载|体验更多Aspose产品 

获取更多厂商信息 或 加入Aspose技术交流群(
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年8月6日
下一篇 2022年8月6日

相关推荐

发表回复

登录后才能评论