How to build an animated Bar Chart Race in Tableau Software

Most of us have encountered Bar Chart Race animations by the time you read me. Can we build it in Tableau? I would say YES and NO, It can be done but using the page shelf which is not available on the web versions of our dashboards.

Let me talk first about who inspired me to do this. John Burn-Murdoch, I invite you to follow him on twitter, deserves an award for building and sharing THIS:

The animation is so smoothed, catchy, I want to stay in it forever and ever. It invites us to know more about the rise and fall of those cities. And we can definetely hear a speaker voice behind this Bar Chart Race (Hans Rosling style).

In Tableau, we can use the page shelf to fake animation, each new value in the page field being a “frame”.

The page shelf

And we can use Tableau to display points, lines and polygons wherever we want! (Once we crack the code 🙂 )

Sooooo… we have everything needed to fake one! (and build anything we want in Tableau, but this is an other debate)

Data

We will use a main dataset with the information we want to animate. In the example, I have four fields: link, year, category and value.

“data” sheet

We will use a second dataset with two fields to build each bars: link, point.

“bar” sheet

And, that’s the new thing, we will use a third dataset to fake each frame.

“frame” sheet

If you want to show a race by year and have monthly measures too, you can try an alternative and better version of this by skipping this third dataset and use the month and year field to animate the viz!

In Tableau, join this three datasets on link as a cartesian product.

Calculations

What we want to do now is to display each bar at the right position regarding its ranking by year, for each value in category. We also want the bar to “move” from the current position to a next one.

Let’s begin the calculations!

anim_inter : usefull later for moving values

[Anim]/{max([Anim])}

frame : usefull later for page shelf (if you have a real field, like month, it’s better!)

[Year]+[anim_inter]

value_current : value for the current year

{FIXED [Year],[Category]:
min([Value])
}

value_next : value of the next year

LOOKUP(sum([value_current]),1)

value_inter : value moving linearly from the current to the next year (if you have real values, it’s better!)

sum([value_current])+
([value_next]-sum([value_current]))*min([anim_inter])

rank_current : rank of the current year

RANK_UNIQUE(sum([value_current]),”desc”)

rank_next : rank of the next year

LOOKUP([rank_current],1)

rank_inter : rank moving linearly from the current to the next year

[rank_current]+
([rank_next]-[rank_current])*min([anim_inter])

@x_inter : moving lenght of the bar

IF min([Point])=1 THEN 0
ELSEIF min([Point])=2 THEN [value_inter]1.0
ELSEIF min([Point])=3 THEN [value_inter]1.0
ELSEIF min([Point])=4 THEN 0
END

@y_inter : moving position of the bar

IF min([Point])=1 THEN [rank_inter]1.0
ELSEIF min([Point])=2 THEN [rank_inter]1.0
ELSEIF min([Point])=3 THEN [rank_inter]+0.5
ELSEIF min([Point])=4 THEN [rank_inter]+0.5
END

filter : to filter my top 10 if available

rank_unique([value_inter],”desc”)

label : display labels on my polygons (you can display what you want/need)

IF min([Point])=1 THEN min([Category])
ELSEIF min([Point])=3 THEN str(int([value_inter]/1000000))+”M”
END

This last step is not perfect. Who am I to know if my values move linearly a year to an other? That’s why it’s better if you use real values instead of my value_inter.

Visualization

Now it is time to visualize. You have to use @x_inter and @y_inter inf columns and rows shelves, frame in Pages, filter in Filters.

You have to use the Polygon Mark and use year, category, anim_inter in details, and Point in Path.

We will use the @y_inter again as a dual axis as a point Mark to display my labels.

You won’t see the bars because we need to configure the table calculations.

@x_inter will be computed as Year, and frame.

@y_inter will be computed as Category for rank_current, by Category, Year and frame for rank_next and restarting every Category.

@filter will be computed as Category for filter, Year and frame for value_next.

And… TADAAAA, a Bar Chart Race in Tableau using the page shelf commands.

page shelf commands.

I updated my first dataset to show information about population by countries by year and whitout modifying anything else:

16 Comments

  1. Hi,

    I tried to replicate this visualisation with my own data, similar structure (Month, Country and Sales) as in your example but I have trouble with these calculations:
    1. rank next – I cant get it to return the actual rank for the following month. I tried all possible options but it either gets me the rank for the country row below or ranks a country by sales across all months, without considering rest of countries.
    2. @x_inter and @y_inter – i cant repeat adding 1.0 after [value_inter] – tableau treats it as error in the formula

    Can you help?

    Xenia

    Like

    1. Hey Xenia! If you have troubles with the calculation, I recommand you to go step by step 🙂 Using intermediate worksheet for example. About 2., I think this is a data type error, you have to make sure every value in other conditions are float, by multiplying by 1.0 for example!

      Hope it helps.

      Ludovic

      Like

      1. I tried to reproduce by steps but its confusing with actual data. Also couldnt understand the logic. Can you please explain the steps with reason in depth or point to an appropriate source to understand it.
        Thanks

        Like

      2. Hey Mayur! Sorry I was a bit short in time for this one. The main principle to get is that an animation is built thanks several pictures or frames. The main goal is to build those frames then. Each bars are moving so we need to calculate each positions, and use the “Pages” functionnality to animate it.

        Like

  2. Hi Ludovic,
    Amazing job! I nearly replicate this but I stuck with labels. I cant understand how you separate them(first with “Category” is over the bar, and second with “Value” is at the bar end) Could you give me any advice?

    Like

    1. Oh sorry Lukasz, it’s not clear in my blogpost. You have to use the filed “label”: display category on point 1 and value on point 3 🙂

      Like

    1. Bonjour Mohamed, de quelle calculs parles-tu ? Les techniques utilisées ici peuvent être assez complexes à comprendre et à reproduire pour des nouveaux utilisateurs Tableau 🙂

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s