Skip to main content
February 24, 2011
Question

How to create a trend line in cfchart?

  • February 24, 2011
  • 1 reply
  • 990 views

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".

Thanks!

------------------------------------------

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) + sum_xy>

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

     </cfloop>

     <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)>

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

     </cfloop>

     <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"/>

     </cfchart>

    This topic has been closed for replies.

    1 reply

    Inspiring
    February 25, 2011

    Have you read the docs for CFCHART, CHCHARTSERIES, etc?  Did you mess around with the example code sufficiently so you understand how the tags work?

    http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7930.html

    --

    Adam