Sunday, 30 October 2011

Displaying Colors in Data Bars based on conditions

By this time we have seen how Data Bars works. Now let us customize it a little bit for displaying custom colors based on conditions. What we will do is that, if the runs made is greater than or equal to 100 we will display Green Color bars else Red color Bars.
Let us enter the below custom code to our report (Kindly refer to Working with expression section for understanding how to enter custom code to report)
'Function to set the color 
Public Shared Function SetColor(ByVal RunsMade As Integer) As String
SetColor= "Red"
If RunsMade  > 100 Then
SetColor= "Green"
End IF
End Function
The code is simple enough to understand. It will return the color as Red or Green depending on the condition.
Now consider we have designed our report as under (we have already seen this earlier. Henceforth, I am not explaining it again)
45.jpg
Let us select the Data Bar for getting the Chart Data popup. Then right-click on the bar and select Series Properties.
46.jpg
In the Series Properties window, select the Fill tab and click the Expression button (fx).
47.jpg
And in the expression editor enter the below expression
 =Code.SetColor(Fields!RunsMade.Value) 
48.jpg
That's it. Now if we run the report, we will get the desired result
49.jpg
Kindly note that, we have given some gradient effect to our report for beautification.

No comments:

Post a Comment

SSIS: Creating Package Configurations

This post discusses the creation of Configuration Files and how they can be useful while migrating a package from one environment to an...