Using csDrawGraph with Cold Fusion

If Cold Fusion is running on Windows NT Server/2000/2003 it can use COM objects. The DLL file for the object must be registered on the server and any appropriate permissions set.

The object is created inside a <cfobject> tag. Properties are set and methods called either inside <cfset> tags or inside a <cfscript> block. Cold Fusion does not directly support binary streaming, except from a file, so any dynamically created image must be saved to disk as a temporary file. This is not a problem because the <cfcontent> tag which controls streaming has an automatic delete option.

*** Note *** There is an alternative method available in Cold Fusion MX which uses the PageContext Java Object and this can stream images directly to the browser without saving to a temporary file first. The is more information in the online manual.

The code fragment below shows how to create a bar chart with csDrawGraph. The image is saved as a temporary file with a unique name by using the CreateUUID function. To delete the file after streaming the directory permissions must give the Internet Guest User "Modify" permission.

<cfcache action="flush">
<cfobject action="create" name="chart" class="csDrawGraph.Draw">
<cfset tempfile=ExpandPath(".") & "\" & CreateUUID() & ".gif">
<cfset Chart.AddData("Item1", 17, "ff0000")>
<cfset Chart.AddData("Item2", 28, "00ff00")>
<cfset Chart.AddData("Item3", 5, "0000ff")>
<cfset chart.SaveGIFBar(#tempfile#)>
<cfcontent type="image/gif" deletefile="yes" file=#tempfile#>

To display the above chart in a web page the script name will be put inside an <img> tag. So if the code shown above forms a script called "bar.cfm" the web page which displays the chart would have an <img> tag like this:

<img src="bar.cfm">

The instructions to csDrawGraph are aimed at ASP users and assume that VBScript is used. Cold Fusion creates an instance of a COM object by using the <cfobject> tag, as shown in the code above. Calling a method or setting a property is done from inside a <cfset> tag. There are also some syntax differences when using Cold Fusion, the most important being the use of brackets when calling functions.

VBScript:       Chart.AddData "Item1", 17, "ff0000"

Cold Fusion:  <cfset Chart.AddData("Item1", 17, "ff0000")>

There are some working examples of using csDrawGraph with Cold Fusion in our demonstration area.