Search code examples
reportbuilderssrs-2016ssdt-bi

SSRS: Custom colors for the Category axis of a stacked bar chart


I have a stacked bar chart that only ever has 5 categories(but the value of the categories change from year to year, it is a sliding 5 year window).

I have successful customised the bars to the colors I want.

But now I wish to make the label of each Category the same color as the customised bar color.

Is there a way to do this?


Solution

  • You can use custom code for this.

    In Report Properties | Code, you can paste in the following code:

    Private colourPalette As String() = {"#418CF0", "#FCB441", "#DF3A02", "#056492", "#BFBFBF", "#1A3B69", "#FFE382", "#129CDD", "#CA6B4B", "#005CDB", "#F3D288", "#506381", "#F1B9A8", "#E0830A", "#7893BE"}
    Private count As Integer = 0
    Private mapping As New System.Collections.Hashtable()
    
    Public Function GetColour(ByVal groupingValue As String) As String
    
        If mapping.ContainsKey(groupingValue) Then
                        Return mapping(groupingValue)
        End If
    
        Dim c As String = colourPalette(count Mod colourPalette.Length)
    
        count = count + 1
    
        mapping.Add(groupingValue, c)
    
        Return c
    
    End Function
    

    This will give you the option of the pastel colour palette. If you want other colours, simply replace the hex colour codes with values of your choice.

    To use this, simply use the following expression:

    =Code.GetColour(Fields!Thingy.Value)
    

    Use this on your series and your label fill expressions. This will ensure that the same colour appears for both. If you have multiple graphs with the same values in, this will also ensure that the same data series across multiple graphs always have the same colour.