สร้างแผนภูมิ ORG ใน Java

บทช่วยสอนฉบับย่อนี้ครอบคลุมถึงวิธี สร้างแผนภูมิ ORG ใน Java โดยจะอธิบายการกำหนดค่าสภาพแวดล้อม อัลกอริธึมแบบขั้นตอน และโค้ดตัวอย่างเพื่อสร้าง ตัวสร้างแผนภูมิองค์กรใน Java นอกจากนี้ โค้ดตัวอย่างนี้สามารถปรับเปลี่ยนเพิ่มเติมได้ชั่วคราวเพื่อปรับแต่ง ORG chart ตามความต้องการของคุณ

ขั้นตอนในการสร้างแผนภูมิ ORG ใน Java

  1. ติดตั้ง Aspose.Diagram API ในสภาพแวดล้อมของคุณเพื่อสร้างแผนภูมิ ORG
  2. ใช้วัตถุคลาส Diagram เพื่อรับรูปร่างหลักจากสเตนซิลที่มีอยู่
  3. แทรกรูปร่างใหม่และเพิ่มการเชื่อมต่อระหว่างโหนด
  4. ระบุคุณสมบัติแผนภูมิที่แตกต่างกันด้วยคลาส LayoutOptions และส่งออกแผนภูมิ ORG ที่สร้างขึ้น

ขั้นตอนเหล่านี้นำเสนอภาพรวมในการพัฒนา *ตัวสร้างแผนภูมิ ORG ใน Java ก่อนอื่น ให้เตรียมสภาพแวดล้อมของระบบและเข้าถึงรูปร่างหลักจากไฟล์สเตนซิลที่มีอยู่ ถัดไป เพิ่มรูปร่างและการเชื่อมต่อของแผนภูมิ ORG ก่อนที่จะเรนเดอร์ไดอะแกรมที่สร้างขึ้น

รหัสเพื่อสร้างแผนภูมิ ORG ใน 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");
}
}

โค้ดตัวอย่างนี้มีจุดมุ่งหมายเพื่อสร้าง ORG Chart Builder ใน Java ในขณะที่คุณสามารถเพิ่มได้ เช่น การเปลี่ยนจำนวนรูปร่างสี่เหลี่ยม ความยาว ความกว้าง ตำแหน่ง หมายเลขหน้า ฯลฯ ในเมธอด addShape ในทำนองเดียวกัน คุณสามารถเปลี่ยนการเชื่อมต่อระหว่างโหนดต่างๆ ได้โดยการเปลี่ยน ID รูปร่างหลักหรือจุดเชื่อมต่อตามความต้องการของคุณ

บทช่วยสอนสั้นๆ นี้ครอบคลุมรายละเอียดของการสร้าง ORG Chart Maker ใน Java นอกจากนี้ หากคุณต้องการวาดผังงาน โปรดดูบทความใน วิธีสร้างผังงานใน Java

 ไทย