How to create and update annotation template dynamically in React Diagram?
This article explains how to create and update annotation templates dynamically in React Diagram.
In React Diagram, an annotation is a block of text that can be displayed over a node or connector. An annotation is used to textually represent an object with a string that can be edited at runtime. The diagram provides either an HTML or SVG template for node and connector annotations. To do this, simply access the annotation template property and specify the desired template. The template should be in a string format. To see an example of how to set an annotation template for a node and connector.
please refer to the code snippet below:
//Initializes diagram control
let diagram: Diagram;
<diagramcomponent id="diagram" ref="{diagram" ==""> (diagramInstance = diagram)}
width={"100%"} height={"645px"}
nodes={nodes} connectors={connections} >
</diagramcomponent>
//initilaize the nodes and connectors
let nodes = [
{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
annotations:[ {
content: '|||',
template:
'<div><img src="https://www.freepnglogos.com/uploads/plus-icon/plus-icon-plus-math-icon-download-icons-9.png" height="30" weight="30"></div>',
},]
}, ]
let connectors= [
{
id: 'connector4',
sourcePoint: { x: 250, y: 400 },
targetPoint: { x: 400, y: 400 },
annotations: [
{
content: 'F',
offset: 1,
margin: { bottom: 20 },
template: '<div><input type="button" value="Submit"></div>',
},
{
offset: 0.5,
content: 'HTML Template',
margin: { top: 20 },
}
],
},
];
The provided code sets initial annotation templates for a node and a connector.
Additionally, annotation templates can be changed dynamically in response to user actions. The code snippet below illustrates how to update the annotation template for a node and a connector on a button click:
function add(){
diagramInstance.connectors[0].annotations[0].template =
'<div><input type="button" value="connector"></div>';
diagramInstance.nodes[0].annotations[0].template =
'<div><input type="button" value="Node"></div>';
}
This code dynamically changes the annotation templates for a node and a connector, followed by updating the diagram to reflect the changes.
Refer to the working sample for additional details and implementation: sample
Conclusion
We hope you enjoyed learning about how to create and update annotation templates dynamically in React Diagram.
You can refer to our React Diagram feature tour page to know about its other groundbreaking feature representations and documentations. You can also explore our React Diagram example to understand how to present 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 React Spreadsheet and other components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!