Cách tạo biểu đồ ORG trong C#

Hướng dẫn này giải thích cách tạo biểu đồ ORG trong C#. Nó thảo luận về quy trình từng bước cấu hình hệ thống và một đoạn mã để phát triển Trình tạo biểu đồ tổ chức trong C#. Hơn nữa, đoạn mã này có thể được nâng cao để tạo ORG chart theo yêu cầu của bạn.

Các bước tạo biểu đồ ORG trong C#

  1. Định cấu hình API Aspose.Diagram trong hệ thống của bạn để tạo biểu đồ ORG
  2. Tải các hình dạng chính từ khuôn tô hiện có bằng lớp Diagram
  3. Thêm hình dạng mới và tạo kết nối giữa các nút
  4. Đặt thuộc tính biểu đồ bằng lớp LayoutOptions và lưu sơ đồ biểu đồ ORG đầu ra

Các bước này bao gồm thuật toán để tạo trình tạo biểu đồ ORG trong C#. Điều kiện tiên quyết là định cấu hình môi trường và tải các hình dạng chính từ khuôn mẫu hiện có. Sau đó, thêm các hình dạng và kết nối tương ứng trước khi lưu tệp sơ đồ đầu ra.

Mã để tạo biểu đồ ORG trong C#

using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args) // Create organization chart in C#
{
// Set the license
new Aspose.Diagram.License().SetLicense("License.lic");
// Load the masters
string visioStencil = "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 the hierarchy
List listPos = new List(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 for name and 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;
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
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 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("ORGChart_out.vsdx", Aspose.Diagram.SaveFileFormat.Vsdx);
Console.WriteLine("Done");
}
}

Đoạn mã này được thiết kế để tạo trình tạo biểu đồ ORG trong C#. Tuy nhiên, bạn có thể sửa đổi nó cho phù hợp với nhu cầu của mình như thay đổi số lượng hình chữ nhật, chiều cao, chiều rộng, vị trí, v.v. trong phương thức AddShape. Tương tự như vậy, bạn có thể thao tác các kết nối giữa các nút bằng cách sử dụng ID hình dạng gốc hoặc các điểm kết nối để đáp ứng yêu cầu của bạn.

Hướng dẫn cơ bản này đã trình bày chi tiết về cách tạo trình tạo biểu đồ ORG trong C#. Trong khi đó, nếu bạn muốn vẽ sơ đồ thì hãy đọc bài viết trên cách tạo sơ đồ trong C#.

 Tiếng Việt