Hướng dẫn nhanh này trình bày cách tạo biểu đồ ORG trong Java. Nó giải thích cấu hình môi trường, thuật toán từng bước và mã mẫu để tạo Trình tạo biểu đồ tổ chức trong Java. Hơn nữa, mã mẫu này có thể được tùy chỉnh thêm để tùy chỉnh ORG chart dựa trên nhu cầu của bạn.
Các bước tạo biểu đồ ORG trong Java
- Cài đặt API Aspose.Diagram trong môi trường của bạn để tạo biểu đồ ORG
- Sử dụng đối tượng lớp Diagram để lấy các hình dạng chính từ khuôn mẫu hiện có
- Chèn hình dạng mới và thêm kết nối giữa các nút
- Chỉ định các thuộc tính biểu đồ khác nhau bằng lớp LayoutOptions và xuất biểu đồ ORG đã tạo
Các bước này trình bày tổng quan để phát triển trình tạo biểu đồ ORG trong Java. Trước hết, hãy chuẩn bị môi trường hệ thống và truy cập các hình dạng chính từ tệp stencil hiện có. Tiếp theo, thêm các hình dạng và kết nối của biểu đồ ORG trước khi hiển thị sơ đồ đã tạo.
Mã để tạo biểu đồ ORG trong Java
import com.aspose.diagram.*; | |
import java.util.Arrays; | |
import java.util.Hashtable; | |
import java.util.List; | |
public class Main | |
{ | |
public static void main(String[] args) throws Exception // Create OR chart in Java | |
{ | |
// Set the licenses | |
new License().setLicense("License.lic"); | |
// Load masters from any existing diagram | |
String visioStencil = "BasicShapes.vss"; | |
String rectangleMaster = "Rectangle"; | |
String connectorMaster = "Dynamic connector"; | |
int pageNumber = 0; | |
double width = 1; | |
double height = 1; | |
double pinX = 4.25; | |
double pinY = 9.5; | |
// Define values to construct the hierarchy | |
List<String> listPos = Arrays.asList(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 name to id | |
Hashtable shapeIdMap = new Hashtable(); | |
// Create a new diagram | |
com.aspose.diagram.Diagram diagram = new com.aspose.diagram.Diagram(visioStencil); | |
diagram.getPages().get(pageNumber).getPageSheet().getPageProps().getPageWidth().setValue(11); | |
for (String orgnode : listPos) | |
{ | |
// Add a new rectangle shape | |
long rectangleId = diagram.addShape(pinX++, pinY++, width, height, rectangleMaster, pageNumber); | |
// Set the new shape's properties | |
com.aspose.diagram.Shape shape = diagram.getPages().get(pageNumber).getShapes().getShape(rectangleId); | |
shape.getText().getValue().add(new com.aspose.diagram.Txt(orgnode)); | |
shape.setName(orgnode); | |
shapeIdMap.put(orgnode, rectangleId); | |
} | |
// Create connections between nodes | |
for (String orgName : listPos) | |
{ | |
int lastColon = orgName.lastIndexOf(':'); | |
if(lastColon > 0) | |
{ | |
String parendName = orgName.substring(0, lastColon); | |
long shapeId = (long)shapeIdMap.get(orgName); | |
long parentId = (long)shapeIdMap.get(parendName); | |
com.aspose.diagram.Shape connector1 = new com.aspose.diagram.Shape(); | |
long connecter1Id = diagram.addShape(connector1, connectorMaster, pageNumber); | |
diagram.getPages().get(pageNumber).connectShapesViaConnector(parentId, | |
com.aspose.diagram.ConnectionPointPlace.RIGHT, | |
shapeId, com.aspose.diagram.ConnectionPointPlace.LEFT, connecter1Id); | |
} | |
} | |
// Auto layout CompactTree chart | |
com.aspose.diagram.LayoutOptions compactTreeOptions = new com.aspose.diagram.LayoutOptions(); | |
compactTreeOptions.setLayoutStyle(com.aspose.diagram.LayoutStyle.COMPACT_TREE); | |
compactTreeOptions.setDirection(com.aspose.diagram.LayoutDirection.DOWN_THEN_RIGHT); | |
compactTreeOptions.setEnlargePage(false); | |
diagram.getPages().get(pageNumber).layout(compactTreeOptions); | |
// Save diagram | |
diagram.save("ORGchart_java.vsdx", com.aspose.diagram.SaveFileFormat.VSDX); | |
System.out.println("Done"); | |
} | |
} |
Mã mẫu này nhằm mục đích tạo trình tạo biểu đồ ORG trong Java. Trong khi đó, bạn có thể nâng cao nó như thay đổi số lượng hình chữ nhật, kích thước chiều dài hoặc chiều rộng, vị trí, số trang, v.v. trong phương thức addShape. Tương tự, bạn có thể thay đổi kết nối giữa các nút khác nhau bằng cách thay đổi ID hình dạng gốc hoặc điểm kết nối theo yêu cầu của bạn.
Hướng dẫn ngắn gọn này đã trình bày chi tiết về cách tạo trình tạo biểu đồ ORG trong Java. Ngoài ra, nếu bạn muốn vẽ sơ đồ thì tham khảo bài viết trên cách tạo sơ đồ trong Java.