How to update diagram json before loading a Diagram in React?
This article explains how to update diagram JSON before loading a Diagram in React. To update the diagram JSON before loading a React Diagram, please follow the steps below:
- After rendering the diagram, save the initial diagram data in a separate variable.
index.html
<input type="button" id="Save" value="Save">
index.js
let savedata;
document.getElementById('Save').onclick = function () {
savedata = diagramInstance.saveDiagram();
};
- After saving the diagram data, parse the saved data to convert it into an object. This allows you to manipulate the nodes and connectors in the diagram. For example, you can change the fill property for nodes.
index.html
<input type="button" id="Load" value="Load"/>
index.js
document.getElementById('Load').onclick = function () {
var diagramData = JSON.parse(savedata);
for (let i = 0; i < diagramData.nodes.length; i++) {
diagramData.nodes[i].style.fill = 'red';
}
};
- Once you’ve made the necessary changes to the diagram data, stringify the modified data and pass it to the loadDiagram method. This method will load the updated diagram from the serialized string data.
document.getElementById('Load').onclick = function () {
var diagramData = JSON.parse(savedata);
for (let i = 0; i < diagramData.nodes.length; i++) {
diagramData.nodes[i].style.fill = 'red';
}
diagramInstance.loadDiagram(JSON.stringify(diagramData));
};
Refer to the working sample for additional details and implementation: Sample
Conclusion
We hope you enjoyed learning about how to update diagram JSON before loading a diagram in React.
You can refer to our React 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 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!