Search code examples
reporting-servicessql-server-2008-r2ssrs-2008

changing bar fill colours in ssrs chart


SO Post

Current I've got 5 bars in my RS chart - in the future there might be 7 bars or 17 bars or 27 bars!

With a couple of bars I can have an expression like this:

=iif(Fields!Market.Value = "Spain"
,"Gold"
,iif (Fields!Market.Value = "Denmark"
    , "Gray"
    , iif(Fields!Market.Value = "Italy"
        , "Blue"
        , "Purple"
        )
    )
)

If I can't predict how many countries will be included + I'd rather not have to hard code in "Green", "Red" etc how do I change the expression?

I've tried this but it is erroring:

=Switch(Mod(Fields!Rank.Value/CDbl(2))=CDbl(0), "Gold", 
   Mod(Fields!Rank.Value/CDbl(3))=CDbl(0), "Gray", 
   Mod(Fields!Rank.Value/CDbl(2))>CDbl(0) "Blue")

Above is the totally incorrect syntax: This works:

=Switch(CDbl(Fields!Rank.Value Mod 2)=CDbl(0), "Gold", 
   CDbl(Fields!Rank.Value Mod 3)=CDbl(0), "Gray", 
   CDbl(Fields!Rank.Value Mod 2)>CDbl(0), "Blue")

Ok - the above runs (not sure how!) but the below is based on help from Dominic Goulet and is really easy to follow and nice and expandable to more colours; this is the solution for 5 colours:

=Switch(CDbl(Fields!Rank.Value Mod 5)=CDbl(0), "Gold", 
   CDbl(Fields!Rank.Value Mod 5)=CDbl(1), "Gray", 
   CDbl(Fields!Rank.Value Mod 5)=CDbl(2), "Green", 
   CDbl(Fields!Rank.Value Mod 5)=CDbl(3), "Red", 
   CDbl(Fields!Rank.Value Mod 5)=CDbl(4), "Pink")

Solution

  • First of all, instead of using many "IIF"s, you should use "Switch", it's leaner that way.

    Switch(Fields!Market.Value = "Spain", "Gold",
           Fields!Market.Value = "Denmark", "Gray",
           Fields!Market.Value = "Italy", "Blue")
    

    Now if you want a color per coutry, you should defenitely store that in your database and pull it out when you need it. That way, a country will always have the same color on every report you have.