How to create layout with HTML nodes at runtime in Angular Diagram?
Using Syncfusion® Angular Diagram, you can create automatic layouts that visually represent the structure of an organization and its relationships. In this knowledge base article, we will explain how to dynamically create layouts with HTML nodes using the event handling mechanisms provided by Syncfusion®. For instance, you can utilize the created event to dynamically render a layout.
To implement an automatic layout, define the layout property of the diagram and specify the desired layout type. To add an assistant to the organizational chart, use the getLayoutInfo function. Here’s a code snippet demonstrating this:
public created(): void {
//set the Layout properties
this.diagram.layout = {
type: 'OrganizationalChart',
getLayoutInfo: (node: Node, options: any) => {
if ((node.data as DataInfo)['Role'] === 'General Manager') {
options.assistants.push(options.children[0]);
options.children.splice(0, 1);
}
if (!options.hasSubTree) {
options.type = 'Right';
}
},
};
this.diagram.doLayout();
}
To render the layout, it’s essential to assign the JSON data to the dataSource property within the dataSourceSettings object of the diagram.
public created(): void {
this.diagram.dataSourceSettings = {
id: 'Id',
parentId: 'Manager',
//you need to set your data from db to dataManager
dataSource: new DataManager(this.localBindData),
doBinding: (nodeModel: NodeModel, data: object, diagram: Diagram) => {
nodeModel.shape = {
type: 'HTML',
};
},
};
}
We have created the layout using the HTML node. When creating HTML nodes dynamically, you define a nodeTemplate that specifies the structure and appearance of each node. Here’s an example of how you might define a nodeTemplate for HTML nodes:
<ng-template #nodeTemplate let-data>
<ng-container>
<div [ngStyle]="{ 'background': data.color, 'height': '100%', 'width': '100%', 'position': 'absolute' }">
<div class="details">
<p class="myTitle">{{ data.data.Role }}</p>
<p>Description</p>
<button
type="button"
id="nodeBtn_{{ data.data.Role }}"
(click)="nodeBtnClick()"
style="width:100px;margin-bottom: 30px;"
>
{{ data.data.Role }}
</button>
</div>
</div>
</ng-container>
</ng-template>