Tạo Lưu đồ Java

Lưu đồ có thể hữu ích để hiểu các thuật toán từng bước để thiết kế biểu diễn đồ họa của quy trình và quy trình công việc. Trong một số trường hợp, bạn có thể cần tạo sơ đồ để giải quyết vấn đề. Bài viết này trình bày cách tạo một sơ đồ lưu đồ theo lập trình trong Java.

Cài đặt Java API để tạo sơ đồ lưu đồ

Bạn có thể tạo sơ đồ lưu đồ bằng cách sử dụng API Aspose.Diagram for Java. Nó hỗ trợ tạo hoặc chỉnh sửa các tệp Visio trong VSD, VSDX và các định dạng được hỗ trợ khác. Bạn có thể dễ dàng cài đặt API bằng cách tải xuống tệp JAR từ phần Bản phát hành mới hoặc sử dụng các thông số kỹ thuật của Maven sau:

Kho:

 <repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <name>Aspose Java API</name>
        <url>http://repository.aspose.com/repo/</url>
    </repository>
</repositories>

Sự phụ thuộc:

 <dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-diagram</artifactId>
        <version>22.1</version>
        <classifier>jdk16</classifier>
    </dependency>
</dependencies>

Tạo lưu đồ có lập trình trong Java

Bạn có thể lập một sơ đồ để giải thích các bước của một trình tự. Bạn cần làm theo các bước dưới đây để tạo sơ đồ:

  1. Đầu tiên, tạo lược đồ để tạo sơ đồ.
  2. Thứ hai, tải một tệp VSS làm tệp chính để thêm hình dạng. bằng cách sử dụng lớp Sơ đồ.
  3. Thêm hình dạng và trình kết nối từ lược đồ.
  4. Đặt bố cục cho sơ đồ lưu đồ.
  5. Cuối cùng, ghi tệp đầu ra với lưu đồ ở định dạng VSDX bằng phương pháp Lưu.

Mẫu mã dưới đây trình bày chi tiết cách tạo một sơ đồ lưu đồ theo lập trình trong Java:

// Tạo một sơ đồ mới 
int pageNumber = 0;
String rectangleMaster = "Process", decisionMaster = "Decision", connectorMaster = "Dynamic connector"; 

Diagram diagram = new Diagram("XANFLOWCHARTNEW.vss"); 

double width = 1, height = 1, pinX = 4, pinY = 10; 
long process1 = diagram.addShape(pinX, pinY, width, height, rectangleMaster, 0); 
Shape processShape1 = diagram.getPages().getPage(pageNumber).getShapes().getShape(process1); 

processShape1.getText().getValue().add(new Txt("PROCESS")); 
processShape1.setName("PROCESS"); 
processShape1.getXForm().getLocPinX().getUfe().setF("Width*0.5"); 
processShape1.getXForm().getLocPinY().getUfe().setF("Height*0.5"); 

pinY = pinY - 2; 

long decision1 = diagram.addShape(pinX, pinY, width, height, decisionMaster, 0);
Shape decisionShape1 = diagram.getPages().getPage(pageNumber).getShapes().getShape(decision1);

decisionShape1.getText().getValue().add(new Txt("DECISION")); 
decisionShape1.setName("DECISION"); 
decisionShape1.getXForm().getLocPinX().getUfe().setF("Width*0.5"); 
decisionShape1.getXForm().getLocPinY().getUfe().setF("Height*0.5"); 

pinY = pinY - 2; 
long process2 = diagram.addShape(pinX, pinY, width, height, rectangleMaster, 0); 
Shape processShape2 = diagram.getPages().getPage(pageNumber).getShapes().getShape(process2);

processShape2.getText().getValue().add(new Txt("PROCESS")); 
processShape2.setName("PROCESS");
processShape2.getXForm().getLocPinX().getUfe().setF("Width*0.5");
processShape2.getXForm().getLocPinY().getUfe().setF("Height*0.5"); 

