Consistent custom palette across charts

  • I am attempting to create a custom palette to use for multiple charts on my report.  I always want series 1 to be ColorA, series 2 = ColorB, etc.  I am not trying to match values to certain colors.  I am attempting to use the custom code functionality, so I can easily replicate the palette across reports.  I have tried to use the following code I found on the web, but with mixed results.

    Private colorPalette As String() = {"CornflowerBlue","BurlyWood","IndianRed","SteelBlue","Khaki"}
    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

    Then the Fill color for the series group was set to the expression, where "GroupLabel" is my series variable
    =Code.GetColor(Fields!GroupLabel.Value)

    The problem is that I have multiple pie charts and bar charts and each chart starts with a different color for the first series.  For example, Pie1 has Male and Female and the colors are the first and second colors in my function.  Then Pie2 uses the 3rd and 4th colors for its two values of Yes and No.  When we get to Chart3, which has 3 values, it will use the colors from my function in the order of 5,1,2.

    It seems the function is using a hashtable to always use the same color for each value, but how do I write it so that each chart starts with the first one in the string for the first series?

  • mikeg13 - Saturday, February 11, 2017 4:45 PM

    I am attempting to create a custom palette to use for multiple charts on my report.  I always want series 1 to be ColorA, series 2 = ColorB, etc.  I am not trying to match values to certain colors.  I am attempting to use the custom code functionality, so I can easily replicate the palette across reports.  I have tried to use the following code I found on the web, but with mixed results.

    Private colorPalette As String() = {"CornflowerBlue","BurlyWood","IndianRed","SteelBlue","Khaki"}
    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

    Then the Fill color for the series group was set to the expression, where "GroupLabel" is my series variable
    =Code.GetColor(Fields!GroupLabel.Value)

    The problem is that I have multiple pie charts and bar charts and each chart starts with a different color for the first series.  For example, Pie1 has Male and Female and the colors are the first and second colors in my function.  Then Pie2 uses the 3rd and 4th colors for its two values of Yes and No.  When we get to Chart3, which has 3 values, it will use the colors from my function in the order of 5,1,2.

    It seems the function is using a hashtable to always use the same color for each value, but how do I write it so that each chart starts with the first one in the string for the first series?

    In the past, I have simply cloned the code.  Created GetColor1, GetColor2 etc with equivalent Count1, Count2, ... and Mapping1, Mapping2 etc variables.  Each chart uses a separate function.

    At the end of the day, you need to have separate "mapping" and "count" variables for each chart. There are always many ways to achieve this.  My simplistic cloning of the code worked for me at the time but you can code this however you wish

  • I guess that is a solution, but not as elegant as I was hoping.  But I had not thought of that.

    Is there some code that I can add, like a second parameter, that would allow for each chart to start over again at the first color in the palette?  This code is not SQL so I would not even know where to begin trying to change the code.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply