Plotly javascript legend lables

I want to restrict number of characters in a legend and on hover I want to display the full text of legend.

enter image description here

I Have the above function to show tooltip on mouseover.
I’m trying to change legend text using

enter image description here

when I do console.log(element[0].firstChild.innerHTML), it gives me substring of legend but on graph it still shows original value.
Any help is appreciated! Thank you!