Yes, the title of today’s blog was inspired by a song from one of the best movies ever (Cheetah Girls); no, I am not taking any questions on this subject at this time. Anyway, today, I’m going to be walking you through how to add a legend/labels to a line chart, like this:
Now, Tableau does have some options for you to choose from to add labels, and Line Ends is one of those options. Not only that, you can select the option to Match Mark Colors to have the text match the color of the line.
While these options are convenient, sometimes making the text match the mark colors doesn’t work out; if the mark was a lighter color, matching the text to that color would make the text harder to see. That’s why I’m going to show you how to add a shape next to the label for the line, which is a nice compromise that allows you to still have your color legend while keeping the label readable.
(Note: As always, whether you implement this feature in a dashboard will heavily depend on your end users. What I’m about to show you is probably better suited for one-off projects.)
The Process
Labeling the Line
0. Let’s use Superstore to set up the line chart first, to show Sales by Segment over time. Drag Order Date to Columns and select green/continuous Month. Then, put SUM(Sales) on Rows, and add Segment to Color on the Marks Card. Your line chart should now look like this, with the color legend that automatically pops up when you put something on Color on the left:
1. Now, let’s add in a label for the line. As mentioned above, you can add a label through the menu in the Marks Card, and that’s fine in a lot of cases, but to have a little more control over the formatting for this specific use case, let’s create a calculated field.
I’ve called mine LABEL: Segment and written the following IF/THEN statement:
IF LAST() = 0
THEN ATTR([Segment])
END
What does this mean? It’s basically saying, if the distance between the last point and the current point is 0 (i.e., there’s no distance between the last point and the current point because they are one and the same), then give us the corresponding Segment values, only at the end of the line (Segment is in the view, on Color, so we’ll get Consumer, Corporate, and Home Office returned to us). For all the other points, nothing will show.
Below, I’ve modified the calculated field slightly to show the distance between each point and the last point, while keeping the Segment label only on the last point of the line, which is where LAST() = 0. Because that last point is the only time the condition is true, the Segment labels show up, highlighted in yellow. For every other point, where LAST() does not equal 0, there is no label.
(Curious about how I modified the calculated field to show this, and want to try it yourself? Here it is, below:
IF LAST() = 0
THEN STR(LAST()) + ' ' + ATTR([Segment])
ELSE STR(LAST())
END
This calculated field says, if the distance between the last point and the current point is 0, then give us that distance, but as a string and not a number, as well as the corresponding Segment label. Otherwise, for all other points, where the distance is not equal to 0, just give us that distance, but as a string and not a number. They have to be strings and not numbers, as in the data type, because you’ll otherwise run into an error.)
Put the LABEL: Segment calculated field on Label in the Marks Card, and the labels will pop up at the end.
Adding in the Shapes
Now, it’s time to add in the little squares (or any other shapes) to the left of the label.
When you open up the label window, you’ll see the one calculated field we have there currently, which is <AGG(LABEL: Segment)>. Here is also where we can color our text, though we’re not going to, because we’re going to color in the shape instead.
Similar to earlier with the labels only showing up when LAST() = 0 and absent everywhere else, we’re going to create calculated fields for each Segment type and color them each the corresponding color for that type.
That means three different calculated fields, for each type of Segment, and each of those fields will hold a square that only appears for that Segment. Only one of those squares will ever appear at a time because we’re going to detail the exact conditions it can appear under in the calculated field.
Still a little confused? Let’s walk through creating one of these calculated fields, which will make it easy to duplicate and modify for the others.
1. Start with a new calculated field for the square that will appear with the Consumer label. I called mine SQUARE: Consumer.
IF LAST() = 0 AND [LABEL: Segment] = ‘Consumer’
THEN ‘■’
END
This calculated field says, if it’s the last point AND it’s the line labeled “Consumer,” then give us back a square (otherwise, for all the other points, show nothing); only when these two conditions are met will you see this square appear. You can copy and paste the square character and other shapes from the Character Map (search for it on your computer, if you have Windows–I think it’s called the Character Palette or Character Viewer for Macs).
2. Right-click the field to duplicate it twice, so that you can modify the calculated field to instead say “Corporate” or “Home Office” for both the title of the calculated field and the part that originally said [LABEL: Segment] = ‘Consumer’.
For easy reference, here’s what the three calculated fields should look like:
3. Now, drag all three of the calculated fields to Label on the Marks Card, which should now look like this:
The squares will pop up but in strange places, but that’s okay; we just haven’t positioned them correctly yet.
4. Click open the Label, then the … next to Text:, and the window for formatting the labels will pop up.
5. Time to format! In that window, bring your three square calculated fields to the front, and make sure there aren’t any spaces in between any of those calculated fields, as we don’t want trailing spaces to appear. The only space that should be present is the one between the cluster of squares and the actual Segment label.
Color each of the square calculated fields the corresponding color, and play around with the font sizes, so that the text looks more aligned with the square. For example, I used Tableau Medium, size 12 for the Segment label and the same font but size 18 for the squares.
It should look like this (space circled in blue):
Click OK, and you’re good to go!
…almost.
6. Now your line chart looks like this, which has the labels and color legend at the ends of the lines, but the marks all look like they’re on top of each other:
We need to give the labels a little more space to breathe, so what can we do?
Extend the axis!
Right-click on the Month of Order Date axis, and select Edit Axis. Change the range from being Automatic to Fixed, and change the Fixed End. This part, too, will involve some finagling until you figure out what looks best.
7. And finally, one more step. See how the lines and the labels don’t really line up nicely? Did you know you could reposition those labels just by clicking on and dragging them wherever you want?
There is, of course, still some cleaning up to do, with the tooltips, for instance, but I hope you found this tip useful! Of course, there’ll be times when you shouldn’t include legends like this, but I quite like this tip for one-offs or personal projects :)