How to use HTML templates and CSS in the organization chart using the HTML node in Vue Diagram?
Using Syncfusion® Vue 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:
layout: {
type: 'OrganizationalChart',
margin: { top: 20 },
horizontalSpacing: 70,
verticalSpacing: 80,
getLayoutInfo: (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 DataManager(data),
},
The nodeTemplate for the HTML node can be defined as shown below.
<template>
<div>
<div :style="{ width: '100%', height: '100%', backgroundColor: nodeBackgroundColor, display: 'flex', alignItems: 'center', justifyContent: 'center' }">
<div :style="{ display: 'flex', flexDirection: 'column', alignItems: 'center' }">
<img :src="data.data.ImageUrl" :style="{ width: '40px', height: '40px', objectFit: 'cover', borderRadius: '50%', marginBottom: '35px', marginTop: '-25px' }" alt="Profile" />
<div :style="{ color: 'black', fontSize: '14px', textAlign: 'left', marginTop: '10px' }">
<div style="font-weight: 500">Name: {{ data.data.Name }}</div>
<div>Designation: {{ data.data.Designation }}</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
components: {},
data() {
return {};
},
computed: {
nodeBackgroundColor() {
if (
this.data.data.Designation === 'Managing Director' ||
this.data.data.ReportingPerson === 'parent'
) {
return 'Lightblue';
} else if (
[57, 29, 43, 89, 52, 68].includes(this.data.data.ReportingPerson)
) {
return 'lightgreen';
} else {
return '#DADAD8';
}
},
},
};
</script>
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 Vue Diagram.
You can refer to our Vue 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 Vue 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!