How to increase the node size when text exceeds the node size in React Diagram?
This article explains how to increase the node size when text exceeds the node size in React Diagram.
Annotations can be added to both nodes and connectors in a React Diagram. When the height and width of an annotation increase, the height and width of the associated node can also be adjusted accordingly. The following code sample demonstrates how the height and width of a node increase with the corresponding increase in the height and width of its annotation.
// Initialize the diagram component
<DiagramComponent id="diagram" ref={diagram => (diagramInstance = diagram)} width={"100%"} height={"499px"}
nodes={nodes} connectors={connectors} textEdit={textEdit}>
</DiagramComponent>
// to automatically increase the node size when text exceeds the node size
function textEdit() {
var nodes = diagramInstance.selectedItems.nodes[0];
setTimeout(() => {
let id = nodes.id;
let node = nodes;
let annotation = node.annotations[0].id;
let bounds = document
.getElementById(node.id + '_' + annotation)
.getBoundingClientRect();
if (bounds.height > node.height) {
node.height = bounds.height + 15;
diagramInstance.dataBind();
} else if (bounds.height < 50) {
node.height = 50;
diagramInstance.dataBind();
} else {
node.height = bounds.height + 15;
diagramInstance.dataBind();
}
if (bounds.width > node.width) {
node.width = bounds.width + 15;
diagramInstance.dataBind();
} else if (bounds.width < 50) {
node.width = 50;
diagramInstance.dataBind();
} else {
node.width = bounds.width + 15;
diagramInstance.dataBind();
}
}, 50);
}
The height and width of the annotation are obtained from the selected node. The bounds of the node’s annotation are stored in the variable “bounds”.
If the annotation’s height is greater than the node’s height, then the node’s height is assigned the value of the annotation’s height plus 15. If the annotation’s width is greater than the node’s width, then the node’s width is assigned the value of the annotation’s width plus 15. A value of 15 is added to the annotation’s height or width to prevent the annotation from becoming congested within the node.
Refer to the working sample for additional details and implementation:Sample
Conclusion
We hope you enjoyed learning about how to increase the node size when text exceeds the node size in React Diagram.
You can refer to our React Diagram feature tour page to learn about its other groundbreaking feature representations and documentation. 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 Diagram 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!