In Tableau text fields, such as text label or tooltips, there is no text wrap option. When a text label is long, we wish we can have it. Here we are going to show that we can make it happen.
In one project, we have limited space for text labels. That means, we have limits on both width and height. Given that the text label string can be very long, we need to wrap the text around. Since there is no such functionality in Tableau, we have to solve the problem by ourselves.
First, we need to define the width of the string, namely in number of characters. Second, define the number of rows. Assume each row can accommodate up to 80 characters and there are in total 3 rows. In total, the max number of characters is 240.
How to implement text wrap? Given a width limit, we will add line-breaks to a long string at a word that is at the 80 character limit or below. It is important to note that a line-break should be inserted between two words. It is a line-break and not a word-break.
The key steps are as follows, assuming the width limit is N characters. N can be a parameter.
1.Take the first N characters of a string using LEFT(String,N) function. Name the above substring Row1.
2.Find the position of the last space in the Row1. Cut off the chars beyond the last space from Row1.
4.Create Row3, etc.
5.Create a label
Row1+'\n'+Row2+'\n'+Row3
6.Apply this new string to the Label field.
Voila.
Note this method applies when we have a limited space for displaying a long text label.
Where is [Row Width in Chars] defined/created? Don't see it mentioned here but it is used.
ReplyDeleteIt is a parameter.
DeleteHi, can you please post your code in text file. I am having some challenges creating row2. Also, is there a way to correct blank lines when my text is less than N characters? Thanks
DeleteAll codes are in text. Please download the workbook and see the details.
DeleteIf second row is Null, it may self correct. Test yourself and see.
DeleteI don't see a ink to download the workbook. Does it sit on Tableau Public?
ReplyDeleteThe viz is at the bottom of the post. There is a download button there. Here is the link https://public.tableau.com/views/TextWrapinTableau/Dashboard1
DeleteCan you use this to wrap the text of a legend?
ReplyDeleteWhen necessary, use table to create a legend instead of the default one. Then you may apply the above technique.
Delete