Using csDrawGraph with Cold Fusion

If Cold Fusion is running on Windows it can use COM objects. The DLL file for the object must be registered on the server and any appropriate permissions set. If it is the 64 bit version of Cold Fusion it must be at least Version 10 Update 11. Prior to this update, only the 32 bit version would support COM objects.

The object is created inside a <cfobject> tag. Properties are set and methods called either inside <cfset> tags or inside a <cfscript> block. With our components the class name will differ depending on whether the 32 bit or 64 bit, full or trial versions oare used. Take care to use the appropriate class name.

When a dynamically created image is to be streamed to the browser there are two methods. One is to create a temporary file and stream it using the tag. Most of our examples show this method because it was the only method available with Cold Fusion 5. Modern versions of Cold Fusion can stream a file dynamically using the PageContext Java object, which requires more code but does not create a temporary file.

Both of these methods require the dynamic image to be created in a separate script, called as the src attribute in an image tag.

Creating a graph with Cold Fusion - using a temporary file

The code fragment below shows how to create a graph using 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 allow "Modify" permission to the account which Cold Fusion is using (typically Local Service or Network Service).

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

Creating a graph with Cold Fusion - using PageContext

The code fragment below shows how to create a graph image dynamically and stream it using the PageContext Java Object. This code would also be placed in a script that would be called from an <img> tag as shown above.

<cfcache action="flush">
<cfobject action="create" name="Image" class="csDrawGraph.Draw">
<cfscript>
  Chart.AddData("Item1", 17, "ff0000")
  Chart.AddData("Item2", 28, "00ff00")
  Chart.AddData("Item2", 28, "00ff00")
  Context = GetPageContext();
  Context.SetFlushOutput(false);
  Response = Context.GetResponse().GetResponse();
  Out = Response.GetOutputStream();
  Response.SetContentType("image/gif");
  Out.Write(Chart.GIFBar);
  Out.Flush();
  Response.Reset();
  Out.Close();
</cfscript>

Note how several commands can be added inside a <cfscript> tag.

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.