<!---//-------------------------------------------------------------
Writing a chart to a file and then displaying it using a .CFC
Written by Chris Peterson, override11@gmail.com

I have always had erratic problems displaying a chart directly
to the end users screen. I have yet to figure out why I have
these problems, but if I write the chart to a variable, and then
save it to a file and display it, it always works perfect.

Here is a .cfc I wrote to dynamically write a chart to a folder
you specify inside this .cfc. It would be easy for you to
modify so you could pass as an additional arguemnt what sub-folder
you want to place the charts in, I have this hard-coded to a sub-folder
named 'charts'.

To use this CFC, simply do your CFCHART tag as you always would
normally, only add the NAME='MyChartName' parameter (which instead
of returning the chart to the screen, will write it to a variable
named 'MyChartName'. Then invoke the CFC like this:

<cfinvoke component="Chart" method="WriteChartToFile" returnvariable="OutputChart">
    <cfinvokeargument name="ChartName" value="#MyChartName#">
    <cfinvokeargument name="Height" value="200">
    <cfinvokeargument name="Width" value="350">
    <cfinvokeargument name="chartformat" value="flash">
</cfinvoke>

Then, to display the resulting file, simply this!

<cfoutput>#OutputChart#</cfoutput>

----------------------------------------------------------//--->

<!---// Begin code for Chart.cfc //--->

<cffunction
        
    name="WriteChartToFile"
    
        returntype="string"
    
        hint="Accept parameters and return a string that is the code necessary to display a .swf file">

    
     <cfargument
    
            name="chartname"
    
            type="any"
    
            required="true"
    
            hint="Pass in a variable that contains the cfchart flash data">
    
    <cfargument
    
            name="width"
    
            type="any"
    
            required="true"
    
            hint="Pass in a numeric width for the resulting chart">
    
    <cfargument
    
            name="height"
    
            type="any"
    
            required="true"
    
            hint="Pass in a numeric height for the resulting chart">
    
    <cfargument
    
            name="chartformat"
    
            type="any"
    
            required="true"
    
            hint="Pass the chart type you are giving me. Valid types are flash,jpg, or png">
        
        <!--- figure out file name extension based on chart type --->
        <cfswitch expression="#Arguments.chartformat#">
            <cfcase value=
"flash"><cfset Fileext = '.swf'></cfcase>
            <cfcase value=
"png"><cfset Fileext = '.png'></cfcase>
            <cfcase value=
"jpg"><cfset Fileext = '.jpg'></cfcase>
        </cfswitch>

        <!--- First, generate a random file name --->
        <cfset filename = RandRange(10000, 99999) & '#fileext#'>

        <cftry>
            <cfif FileExists(#ExpandPath(
'charts/' & filename)#)>
                <cffile action=
"delete" file="#ExpandPath('images/' & filename)#">
            </cfif>
            <cfcatch type=
"any"></cfcatch>
        </cftry>

        <cflock name=
"ReservationChart" type="exclusive" timeout="10">
            <cffile
                action=
"write"
                charset=
"ISO-8859-1"
                file=
"#ExpandPath('charts/' & filename)#"
                output=
"#arguments.chartname#"
                addnewline=
"yes">
        </cflock>

        <cfset tempstring = "<div style='z-index:-5;' align='left'><object><param name='movie' value='charts/#filename#'>
        <embed src='charts/#filename#' width='#Arguments.Width#' height='#Arguments.Height#'></embed>
        </object></div>"
>
        <cfset returnstring =
'#Replace(tempstring, "'", """", "ALL")#'>


        <cfreturn returnstring>

</cffunction>

<!---// END chart.cfc //--->

About This Tutorial
Author: Justice
Skill Level: Intermediate 
 
 
 
Platforms Tested: CFMX
Total Views: 21,117
Submission Date: January 14, 2005
Last Update Date: June 05, 2009
All Tutorials By This Autor: 4
Discuss This Tutorial
  • Great Tutorial -- thanks

Advertisement

Sponsored By...
Powered By...