Thursday, January 2, 2020

Creating Radial Stacked Bar Chart in Tableau

This is a stacked bar chart on contribution of sales per segment per month in percentage.
Today we would like to create a radial alternative of the same chart. 
The key idea is using Polygon as data mark to generate each color block. And we will use 100 points for drawing the contour of each Polygon, or the path.

The steps are as follows:
- Union the data table with itself. This will generate a new column Table Name with two table names.
- Create Point 1 and Point 100 through the two table names.
- Create Bins using Point with step size = 1. These bins will allow us to create 100 points.
- Create IndexPoint which will index the 100 points. Set it to compute along the Point Bins.
- Create IndexMonth which will index the 12 months. Set it to compute along the order date.
- Create Percent field. This is the share of the sales at a particular month. When split by Segment, this will give the percentage of sales in each Segment.
  • Sum(Sales)/Sum({Fixed Year(Order Date),Month(Order Date): Sum(Sales)})
- Create Running Percent. Set to compute along Segment. This will calculate the distance of stacked bars from the center of the circle. This defines the starting point of a polygon.
  • RUNNING_SUM([Percent])
- Create Running Percent Prev. Set to compute along Segment. This defines the end point of a polygon.
  • ZN( LOOKUP([Running Percent],-1))
- Create Running Percent Radial. Set to compute along Point Bins. This helps define the outer radius for all the 100 points of a polygon.
  • IF [IndexPoint]<=50 
  • THEN WINDOW_MAX( [Running Percent])
  • ELSE WINDOW_MAX( [Running Percent Prev])
  • END
- Create Parameter: Radius Inner (An integer from 50 and 100) This defines the inner circle size.
- Create Parameter: Radius Outer (An integer from 100 and 200) This defines the outer circle size where the radial stacked bar chart resides.

- Create Radius. This defines the radius for every points including inner and outer radius.
  • [Radius Inner]+[Radius Outer]*[Running Percent Radial]
- Create Angle. Each of the 100 points is on a circle. We need its Angle to calculate its coordinates. The first 50 points and the second 50 points are on two different circles.
  • (IF [IndexPoint]<=50 
  • THEN [IndexPoint]-1
  • ELSE 100-[IndexPoint]
  • END/50 
  • +([IndexMonth]-1)
  • )* 3.1415926/6
- Create X coordinates
  • SIN([Angle])*[Radius]
- Create Y coordinates
  • COS([Angle])*[Radius]
That's it. The video tutorial can be found here. The workbook can be downloaded here.

11 comments:

  1. Thanks for this, Alexander! This is one of the easiest tutorials that I've seen on this type of graph. As I recreated it with my data, I noticed that - despite having a field equivalent to [Month] that is actually 20 items instead of 12 - I only have 12 slices/angles on my chart. Do you have any idea why that is? I don't really see anything in the formulas that would lead to only 12 angles so I'm reaching out to you.

    ReplyDelete
    Replies
    1. Hard to tell without open your workbook. If possible, send me your workbook so that I can have a look.

      Delete
    2. Hello Nicole. I came across the same issue you described and I just found a solution. The thing is that you should set the right angle increase according to the number of "slices" you want. On my dataset I have 148 countries and I want them unique represented on each bar. Thus, on the "Angle" calculated field I adjusted the bars width as following:

      (IF [IndexPoint]<=50
      THEN [IndexPoint]-1
      ELSE 100-[IndexPoint]
      END/50
      +([IndexMonth]-1)
      ) * RADIANS(2,432432)

      This 2,432 degrees angle comes from 360°/148

      In your case, 360°/20 = 18°. So make it *RADIANS(18).

      On Mr. Mou tutorial, since he wants to divide the circle on 12 slices:

      pi = 3,1415 = 180° --> 360°/12 = 2*pi/12 = pi/6

      Excellent tutorial btw, thank you Alexander.

      Delete
  2. Hi Alexander, great tutorial you have here. I have 2 questions:
    1. How did you get the month labels to line up outside the circle? Mine are overlapped at the edge of the circle and I can't seem to change them
    2. How would you go about a current YTD showing to say August and then the remaining months display the previous year?

    ReplyDelete
    Replies
    1. 1.You can drag the labels and place them to where you like.
      2.Use a rolling 12-month worth of data and you will have a full circle.

      Delete
    2. Thanks, Alexander. Unfortunately, I can't reproduce your results even when I use your workbook and carefully follow the video tutorial to construct the "Radial Stacked Bars" sheet from scratch. I see only two marks per segment per month when marks are automatic and a blank sheet when I select polygon. Is there a subtle configuration change that I am missing?

      Delete
    3. The key is probably the Bins. Make sure they are in the view.

      Delete
    4. I can't reproduce your results even when I use your workbook and carefully follow the video tutorial to construct the "Radial Stacked Bars" sheet from scratch. I see only two marks per segment per month when marks are automatic and a blank sheet when I select polygon. Is there a subtle configuration change that I am missing?

      Delete
    5. The remedy:
      1.Create a calculated field INDEX = Index()
      2.Place this in the Detail card
      3.Set it to compute along the Bins

      Delete
  3. Great tutorial but I think that the title needs to be a bit more descriptive like Creating Radial Stacked Bar Chart using ... (like HTML5, charts.js etc.) so the right person lands on this page

    ReplyDelete