Tuesday, August 17, 2010

SSRS code - colour


http://blogs.msdn.com/bwelcker/archive/2006/09/26/End-of-Amnesia-_2800_Avoiding-Divide-By-Zero-Errors_2900_.aspx

http://blogs.msdn.com/davidlean/archive/2009/02/17/sql-reporting-how-to-conditional-color-1-4-the-basics-report-expressions-custom-code.aspx

Public Function ColorRYG_CTAS123(ByVal Value As Decimal, ByVal MaxPositive As Decimal
, ByVal MaxNegative As Decimal, ByVal Neutral As Decimal) As String
'Example: =code.ColorBack(expression, Max(expression), Min(expression), 0)
'=code.colorback( Fields!Sales.Value,max( Fields!Sales.Value),min( Fields!Sales.Value),0)
'Find Largest Range
Dim decRange As Decimal
Dim decPosRange As Decimal = Math.Abs(MaxPositive - Neutral)
Dim decNegRange As Decimal = Math.Abs(MaxNegative - Neutral)
decRange = IIf(decPosRange > decNegRange, decPosRange, decNegRange)
'Force color into Max-Min Range. Important if you want to Clip the color display to a subset of the data range.
Value = Switch((Value > MaxPositive), MaxPositive, Value <>
'Find Delta required to change color by 1/255th of a shade
Dim decColorInc As Decimal = 255 / decRange
'Find appropriate color shade
Dim iColor As Integer = CInt(Math.Round((Value - Neutral) * decColorInc))
'Return Appropriate +ve or -ve color
Dim strColor As String
If iColor >= 0 Then
'Green
iColor = 255 - iColor 'Thus 0 = White & 255 = Green
strColor = "#" & iColor.ToString("X2") & "FF00"
Else
'Red
iColor = iColor + 255 'NB iColour is -ve; -1 - -255
strColor = "#FF" & Math.Abs(iColor).ToString("X2") & "00"
End If
Return strColor
End Function
Public Function ColorRYG_CTAS45(ByVal Value As Decimal, ByVal MaxPositive As Decimal, ByVal MaxNegative As Decimal, ByVal Neutral As Decimal) As String
'Example: =code.ColorBack(expression, Max(expression), Min(expression), 0)
'=code.colorback( Fields!Sales.Value,max( Fields!Sales.Value),min( Fields!Sales.Value),0)
'Find Largest Range
Dim decRange As Decimal
Dim decPosRange As Decimal = Math.Abs(MaxPositive - Neutral)
Dim decNegRange As Decimal = Math.Abs(MaxNegative - Neutral)
decRange = IIf(decPosRange > decNegRange, decPosRange, decNegRange)
'Force color into Max-Min Range. Important if you want to Clip the color display to a subset of the data range.
Value = Switch((Value > MaxPositive), MaxPositive, Value <>
'Find Delta required to change color by 1/255th of a shade
Dim decColorInc As Decimal = 255 / decRange
'Find appropriate color shade
Dim iColor As Integer = CInt(Math.Round((Value - Neutral) * decColorInc))
'Return Appropriate +ve or -ve color
Dim strColor As String
If iColor >= 0 Then
'Green
iColor = 255 - iColor 'Thus 0 = White & 255 = Green
strColor = "#" & iColor.ToString("X2") & "FF00"
Else
'Red
iColor = iColor + 255 'NB iColour is -ve; -1 - -255
strColor = "#FF" & Math.Abs(iColor).ToString("X2") & "00"
End If
Return strColor
End Function
Public Function ColorRYG_Admissions(ByVal Value As Decimal, ByVal MaxPositive As Decimal, ByVal MaxNegative As Decimal, ByVal Neutral As Decimal) As String
'Example: =code.ColorBack(expression, Max(expression), Min(expression), 0)
'=code.colorback( Fields!Sales.Value,max( Fields!Sales.Value),min( Fields!Sales.Value),0)
'Find Largest Range
Dim decRange As Decimal
Dim decPosRange As Decimal = Math.Abs(MaxPositive - Neutral)
Dim decNegRange As Decimal = Math.Abs(MaxNegative - Neutral)
decRange = IIf(decPosRange > decNegRange, decPosRange, decNegRange)
'Force color into Max-Min Range. Important if you want to Clip the color display to a subset of the data range.
Value = Switch((Value > MaxPositive), MaxPositive, Value <>
'Find Delta required to change color by 1/255th of a shade
Dim decColorInc As Decimal = 255 / decRange
'Find appropriate color shade
Dim iColor As Integer = CInt(Math.Round((Value - Neutral) * decColorInc))
'Return Appropriate +ve or -ve color
Dim strColor As String
If iColor >= 0 Then
'Green
iColor = 255 - iColor 'Thus 0 = White & 255 = Green
strColor = "#" & iColor.ToString("X2") & "FF00"
Else
'Red
iColor = iColor + 255 'NB iColour is -ve; -1 - -255
strColor = "#FF" & Math.Abs(iColor).ToString("X2") & "00"
End If
Return strColor
End Function