How to Add Data to the DataSource at Runtime in React Diagram?
This article explains how to add data to the data source at runtime in React Diagram. In Syncfusion® React 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 sample implementation to achieve this.
- Use dataSourceSettings to bind the diagram with the initial data.
// Diagram Component with dataSourceSettings
<DiagramComponent
id="diagram"
ref={(diagram) => (diagramInstance = diagram)}
//configures data source settings
dataSourceSettings={{
id: 'Id',
parentId: 'Manager',
dataSource: new DataManager(localBindData),
}}
>
</DiagramComponent>
- 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
function addData() {
// 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
diagramInstance.dataSourceSettings.dataSource.dataSource.json.push(data1);
diagramInstance.dataSourceSettings.dataSource.dataSource.json.push(data2);
// Refresh the diagram to apply changes
diagramInstance.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 React Diagram.
You can refer to our React 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 React 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!