How to Add Data to the DataSource at Runtime in JavaScript Diagram?
This article explains how to add data to the data source at runtime in JavaScript Diagram. In Syncfusion® JavaScript Diagram, you can dynamically add new data to the data source at runtime and refresh the diagram to reflect the changes. This allows you to modify the diagram structure without reloading the entire diagram.
Below is an explanation and a sample implementation to achieve this:
- Use dataSourceSettings to bind the diagram to the initial data.
// Initializes the diagram
var diagram = new ej.diagrams.Diagram({
width: '100%', height: '700px',
// Configures data source settings
dataSourceSettings: {
id: 'Id', parentId: 'Manager',
dataSource: new ej.data.DataManager(window.localBindData),
},
});
diagram.appendTo('#diagram');
- Use the dataSourceSettings.dataSource object to dynamically add new data to the existing data source. After adding the new data, call diagram.refresh() to re-render the diagram with the updated data.
// Add new data to the data source on button click
document.getElementById('addData').onclick = function() {
// New data to be added
let data1 = {
Id: '22',
Role: 'HR Department',
Manager: '16',
color: '#2E95D8',
};
let data2 = {
Id: '23',
Role: 'Finance Department',
Manager: '10',
color: '#2E95D8',
};
// Push new data to the data source
diagram.dataSourceSettings.dataSource.dataSource.json.push(data1);
diagram.dataSourceSettings.dataSource.dataSource.json.push(data2);
// Refresh the diagram to apply changes
diagram.refresh();
};
Refer to the working sample for additional details and implementation: Click Here
Conclusion
We hope you enjoyed learning about how to add data to the DataSource at runtime in the JavaScript Diagram.
You can refer to our JavaScript Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, as well as 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!