How to change the font formatting of annotations at runtime in JavaScript Diagram
This article explains how to change the font formatting of annotations at runtime in a JavaScript Diagram. An Annotation is a text label that appears on a node or connector in a JavaScript diagram, allowing you to describe or label the object. An annotation is used to textually represent an object with a string that can be edited at runtime. You can easily customize annotations by adjusting font properties such as bold, italic, underline, size, text color, and background color, enhancing the clarity and appearance of your diagram.
Below is a code example demonstrating how to set the initial font formatting for annotations on nodes and connectors.
// initialize Diagram component
var diagram = new ej.diagrams.Diagram({
width: '100%', height: '600px', nodes: [node] ,connectors:[connector]
}, '#element');
//initialize node and connector with annotations
var node = {
id:'node',
// Position of the node
offsetX: 300,
offsetY: 300,
// Size of the node
width: 100,
height: 100,
style: { fill: '#6BA5D7', strokeColor: 'white' },
annotations: [
{
content: 'Annotation',
style: {
color: 'blue',
fontFamily: 'TimesNewRoman',
fill: 'orange',
},
visibility:true
},
],
};
var connector = { id: "connector1",
type:'Straight',
sourcePoint: {x: 100,y: 100},
targetPoint: { x: 200,y: 200},
annotations: [
{
content: 'Annotation',
style: {
color: 'blue',
fontFamily: 'TimesNewRoman',
fill: 'orange',
},
visibility:true
},
],
}
The code snippet below shows how to dynamically update the annotation font formats for a node and a connector.
document.getElementById('Bold').onclick = () => {
diagram.nodes[0].annotations[0].style.bold = true;
diagram.connectors[0].annotations[0].style.bold = true;
};
document.getElementById('Italic').onclick = () => {
diagram.nodes[0].annotations[0].style.italic = true;
diagram.connectors[0].annotations[0].style.italic = true;
};
document.getElementById('textsize').onclick = (args) => {
diagram.nodes[0].annotations[0].style.fontSize = Number(args.target.value);
diagram.connectors[0].annotations[0].style.fontSize = Number(args.target.value);
}
document.getElementById('textcolor').onchange = (args) => {
diagram.nodes[0].annotations[0].style.color = args.target.value;
diagram.connectors[0].annotations[0].style.color = args.target.value;
diagram.dataBind();
};
document.getElementById('Underline').onclick = () => {
diagram.nodes[0].annotations[0].style.textDecoration = 'Underline';
diagram.connectors[0].annotations[0].style.textDecoration = 'Underline';
};
document.getElementById('bgcolor').onchange = (args) => {
diagram.nodes[0].annotations[0].style.fill = args.target.value;
diagram.connectors[0].annotations[0].style.fill = args.target.value;
diagram.dataBind();
};
Refer to the working sample for additional details and implementation: Sample
Conclusion
We hope you enjoyed learning about how to change the font formatting of annotations at runtime in JavaScript Diagram.
You can refer to our JavaScript Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, as well as how to quickly get started with 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, BoldDesk Support, or feedback portal. We are always happy to assist you!