This quick tutorial covers how to create ORG chart in Java. It explains the environment configuration, stepwise algorithm, and a sample code to create an Organizational chart maker in Java. Moreover, this sample code can be further improvised to customize the ORG chart based on your needs.
Steps to Create ORG Chart in Java
- Install Aspose.Diagram API in your environment to create an ORG chart
- Use the Diagram class object to get the master shapes from the existing stencil
- Insert new shapes and add connections between the nodes
- Specify different chart properties with the LayoutOptions class and export the generated ORG chart
These steps present an overview to develop an ORG chart generator in Java. First of all, prepare the system environment and access the master shapes from an existing stencil file. Next, add the shapes and connections of the ORG chart before rendering the created diagram.
Code to Create ORG Chart Maker in 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"); | |
} | |
} |
This sample code is intended to make ORG chart builder in Java. Whereas, you can enhance it like changing the number of rectangular shapes, length, or width dimensions, position, page number etc. in the addShape method. Similarly, you can change the connections between different nodes by changing their parent shape ID or connection points as per your requirements.
This brief tutorial has covered the details of creating an ORG chart maker in Java. Besides, if you want to draw a flowchart then refer to the article on how to create flowchart in Java.