So you build a horizontal bar chart showing some values over time...let's say by month. You start off building it with a discrete date, but you decide to show a trend line. Denied! You can't show a trend line with that discrete date. So you change it to a continuous date, you add your trend line without a problem, but then you realize the labels are not centered to the bar. And there is no way to center them. Argh!!!!!!! This blog post will help you solve this problem!
I shared this tip at TC25. It's such a pain and I'm happy to have a solution to it. To follow along, you can access and download the workbook here on Tableau Public.
Okay, so you've created a bar chart with a discrete date axis. The chart looks like the following:
All looks good. The labels are properly aligned to the center of the bars...looks great. But there are some problems with using a discrete date axis, one of which is that you cannot add a trend line. So if you need to add a trend line, what do you do? You change the date axis to continuous.
Now we add our trend line and all is good, right? Not exactly.
Check out the axis labels. They are centered to the beginning of the bar, not centered to the entire bar. This can be really confusing to end users. So let's click the Size card and see what our options are.
We could change the size to Manual and make it as big as possible. The labels seem to be centered, but the bars are tiny. That's not what we want.
Okay, so let's change it to Fixed. Since we are looking at months, I just entered a value of 25 (25 days) so that the bars would be evenly spaced. This looks good, but the labels are still offset. Wait! What's that option below the 25 that I entered? It appears to be related to alignment and is set to "Left". I'll just change that to be "Center" and we will be set! Wait...it's grayed out? Why the heck is it grayed out? Why can't I change it? Oh, the humanity!!!
Okay, here's the trick. You must use a continuous Exact Date in order for this to work. Since we are looking at this data by month, let's create the following calculation:
Month of Order Date
DATETRUNC('month', [Order Date])
This will truncate the order date to the order month. Now let's replace our current date with this date and set it to Exact Date and ensure it's continuous. When you do that, it should look like this...same as before with the left aligned label.
But! And it's a big but! Now when you click the Size card, that Alignment option is no longer grayed out (all because of the exact date). So change it from Left to Center and your labels are now centered to the bar.
And that's it! It's a problem that I've dealt with forever and it's actually amazing how simple the solution is. It has gotten me out of a number of binds. Again, you can download this workbook here on Tableau Public. We hope this blog post will help you out!
As always, thanks for reading and please reach out if you have any questions. Thanks!!!
───────────────────────
Need help with anything related to Tableau? Through Moxy Analytics, Ken and I provide consulting services such as Tableau Lifeline (get us for 1 hour to help solve a sticky problem), Fractional Data Hero (get us on your team for N number of hours a month for whatever you want us for), Tableau & Dataviz Training, and of course, project work. Click the Icon below if you are interested.
No comments: