本快速教程介绍了如何在 Java 中创建 ORG 图表。它解释了环境配置、逐步算法以及使用 Java 创建 组织图表制作工具 的示例代码。此外,可以根据您的需求进一步改进此示例代码以自定义 ORG chart。
用 Java 创建 ORG 图表的步骤
- 在您的环境中安装 Aspose.Diagram API 以创建 ORG 图表
- 使用 Diagram 类对象从现有模板中获取主形状
- 插入新形状并添加节点之间的连接
- 使用 LayoutOptions 类指定不同的图表属性并导出生成的 ORG 图表
这些步骤概述了使用 Java* 开发 *ORG 图表生成器。首先,准备系统环境并从现有模板文件访问主形状。接下来,在渲染创建的图表之前添加 ORG 图表的形状和连接。
用 Java 创建 ORG 图表制作器的代码
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"); | |
} | |
} |
此示例代码旨在使用 Java* 制作 *ORG 图表生成器。然而,您可以增强它,例如在 addShape 方法中更改矩形形状的数量、长度或宽度尺寸、位置、页码等。同样,您可以根据您的要求通过更改其父形状 ID 或连接点来更改不同节点之间的连接。
这个简短的教程涵盖了使用 Java* 创建 *ORG 图表制作器的详细信息。另外,如果你想画流程图,可以参考如何用Java创建流程图上的文章。