Wednesday, June 2, 2010

Display Total on top of Stacked Chart

It is a normal phenomenon that human beings take most of the simple things for granted. Take this case, one of my blog readers sent me a mail asking how to display the total on top of a stacked chart in SSRS. The first time I read it, I was almost going to blurt - just enable some property to display subtotals. But the more I thought about it, the more elusive the property became and that is when I realized there is no such property. So for the benefit of all, I thought about posting the workaround that I devised for this.

Suppose I have a simple stacked column chart with Month on Categories, Product Category on Series and Sales as the measure. The requirement is to display the total of all Product Categories for a month on top of the stacked column chart. The required output is shown in the below image.

Stacked Column Chart - Total

For this, follow the steps below:-

1) The original query was

SELECT  Month,
            Category,
            Sales
FROM    Sales

Modify it as follows

SELECT   Month,
             Category,
             Sales
FROM     Sales
UNION
SELECT   Month,
             'Total' AS Category,
             0.1 * SUM(Sales) AS Sales
FROM     Sales
GROUP BY Month;

This is done to include an extra row called Total for Product Categories. The value for Sales for this row would be 10% of the total Sales of that month.

2) Go to the Design tab, right click on the chart and enable the Show Data Labels option. Then select the series label properties by right clicking on the data labels.

Show Data Labels

3) Edit the Label data and enter the following expression

=iif(Fields!Category.Value="Total",sum(Fields!Sales.Value,"Chart1_CategoryGroup")-sum(Fields!Sales.Value),"")

Label Data Expression

The above expression displays blank if the Product Category is not “Total” and displays the sum of the entire Product Categories for that month (including the value for Total) – sum of the Total field.

P.S. : “Chart1_CategoryGroup” would have to be replaced with the category group name of your stacked column chart. To find this, click on the Month (category) button in the chart and press F4. You will find the Name of the group in the property panel that opens up as shown below.

Find category group name

4) Now we should be getting the following output when we click on the Preview tab.

Statcked column chart with Total

Go back to the Design tab and click on Product Category (series) in the chart and press F4 to open the property panel. Enter the below expression in the Label property

=iif(Fields!Category.Value="Total"," ",Fields!Category.Value)

Edit Label expression for series

This is done so that the Total value will not appear in the legend.

5) Right click on the stacked columns and select series properties.

Series properties

6) Go to the Fill tab and enter the following expression for the color

=iif(Fields!Category.Value="Total","Transparent","Automatic")

Color expression

This is done so that the Total value will be transparent and hence, would look like it is not present.

7) Preview the report and you should be getting the required output

Final result 

This report has been done with SSRS 2008 R2 but the concept should work in SSRS 2008 more or less the same way.

Update

For SSRS 2008 users, it has been noticed that all the bars become black when the above fill expression is used. That is because the Automatic colour can not be used in expressions. So as a workaround, follow the step below instead of the 6th step above

1) In BIDS, press Alt+R to open the Report Tab and select Report Properties. Then go to the code section and paste the following code

Private colorPalette As String() = {"Green", "Blue", "Red", "Orange", "Aqua", "Teal", "Gold", "RoyalBlue", "#A59D93", "#B8341B", "#352F26", "#F1E7D6", "#E16C56", "#CFBA9B"}
Private count As Integer = 0
Private mapping As New System.Collections.Hashtable()
Public Function GetColor(ByVal groupingValue As String) As String
  If mapping.ContainsKey(groupingValue) Then
    Return mapping(groupingValue)
  End If
  Dim c As String = colorPalette(count Mod colorPalette.Length)
  count = count + 1
  mapping.Add(groupingValue, c)
  Return c
End Function

Rep properties

Right now, there are only 14 colours in the palette but more colours can be added if you have more than 14 values in the series.

2) Use the following expression for colour in the fill tab of series properties.

=iif(Fields!Category.Value="Total", "Transparent",Code.GetColor(Fields!Category.Value))

This would do the trick :)

11 comments:

  1. Jason Thomas this is a blog that is worth plenty! I would like to thank you for this blog that has instructed me on how to implement a total at the top of each stack in a column stack chart. You are a life saver! I hope that the Development Team at Microsoft can read your blog so that they can see how creative you have to be to be able to place a total at the top of each stack, this would be a common requirement in most companies that wish to create column stack type reports and I hope that Microsoft will make this option easier to implement in future releases of Reporting Services.

    Once again, thank you very much Jason, a blog well done! I look forward to future blogs that you develop.
    Steve Kossaris
    stevekossaris@live.com.au

    ReplyDelete
  2. Hey your blog helped me a lot. However, I would like to display values of each stack on top of the bar and then total of the bar totally above all the values. Do you have any suggestions?

    ReplyDelete
  3. Hi Priyanka,
    Do you have an image which shows how you would like to see the chart?

    ReplyDelete
  4. yes I do..
    But how can I attach the screen here?

    ReplyDelete
  5. http://www.megaupload.com/?d=CC52XYJC

    I have uploaded on mega upload. Could you please download from there and see?

    ReplyDelete
  6. @Priyanka : We can't make it exactly as you said (not possible to get the labels in a coloured textbox) but I have mailed you a possible workaround. Let me know if that would do.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  7. I would also be interested in how you were able to get this to work.

    ReplyDelete
  8. @Scott : Could you be more specific?

    ReplyDelete
  9. Is it also possible to work around with the Fetch XML ,to show the Total Counts on a Field in Stacked Bar chart.

    ReplyDelete
    Replies
    1. I am not really sure what you meant by that, but you can get the Total Counts in your dataset query and then display them using the same technique in a Stacked Bar chart

      Delete