Learn how to Create Company Organizational Chart in Python
Created
May 22, 2024 11:41
Create Company Organizational Chart in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This code sample shows how to create a company organizational chart in a CompactTree style | |
import aspose.diagram | |
from aspose.diagram import * | |
# Load masters from any existing diagram, stencil or template | |
visioStencil = "BasicShapes.vss" | |
rectangleMaster = "Rectangle" | |
connectorMaster = "Dynamic connector" | |
pageNumber = 0 | |
width = 1.0 | |
height = 1.0 | |
pinX = 4.25 | |
pinY = 9.5 | |
# Define values to construct the hierarchy | |
listPos = ["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 dictionary to map the string name to long shape id | |
shapeIdMap = {} | |
# Create a new diagram | |
diagram = Diagram(visioStencil) | |
diagram.pages[pageNumber].page_sheet.page_props.page_width.value = 11.0 | |
for orgnode in listPos: | |
# Add a new rectangle shape | |
rectangleId = diagram.add_shape(pinX, pinY, width, height, rectangleMaster, pageNumber) | |
pinX += 1 | |
pinY += 1 | |
# Set the new shape's properties | |
shape = diagram.pages[pageNumber].shapes.get_shape(rectangleId) | |
shape.text.value.add(Txt(orgnode)) | |
shape.name = orgnode | |
shapeIdMap[orgnode] = rectangleId | |
# Create connections between nodes | |
for orgName in listPos: | |
lastColon = orgName.rfind(':') | |
if lastColon > 0: | |
parentName = orgName[:lastColon] | |
shapeId = shapeIdMap[orgName] | |
parentId = shapeIdMap[parentName] | |
connector1 = Shape() | |
connecter1Id = diagram.add_shape(connector1, connectorMaster, pageNumber) | |
diagram.pages[pageNumber].connect_shapes_via_connector(parentId, manipulation.ConnectionPointPlace.RIGHT, | |
shapeId, manipulation.ConnectionPointPlace.LEFT, connecter1Id) | |
# Auto layout CompactTree chart | |
compactTreeOptions = autolayout.LayoutOptions() | |
compactTreeOptions.layout_style = autolayout.LayoutStyle.COMPACT_TREE | |
compactTreeOptions.direction = autolayout.LayoutDirection.DOWN_THEN_RIGHT | |
compactTreeOptions.enlarge_page = False | |
diagram.pages[pageNumber].layout(compactTreeOptions) | |
# Save diagram | |
diagram.save("CompactTreeChart_out.vsdx", SaveFileFormat.VSDX) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This code sample shows how to create a company organizational chart in a FlowChart style | |
import aspose.diagram | |
from aspose.diagram import * | |
# Load masters from any existing diagram, stencil or template | |
visioStencil = "BasicShapes.vss" | |
rectangleMaster = "Rectangle" | |
connectorMaster = "Dynamic connector" | |
pageNumber = 0 | |
width = 1.0 | |
height = 1.0 | |
pinX = 4.25 | |
pinY = 9.5 | |
# Define values to construct the hierarchy | |
listPos = ["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 dictionary to map the string name to long shape id | |
shapeIdMap = {} | |
# Create a new diagram | |
diagram = Diagram(visioStencil) | |
for orgnode in listPos: | |
# Add a new rectangle shape | |
rectangleId = diagram.add_shape(pinX, pinY, width, height, rectangleMaster, pageNumber) | |
pinX += 1 | |
pinY += 1 | |
# Set the new shape's properties | |
shape = diagram.pages[pageNumber].shapes.get_shape(rectangleId) | |
shape.text.value.add(Txt(orgnode)) | |
shape.name = orgnode | |
shapeIdMap[orgnode] = rectangleId | |
# Create connections between nodes | |
for orgName in listPos: | |
lastColon = orgName.rfind(':') | |
if lastColon > 0: | |
parentName = orgName[:lastColon] | |
shapeId = shapeIdMap[orgName] | |
parentId = shapeIdMap[parentName] | |
connector1 = Shape() | |
connecter1Id = diagram.add_shape(connector1, connectorMaster, pageNumber) | |
diagram.pages[pageNumber].connect_shapes_via_connector(parentId, manipulation.ConnectionPointPlace.RIGHT, | |
shapeId, manipulation.ConnectionPointPlace.LEFT, connecter1Id) | |
# Auto layout FlowChart | |
flowChartOptions = autolayout.LayoutOptions() | |
flowChartOptions.layout_style = autolayout.LayoutStyle.FLOW_CHART | |
flowChartOptions.direction = autolayout.LayoutDirection.TOP_TO_BOTTOM | |
flowChartOptions.enlarge_page = True | |
diagram.pages[pageNumber].layout(flowChartOptions) | |
# Save diagram | |
diagram.save("FlowChart_out.vsdx", SaveFileFormat.VSDX) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment