How to Dynamically Render Layout on Button Click in Javascript Diagram?
In Syncfusion® JavaScript Diagram, you can dynamically adjust the layout of a diagram at runtime. This guide provides a step-by-step approach to implementing a feature that allows users to render the layout of a diagram through a button click.
Steps to Implement Render Layout on Button Click:
- Initialize the Diagram: Create an empty Syncfusion® Diagram component.
- Include a Button for User Interaction: Add a button to your HTML that will trigger the loadLayout function.
- Create a Function to Render Layout: Develop a function that renders the layout configuration to the diagram.
- Bind the Button to the Function: Ensure the button’s click event is linked to your loadLayout function.
The following code demonstrates how to dynamically render a diagram layout when a button is clicked:
// Function to render the diagram layout
document.getElementById('load').onclick = function () {
// Configure data source settings for the diagram
diagram.dataSourceSettings = {
id: 'Id',
parentId: 'ReportingPerson',
dataSource: new ej.data.DataManager(radialTree),
// Binds the data with the nodes
doBinding: function (nodeModel, data) {
nodeModel.annotations = [
{
content: data.Name,
style: data.Id === 'parent' ? { color: 'white', fontSize: 30 }
: { color: 'black', fontSize: 13 },
},
];
// Enable tooltip for nodes using constraints
nodeModel.constraints = ej.diagrams.NodeConstraints.Default | ej.diagrams.NodeConstraints.Tooltip;
// Setup tooltip content and style
nodeModel.tooltip = {
content: data.Name + '<br/>' + data.Designation,
relativeMode: 'Object',
position: 'TopCenter',
showTipPointer: true,
};
// Conditional styling based on designation
if (data.Designation === 'Managing Director') {
nodeModel.width = 150;
nodeModel.height = 150;
nodeModel.shape = { shape: 'Ellipse' };
nodeModel.style = { fill: 'black' };
} else if (data.Designation === 'Project Manager') {
nodeModel.width = 60;
nodeModel.height = 60;
nodeModel.style = { fill: '#f8ab52' };
} else {
nodeModel.width = 60;
nodeModel.height = 60;
nodeModel.shape = { shape: 'Ellipse' };
nodeModel.style = { fill: '#afeeee' };
}
},
};
};
Refer to the working sample for additional details and implementation:Click here for sample
Conclusion
We hope you enjoyed learning about how to dynamically render a layout on a button click in JavaScript Diagram.
You can refer to our JavaScript Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with 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, BoldDesk Support, or feedback portal. We are always happy to assist you!