Articles in this section
Category / Section

How to calculate the width of the node based on it's label in JavaScript Diagram?

1 min read

Calculate Node width based on annotation bounds

The textChange event in the JavaScript Diagram will get invoked when changes happened in the label. Using this event, calculate the size of the node based on the changes in the label.

Please refer to the code example and sample link as follows.

JS

function labelChanged(evt) { 
 if (!evt.element.segments) {
     var diagram = $("#diagram").ejDiagram("instance");
     var node = evt.element;
     var labelElement = document.getElementById(node.name+"_"+evt.label.name);
       var oldContent = labelElement.innerHTML;
       labelElement.innerHTML = evt.value;
       labelElement.parentNode.style.display = "block"
     var rect = labelElement.getBoundingClientRect();
       labelElement.innerHTML = oldContent;
       labelElement.parentNode.style.display = "none";
     
       diagram.updateNode(node.name, {width: rect.width});
     if(rect.height > node.height){
       diagram.updateNode(node.name, {height: rect.height});
     }
     
 }
}
 
$("#diagram").ejDiagram({
            width: "100%",
            height: "600px",
            nodes: nodes,
            //textChangeEvent
            textChange: labelChanged,
        },
        });
 

 

Sample link:


Conclusion

I hope you enjoyed learning about how to change the node width by using text change event 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied