Coloring CFCHART Dynamically

Here is a question that came up earlier this morning on a mailing list. How can we specify colors for CFCHART based on some particular business rule? So for example, imagine we have a set of average temperatures. You want to chart them and specifically call out values that are below, or above, certain notes. This is rather simple if you make use of the colorList attribute of cfchartseries. Normally this is used to provide a static list of colors, but you can certainly make it dynamic as well. Here is a simple example.

Let’s start off with some simple, static data, and no, this isn’t real temperatures for Louisiana.

<cfset data = arrayNew(2)> <cfset data[1][1] = "Jan"> <cfset data[1][2] = 30> <cfset data[2][1] = "Feb"> <cfset data[2][2] = 60> <cfset data[3][1] = "Mar"> <cfset data[3][2] = 70> <cfset data[4][1] = "Ap"> <cfset data[4][2] = 80> <cfset data[5][1] = "May"> <cfset data[5][2] = 85> <cfset data[6][1] = "Jun"> <cfset data[6][2] = 95> <cfset data[7][1] = "Jul"> <cfset data[7][2] = 105> <cfset data[8][1] = "Aug"> <cfset data[8][2] = 104> <cfset data[9][1] = "Sep"> <cfset data[9][2] = 95> <cfset data[10][1] = "Oct"> <cfset data[10][2] = 80> <cfset data[11][1] = "Nov"> <cfset data[11][2] = 75> <cfset data[12][1] = "Dec"> <cfset data[12][2] = 60>

Next, let’s supply this to a cfchart just to see how it looks without any modification:

<cfchart chartheight="500" chartwidth="600" title="Average Temperature" show3d="false" > <cfchartseries type="bar" paintstyle="shade"> <cfloop index="datum" array="#data#"> <cfchartdata item="#datum[1]#" value="#datum[2]#"> </cfloop> </cfchartseries> </cfchart>

This results in the rather lovely chart you see here:

Ok, so it works. I’m not exactly sure where the Halloween Orange theme came from, but it works. So now let’s make it dynamic. I’m going to use the following rules for my colors:

  • If the temperature was less than 40, use blue (get it?).
  • If the temperature was greater than 90, use red (clever, I know).
  • Otherwise just use green. </ul> I whipped up the following to generate a color list:

    <cfset cList = ""> <cfloop index="datum" array="#data#"> <cfif datum[2] lt 40> <cfset cList = listAppend(cList, "##0000FF")> <cfelseif datum[2] gt 90> <cfset cList = listAppend(cList, "##FF0000")> <cfelse> <cfset cList = listAppend(cList, "##00FF00")> </cfif> </cfloop>

    Next I passed in cList:

    <cfchartseries type="bar" colorlist="#cList#" paintstyle="shade">

    And voila:

    Not exactly a work of art, but better. You can quickly see which items are at the extreme of my data set. I've pasted the complete template below.

    <cfset data = arrayNew(2)> <cfset data[1][1] = "Jan"> <cfset data[1][2] = 30> <cfset data[2][1] = "Feb"> <cfset data[2][2] = 60> <cfset data[3][1] = "Mar"> <cfset data[3][2] = 70> <cfset data[4][1] = "Ap"> <cfset data[4][2] = 80> <cfset data[5][1] = "May"> <cfset data[5][2] = 85> <cfset data[6][1] = "Jun"> <cfset data[6][2] = 95> <cfset data[7][1] = "Jul"> <cfset data[7][2] = 105> <cfset data[8][1] = "Aug"> <cfset data[8][2] = 104> <cfset data[9][1] = "Sep"> <cfset data[9][2] = 95> <cfset data[10][1] = "Oct"> <cfset data[10][2] = 80> <cfset data[11][1] = "Nov"> <cfset data[11][2] = 75> <cfset data[12][1] = "Dec"> <cfset data[12][2] = 60> <cfset cList = ""> <cfloop index="datum" array="#data#"> <cfif datum[2] lt 40> <cfset cList = listAppend(cList, "##0000FF")> <cfelseif datum[2] gt 90> <cfset cList = listAppend(cList, "##FF0000")> <cfelse> <cfset cList = listAppend(cList, "##00FF00")> </cfif> </cfloop> <cfchart chartheight="500" chartwidth="600" title="Average Temperature" show3d="false" > <cfchartseries type="bar" colorlist="#cList#" paintstyle="shade"> <cfloop index="datum" array="#data#"> <cfchartdata item="#datum[1]#" value="#datum[2]#"> </cfloop> </cfchartseries> </cfchart>

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments