Reputation: 45
I am building a SSRS report and using a gradient color heatmap using a snippet that I found on another site. It works well, but produces a "blue to red" color scale. I am not pro enough to change this code to do a "green to red" color scale. Any advice would be super helpful.
Thanks!
Public Function ColorRYG(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 < MaxNegative, MaxNegative, True, 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 = "#" & Math.Abs(iColor).ToString("X2") & Math.Abs(iColor).ToString("X2") & "FF"
Else
'Red
iColor = iColor + 255 'NB iColour is -ve; -1 - -255
strColor = "#FF" & Math.Abs(iColor).ToString("X2") & Math.Abs(iColor).ToString("X2")
End If
Return strColor
end function
I tried changing some part of the code that I deemed responsible for the color coding but couldnt change it to green
Upvotes: 0
Views: 158
Reputation: 21748
I've got no way to test this at the moment but give this a try...
First, I've taken the "Green" section of code and broken down the part where strColor is set, so you can see how each part of the colour is made up. The color are being set in Hex notation so #FFFFFF is white #000000 is black, #FF0000 is solid red as each pair of characters represent the red, green and blue values between #00 and #FF (0 - 255)
So here's your existing code
strColor = "#" &
Math.Abs(iColor).ToString("X2") & ' red
Math.Abs(iColor).ToString("X2") & ' green
"FF" ' blue
I think a simple change like this might fix it (but not sure as I can't test)
strColor = "#" &
Math.Abs(iColor).ToString("X2") & ' red
"FF" & ' green
Math.Abs(iColor).ToString("X2") ' blue
Here we are maxing out the green value to FF (255) but making the red and blue value variable based on iColor. The is the opposite of what the original code did where blue was fixed as FF and green and red were variable.
Upvotes: 0