How to change the font formatting of annotations at runtime in React Diagram
An Annotation is a text label that appears on a node or connector in a React diagram, allowing you to describe or label the object.
Annotation is used to textually represent an object with 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
<DiagramComponent id="container"
ref={(diagram) => (diagramInstance = diagram)}
width={'100%'} height={'600px'} nodes={node} connectors={connector}
/>
// Initialize node and connector
let node = [{
id: 'node1',
// Position of the node
offsetX: 300,
offsetY: 100,
// Size of the node
width: 100,
height: 100,
style: {
fill: '#6BA5D7',
strokeColor: 'white'
},
// Sets the annotation for the node
annotations: [{
content: 'Annotation Text',
// Sets the style for the text to be displayed
style: {
color: 'black',
fontSize: 12,
fontFamily: 'TimesNewRoman'
}
}]
}];
let 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 illustrates how to update the annotation font formats for a node and a connector, followed by updating the diagram to reflect the changes.
function App() {
const fontSizeRef = useRef(null);
const handleBold = () => {
diagramInstance.nodes[0].annotations[0].style.bold = true;
diagramInstance.connectors[0].annotations[0].style.bold = true;
diagramInstance.dataBind();
};
const handleItalic = () => {
diagramInstance.nodes[0].annotations[0].style.italic = true;
diagramInstance.connectors[0].annotations[0].style.italic = true;
diagramInstance.dataBind();
};
const handleUnderline = () => {
diagramInstance.nodes[0].annotations[0].style.textDecoration = 'Underline';
diagramInstance.connectors[0].annotations[0].style.textDecoration = 'Underline';
diagramInstance.dataBind();
};
const handleFontSizeChange = () => {
diagramInstance.nodes[0].annotations[0].style.fontSize = Number(fontSizeRef.current.value);
diagramInstance.connectors[0].annotations[0].style.fontSize = Number(fontSizeRef.current.value);
diagramInstance.dataBind();
};
const handleTextColorChange = (args) => {
diagramInstance.nodes[0].annotations[0].style.color = args.target.value;
diagramInstance.connectors[0].annotations[0].style.color = args.target.value;
diagramInstance.dataBind();
};
const handleBackgroundColorChange = (args) => {
diagramInstance.nodes[0].annotations[0].style.fill = args.target.value;
diagramInstance.connectors[0].annotations[0].style.fill = args.target.value;
diagramInstance.dataBind();
};
Conclusion
I hope you enjoyed about how to change the font formatting of annotations at runtime in React Diagram.
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, Direct-Trac, or feedback portal. We are always happy to assist you!