Monday 9 May 2011

SSRS - Charts

Odd beasts! Out of the box they're quite straight forward. However, here are a few notes on their usage.

1. Custom Palette for a Pie-Chart


If, for example you have two pie charts on a single report, you may need for them to be coloured differently (ie use two different colour palettes).


Bizarrely, the only way to do this is to use some custom code to build a custom palette!

1.1 Define the chart series groups and category groups. (ie create the pie chart)
1.2 Define the custom color palette and add custom code.
The colorPalette variable stores the definition of our custom color palette, which has 15 distinct colors. The count variable keeps track of the total count of distinct grouping values in order to wrap around once we exceed the number of distinct colors in the custom color palette. The mapping hash table keeps track of the mapping between grouping values and colors. This ensures that all data points within the same data series have the same color. Later it is used to synchronize the custom legend colors with the chart colors. The following code goes into the custom code window of the report.

 
Private colorPalette As String() = {"Silver", "IndianRed", "RosyBrown", "Peru", "Goldenrod", "DarkKhaki", "Yellow", "YellowGreen", "PaleTurquoise", "Teal", "SkyBlue", "MediumPurple", "Plum", "HotPink"}
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

 Cut and paste the above into the REPORT>PROPERTIES>CODE [Tab]


1.2 Call the GetColor() function to assign colors to data points.
=Code.GetColor(Fields!Security_Country_Name.Value)
The GetColor function is called from the fill color style properties. Edit the data value to open the Edit Chart Value dialog box and click the Appearance tab (Figure 15). Click the Series Style button and click the Fill tab. The current series group value is passed as an argument to the GetColor function, which is needed to map the internal group instance value to the color value.


As an alternative to the above, you may wish to designate a colour for each specific country (instead of using a palette to 'reandomly' assign the colout to each country on each page of the report). To do this, instead of the Report>Properties>Code as added above use the following function utilising a switch statement to determine a specific colour that will always be applied for each country:
function GetCountryColour(Security_Country_Name)
   dim CountryColour as string

  select case Security_Country_Name
    case "Albania"
       CountryColour = "LightGreen"
    case "Algeria"
       CountryColour = "yellow"
    case "Angola"
       CountryColour = "OrangeRed"
    case "Argentina"
       CountryColour = "CornflowerBlue"
    case "Australia"
       CountryColour = "Silver"
    case "Austria"
       CountryColour = "Red"
    case "Azerbajan"
       CountryColour = "Blue"
    case "Bahamas"
       CountryColour = "Green"
    case "EMU"
       CountryColour = "Purple"
    case "United Kingdom"
       CountryColour = "Orange"
    case else
       CountryColour = ""
    end select
       GetCountryColour= CountryColour
end function

Call the function in the same way as the palette code above using the syntax:
=Code.GetCountryColour(Fields!Security_Country_Name.Value)

No comments:

Post a Comment