Articles in this section
Category / Section

How to add data to the DataSource at runtime in Angular Diagram

2 mins read

This article explains how to add data to the DataSource at runtime in Angular Diagram. In Syncfusion® Angular 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:

  1. Use dataSourceSettings to bind the diagram with the initial data.
// Diagram Component with dataSourceSettings
<ejs-diagram
       #diagram
       id="diagram"
       [dataSourceSettings]="data">
</ejs-diagram>

// Datasource settings for the diagram
public data: Object = {
   id: 'Id',
   parentId: 'Manager',
   dataSource: new DataManager(localBindData),
}; 
  1. 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
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
   this.diagram.dataSourceSettings.dataSource.dataSource.json.push(data1);
   this.diagram.dataSourceSettings.dataSource.dataSource.json.push(data2);
   // Refresh the diagram to apply changes
   this.diagram.refresh();
} 

Refer to the working sample for additional details and implementation : sample

Conclusion

We hope you enjoyed learning about how to add data to the DataSource at runtime in Angular Diagram.

You can refer to our Angular 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 Angular 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!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied