How to create and update annotation template dynamically in JavaScript Diagram?
In JavaScript Diagram, Annotation is a block of text that can be displayed over a node or connector. Annotation is used to textually represent an object with a string that can be edited at runtime. Diagram provides either an HTML or SVG template to 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 a annotation template for a node and connector, please refer to the code snippet below.
//Initializes diagram control
let diagram: Diagram;
diagram = new Diagram({
width: '100%', height: '645px',
nodes: nodes,
connectors:connections,
});
diagram.appendTo('#diagram');
//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:
document.getElementById('btn').onclick = function () {
diagram.connectors[0].annotations[0].template = '<div><input type="button" value="connector"></div>';
diagram.nodes[0].annotations[0].template = '<div><input type="button" value="Node"></div>';
diagram.dataBind();
}
This code dynamically changes the annotation templates for a node and a connector, followed by updating the diagram to reflect the changes.
For a live demonstration, please refer to the provided sample showcasing the use of annotation templates for nodes and connectors.
Conclusion
I hope you enjoyed learning about how to create and update annotation template dynamically in JavaScript Diagram.
You can refer to our JavaScript 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 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, Direct-Trac, or feedback portal. We are always happy to assist you!