I am working on some code that is using dynamically generated graphs. And all of these graphs have legends at the y-axis. Now my goal is to check how long the longest legend-string is, and if the longest one is bigger than 20 characters, I only want to show the first characters of every string.
With the code below, I achieved that i can alert the desired shortened strings; but I do not know how to change the text now with these new strings.
var textLengthArray = [];
var labelStrings = domContainer.find(" g > .brm-y-direction > .tick > text");
labelStrings.each(function() {
textLengthArray.push($(this).text());
});
var longestString = textLengthArray.sort(function(a, b) {
return b.length - a.length;
})[0];
if (longestString.length >= 20) {
$("g.tick text").css("font-size", "9pt");
var offsetLeft = longestString.length * 3.7;
textLengthArray.map(function(sub) {
var subString = sub.substring(0, 6);
alert(subString);
});
};
I have tried something like:
$(labelStrings).replaceWith(subString)
With this, I had no legend at all, since I have replaced the whole text tag and all of its attributes with the new string.
So is there any way of not touching the tag and its values at all, but simply change the text between the opening and closing tag?
Thanks in advance!