0 Replies Latest reply on Feb 8, 2011 1:06 PM by mikescott

    How to create trend line in cfchart?


      I can create a scatter chart in cfchart - but I do not know how to create a linear regression trend line in the same chart as a second data series. Kodemonki provided an answer in a forum discussion a few years ago (http://forums.adobe.com/message/192848#192848) - but I am not able to make it work (I have a dangerously small knowledge of coldfusion and statisitcal formulas). Can someone tell me where i put in my to data query variables - such as for a query that gives results for two data fields, "Item1" and "Item2".


      Here is Kodemonki's solution:



      <!--- The min setting is a preference based on how you want the data to be displayed. --->




      <!---<cfset min = arrayMin(grabData["value"]) - 5>--->


           <cfset min = 0>

           <cfset max = ceiling(arrayMax(grabData["value"])) + 1>


           <cfset n = grabData.recordCount>

           <cfset sum_x = 0>

           <cfset sum_x2 = 0>

           <cfset sum_xy = 0>

           <cfloop from="1" to="#grabData.RecordCount#" index="i">

                <cfset sum_x = sum_x + i>

                <cfset sum_xy = (i*grabData.value[i]) + sum_xy>

                <cfset sum_x2 = sum_x2 + (i*i)>


           <cfset sum_y = arraySum(grabData["value"])>

           <cfset slope = round(((n*sum_xy) - (sum_x*sum_y))/((n*sum_x2) - (sum_x*sum_x)))>

           <cfset intercept = round((sum_y - (slope*sum_x))/n)>

           <cfset best_fit = QueryNew("id, value")>

           <cfloop from="1" to="#grabdata.RecordCount#" index="i">

                <cfset queryAddRow(best_fit)>

                <cfset value = round((slope*i) + intercept)>

                <cfset QuerySetCell(best_fit, "id", grabData.xlabel[i])>

                <cfset QuerySetCell(best_fit, "value", value)>


           <cfchart scalefrom="#min#" scaleto="#max#" title="#GrabDatapointInfo.label#" format="jpg" chartHeight="450" chartwidth="500" xaxistitle="Month/Year">

                <cfchartseries type="line" query="best_fit" valuecolumn="value" itemcolumn="id"/>

                <cfchartseries type="line" query="grabData" valuecolumn="value" itemcolumn="xlabel"/>