How to use HTML templates and CSS in the organization chart using the HTML node in JavaScript Diagram?
Using Syncfusion® JavaScript Diagram, you can create automatic layouts such as organizational charts that visually represent the structure of an organization and its relationships. In this blog, we will demonstrate how to create an organizational chart with HTML nodes, allowing for customized styling of node content.
To create an organizational chart, you’ll need to set the layout type as OrganizationalChart. The following code example demonstrates how to achieve this:
To create an automatic layout with html node:
To implement an automatic layout, define the layout property of the diagram and specify the desired layout type. Here’s a code snippet demonstrating this:
//Configrues organizational chart layout
layout: {
type: 'OrganizationalChart',
margin: { top: 20 },
horizontalSpacing: 70,
verticalSpacing: 80,
getLayoutInfo: function (node, tree) {
if (!tree.hasSubTree) {
tree.orientation = 'Vertical';
tree.type = 'Right';
}
},
},
To define the layout, it’s essential to set the data source using the dataSourceSettings property of the diagram. Here’s a code snippet demonstrating this:
//Sets the parent and child relationship of DataSource.
dataSourceSettings: {
id: 'Id',
parentId: 'ReportingPerson',
dataSource: new ej.data.DataManager(window.overviewData),
},
The nodeTemplate for the HTML node can be defined as shown below.
<script id="nodeTemplate" type="text/x-template">
<div style="width: 100%; height: 100%; background-color: ${data.color}; display: flex; align-items: center; justify-content: center;">
<div style="display: flex; flex-direction: column; align-items: center;">
<img src="${data.ImageUrl}" style="width: 40px; height: 40px; object-fit: cover; border-radius: 50%; margin-bottom: 30px;" alt="Profile">
<div style="color: black; font-size: 14px; margin-bottom: 30px; text-align: left; margin-top: 10px;">
<div style="font-weight: 500;">Name: ${data.Name}</div>
<div>Designation: ${data.Designation}</div>
</div>
</div>
</div>
</script>
The customization of styles based on the node data can be done inside the getNodeDefaults function of the diagram.
//Sets the default values of Nodes.
getNodeDefaults: function (obj, diagram) {
obj.shape = { type: 'HTML' };
let backgroundColor = '#DADAD8';
if (
obj.data.Designation === 'Managing Director' ||
obj.data.ReportingPerson === 'parent'
) {
backgroundColor = 'Lightblue';
} else if (
obj.data.ReportingPerson === 57 ||
obj.data.ReportingPerson === 29 ||
obj.data.ReportingPerson === 43 ||
obj.data.ReportingPerson === 89 ||
obj.data.ReportingPerson === 52 ||
obj.data.ReportingPerson === 68
) {
backgroundColor = 'lightgreen';
}
obj.data.color = backgroundColor;
obj.width = 200;
obj.height = 100;
return obj;
},
In the code example provided, the background color of the node is dynamically determined using conditional logic based on the node’s Designation and ReportingPerson data properties.
If the Designation is “Managing Director” or the ReportingPerson is “parent”, the background color is set to ‘Lightblue’. Alternatively, if the ReportingPerson matches specific identifiers, the background color is set to ‘lightgreen’. The template also includes an image representing the node’s profile, along with the name and designation displayed in a column layout.
Overall, the flexibility offered by the nodeTemplate function allows for effective customization of both the appearance and content of nodes to suit specific requirements.
Sample:
Conclusion
I hope you enjoyed learning about how to use HTML templates and CSS in the organization chart using the HTML node in JavaScript Diagram.
You can refer to our JavaScript Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Diagram example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!