This week, I learned how to create a pattern bar chart (also known as textured bar chart or pattern-filled bar chart). My colleague Emma and I were looking into options for a Christmas visualization, and we eventually opted for something like this:
Tableau doesn’t have a built-in option for filling bars with patterns, but the lively Tableau community came up with a brilliant workaround. This time, my special thanks go to Tableau Zen Master Toan Hoang for his blog post on this topic.
In this blog post, I will draw heavily on Toan’s post, but I will go more in depth as to the why behind each step of the process.
This technique leverages data densification. You don’t necessarily need to know what data densification is for the sake of this trick, but if you are as curious as I am, definitely check out Ken Flerlage’s blog post for a good introduction to the topic.
What you will need
In order to create a pattern bar chart, you will need a dataset with at least two discrete and one continuous field. Toan uses Superstore in his example, which is a great way to start as the database has several discrete fields to choose from.
However, if your dataset is missing a discrete field, there is a workaround. In fact, my dataset (which I pulled from this article) contained just one discrete field. All I did was add a “Type” field” and give it a different value for each row:
Category | Yes, buy a real tree | Don’t decorate/celebrate Christmas | Yes, buy an artificial tree | No, reuse an artificial tree | Type |
All adults | 0.17 | 0.16 | 0.12 | 0.54 | all |
Gen Z | 0.19 | 0.14 | 0.2 | 0.48 | z |
Millennials | 0.23 | 0.1 | 0.23 | 0.45 | m |
Gen X | 0.21 | 0.21 | 0.12 | 0.46 | x |
Baby boomers | 0.12 | 0.18 | 0.03 | 0.67 | b |
Prepare the dataset
Path
Pick a discrete field to create a path field. Do not pick the one you will want displayed in the final viz.
Toan’s example: If [Sub-Category] = “Accessories” THEN 0 ELSE 100 END
My example: if [Type] = “all” then 0 else 100 end
Path (bin)
Right-click on Path and create a bin. The size of the bins will affect the height of the bars. You can start off with bin size: 5, and then play around with it at the end to find the size that suits your viz best.
Index
This calc uses the Index() function and subtracts 1 to make the index start at 0: Index() -1
Now, create a series of Table calculations. Start off by performing two window_sum functions of the continuous field you wish to represent in the viz. For Toan, that was [Sales], for me [Yes, buy a real tree]
TC_Sales / TC_Real Tree
Start off by creating a window_sum function of the continuous field you wish to represent in the viz. For Toan, that was [Sales], for me [Yes, buy a real tree]
Toan’s example: WINDOW_SUM(SUM([Sales]))
My example: WINDOW_SUM(SUM( [Yes, buy a real tree]))
TC_Total Sales / TC_Total Real Tree
Create another field with the same calculation
Toan’s example: WINDOW_SUM(SUM([Sales]))
My example: WINDOW_SUM(SUM( [Yes, buy a real tree]))
TC_Percentage
Create a percentage field…
Toan’s example: [TC_Sales]/[TC_Total Sales]
My example: TC_Real Tree/TC_Total Real Tree
TC_Percentage (Adjusted)
…and the an adjusted percentage field
[TC_Percentage]/WINDOW_MAX([TC_Percentage])
TC_Shape
Here, insert the discrete field you want to display in the view.
Toan’s example: IF [Index]/WINDOW_MAX([Index]) <= [TC_Percentage (Adjusted)] THEN WINDOW_MAX(MAX([Region])) ELSE NULL END
My example: IF [Index]/WINDOW_MAX([Index]) <= [TC_Percentage (Adjusted)] THEN WINDOW_MAX(MAX([Category])) ELSE NULL END
Set up the view
Now, here is where we begin to build our pattern bar chart.
Path (bin) to detail (Show Missing Values)
Table calculations are at the core of pattern bar charts. As you may know, Table calculations can be computed using a certain direction (e.g. Table (across), cell) or a certain field, as long as this field is present in the view.
So, what we want to do now is to add Path (bin) to detail so that we can later compute our table calculations using Path (bin). Before adding it to detail, we want to make sure that all the bins are being considered. In fact, since the Path calculation is: if [Type] = “all” then 0 else 100 end, only two of the bins are filled. However, we want all the bins in between to also be considered. So, let’s drag Path (bin) to rows, left click on it and select “Show Missing Values”:
Discrete field on the columns shelf
Drag the discrete field you want in the view (e.g. Region for Toan, Category for me) to columns.
Index on the rows shelf
Drag Index to rows and (here is where we start using our Path (bin) field!) right click on the Index pill and select Compute using → Path (bin)
Shape Mark type and TC_Shape
Change the mark type to: Shape and drag to TC_Shape field onto the Shape mark type. Once again, right-click on the TC_Shape pill and select Compute using → Path (bin)
Edit Table Calculation
Now, for our second-to-last step, we will need to edit out table calculations, specifically the nested calculations within them. Right click on the TC_Shape pill on the Shape mark type and select Edit Table Calculations:
- under Nested Calculations choose TC_Total Sales / TC_Total Real Tree
- Choose Compute using → Specific Dimensions
- Make sure that both Path (bin) and Category are selected in this order.
Now on the same tab
- click on the Nested Calculations drop-down and this time pick TC_percentage (Adjusted).
- Again, choose Compute using → Specific Dimensions and Make sure that both Path (bin) and Category are selected in this order.
Pattern Bar chart
If all goes well, you should see something along these lines:
What is going on here? As you might notice, each category has a different shape, except for the circles at the top of the bar. The different shapes represent the actual value of that category, whereas the circles at the top are there to fill up the bar as much as the category with the highest value.
But what if we substituted the circles at the top with a transparent shape?
And the different shapes with an image of choice?
Well, we would achieve a pattern bar chart!
Congrats for making it so far! If you are not sure how to add custom shapes to you Tableau Desktop repository, check out this post.
Good luck with your creations!
Do you want to learn more about Tableau or Alteryx? Do you want to use these tools to their full potential? Check out our trainings and consultancy services!