pinY = pinY - 2; 
long process3 = diagram.addShape(pinX, pinY, width, height, rectangleMaster, 0);
Shape processShape3 = diagram.getPages().getPage(pageNumber).getShapes().getShape(process3);

processShape3.getText().getValue().add(new Txt("PROCESS")); 
processShape3.setName("PROCESS");
processShape3.getXForm().getLocPinX().getUfe().setF("Width*0.5");
processShape3.getXForm().getLocPinY().getUfe().setF("Height*0.5"); 

pinY = pinY - 2; 
long process4 = diagram.addShape(pinX, pinY, width, height, rectangleMaster, 0);
Shape processShape4 = diagram.getPages().getPage(pageNumber).getShapes().getShape(process4);

processShape4.getText().getValue().add(new Txt("PROCESS")); 
processShape4.setName("PROCESS");
processShape4.getXForm().getLocPinX().getUfe().setF("Width*0.5");
processShape4.getXForm().getLocPinY().getUfe().setF("Height*0.5"); 

long connecterId = diagram.addShape(new Shape(), connectorMaster, 0);
diagram.getPages().getPage(pageNumber).connectShapesViaConnector(process1, ConnectionPointPlace.BOTTOM, 
        decision1, ConnectionPointPlace.TOP, connecterId); 

long connecterId1 = diagram.addShape(new Shape(), connectorMaster, 0);
diagram.getPages().getPage(pageNumber).connectShapesViaConnector(decision1, ConnectionPointPlace.BOTTOM,
        process2, ConnectionPointPlace.TOP, connecterId1); 

long connecterId2 = diagram.addShape(new Shape(), connectorMaster, 0);
diagram.getPages().getPage(pageNumber).connectShapesViaConnector(process2, ConnectionPointPlace.BOTTOM,
        process3, ConnectionPointPlace.TOP, connecterId2); 

long connecterId3 = diagram.addShape(new Shape(), connectorMaster, 0);
diagram.getPages().getPage(pageNumber).connectShapesViaConnector(process3, ConnectionPointPlace.BOTTOM,
        process4, ConnectionPointPlace.TOP, connecterId3); 

long connecterId4 = diagram.addShape(new Shape(), connectorMaster, 0);
diagram.getPages().getPage(pageNumber).connectShapesViaConnector(decision1, ConnectionPointPlace.RIGHT,
        process4, ConnectionPointPlace.TOP, connecterId4);

// Đặt các tùy chọn bố cục tự động
LayoutOptions layoutOptions = new LayoutOptions();

// Phương pháp 
layoutOptions.setLayoutStyle(LayoutStyle.FLOW_CHART);
layoutOptions.setDirection(LayoutDirection.BOTTOM_TO_TOP);
diagram.layout(layoutOptions);

DiagramSaveOptions options = new DiagramSaveOptions(SaveFileFormat.VSDX);
diagram.save("sample.vsdx", options);

Xem trước lưu đồ đầu ra mẫu

Trình tạo lưu đồ Java

Hơn nữa, bạn có thể muốn tải xuống các tệp đầu vào và đầu ra để kiểm tra tính năng này.

Nhận giấy phép API miễn phí

Bạn có thể đánh giá tất cả các tính năng của API mà không có bất kỳ giới hạn nào bằng cách yêu cầu giấy phép tạm thời miễn phí.

Sự kết luận

Tóm lại, bạn đã học được cách tạo hoặc lập lưu đồ bằng Java. Bạn có thể tùy chỉnh và thay đổi hướng hoặc hình dạng của sơ đồ theo yêu cầu của bạn. Vì vậy, bạn có thể dễ dàng nhúng tính năng này bằng cách sử dụng các lệnh gọi API từ bên trong ứng dụng Java của mình. Hơn nữa, bạn có thể truy cập tài liệu API để kiểm tra một số tính năng khác của API. Bạn có thể đặt bất kỳ câu hỏi nào của mình bằng cách liên hệ tại diễn đàn.

Xem thêm