Coolest CFCHART Trick Ever
Ok, maybe not ever, but interesting nonetheless. A few days ago a reader pinged me to ask about gauge charts. Those are charts that act like speedometers. I knew it wasn't supported by ColdFusion out of the box, but I recommended he take a look at the WebCharts design tool that ships with ColdFusion. When I did so - I saw right off the bat that it was supported, and there were quite a few of them.
I generated my style xml, passed it to the CFCHART, and promptly got an error about gauge charts not being supported. Darn. I was as frustrated as Britney Spears in a library.
Yesterday the topic of CFCHART came up again, this time on the CFAUSSIE mail list. When I made the same point there, a user named Simon Haddan shared an interesting fact. You can actually use Java to speak directly to the WebCharts engine embedded in ColdFusion. (Christopher Wigginton blogged on this as well back in 2005.)
Essentially, you can use the WebChart's designed to create a WCP file, which is just an XML file. You can then grab the relevant bits out and speak directly to WebCharts. And guess what?
No limits.
Not as far as I can tell. I made this lovely PNG gauge below. (And yes, SWF is supported too.)

The code is a bit complex, and involves some string manipulation, but could be made easier if someone felt like creating a nice CFC for it. Here is the code, and remember credit goes to Simon and Christopher:
<!--- Read the WCP file --->
<cfset sChartStyle = fileRead(expandPath("./raygauge.wcp"))>
<!--- Get the frameChart component --->
<cfset iStart = findNoCase("<gauge",sChartStyle)>
<cfset iEnd = findNoCase("</gauge>",sChartStyle)>
<cfsavecontent variable="chartStyle">
<cfoutput><?xml version="1.0" encoding="UTF-8"?>
#mid(sChartStyle,iStart,iEnd-iStart+13)#
</cfoutput>
</cfsavecontent>
<cfsavecontent variable="chartModel"><?xml version="1.0" encoding="UTF-8"?>
<XML type="default">
<COL>2000</COL>
<ROW col0="88.0">Sample 0:</ROW>
</XML></cfsavecontent>
<cfscript>
oMyWebChart = createObject("Java","com.gp.api.jsp.MxServerComponent");
oMyApp = getPageContext().getServletContext();
oSvr = oMyWebChart.getDefaultInstance(oMyApp);
oMyChart2 = oSvr.newImageSpec();
oMyChart2.width = 375;
oMyChart2.height= 375;
oMyChart2.type = "png";
oMyChart2.style = "#chartStyle#";
oMyChart2.model = "#chartModel#";
</cfscript>
<!--- Create html tag set --->
<cfsavecontent variable="chartImgTag">
<cfoutput>#oSvr.getImageTag(oMyChart2,"http://192.168.1.108/CFIDE/GraphData.cfm?graphCache=wc50&graphID=")#</cfoutput>
</cfsavecontent>
<!--- Good old Webcharts loves to add an extra /Images/ to the URL --->
<cfset chartImgTag = replace(chartImgTag,"http://192.168.1.108/Images/","http://localhost/","All")>
<cfoutput>
#htmlEditFormat(chartimgtag)#
<p>
#chartimgtag#
</cfoutput>
The first part of the file grabs the style portion from the WCP file. I believe this is the same XML you would normally grab from the style tab. The chartModel section is the data. For a gauge it is rather simple - one value. I'm not sure where "Sample 0:" is even used, but the value, 88, is.
Next up we do the Java magic. Note the getImageTag portion expects a URL. This URL represents the same "magic" URL ColdFusion uses to display images/SWF stuff. You probably wouldn't want to hard code an IP like I did. WebCharts also wants to add /Images to this so it has to be stripped out.
The last thing I do is output the HTML in a form I can read, and as is, which works just fine.
Pretty cool, eh? In theory, one could build a custom tag that lets you paste in a MCP and it would run form that, but the data would be hard coded.
Comments
We use custom CFC API (that we built) for charting on GreatBizTools.com to create all the charts that WebCharts3D supports. There are a bunch of helper java classes for data manipulation and style changes. However I won't get into that here as it's mess to work with directly.
An example chart which is similar to yours is here:
http://coldfusionweekly.com/images/1-2/cfw_1-2_cha...
These are not the Droids you are looking for....
Got to get our copies of CF8 installed...
http://www.cfcode.net/index.cfm/2007/10/16/Web-Cha...
@Peter: Sorry I missed your earlier posts!
When we (three of us now) open webcharts.bat in CF8, all the text is binary zero characters. Totally unreadable.
When I try to run this example, with my own (non modified) gauge .wcp file I get the following text and no graph. What am I doing wrong, or missing?
<!-- WebCharts3D v5.1(2077) --> <IMG SRC="http://192.168.1.108/CFIDE/GraphData.cfm?graphCach...; id="Images_3607960040100155_png" name="Images_3607960040100155_png" usemap="#Images_3607960040100155_png_map" border="0"/> <table cellpadding='0' cellspacing='1' style='visibility: hidden;display: none; position:absolute;font-family: Arial;font-size: 13px; font-weight:700;background:#FFFF00;foreground:#0000FF;color:#0000FF;-moz-opacity:.50;-opacity:.50;filter:alpha(opacity=50);border:1px solid #0000FF;' name='GP1215621759443AAAB' id='GP1215621759443AAAB'><tr><td width='8'> </td></table> <MAP name='Images_3607960040100155_png_map'> <AREA shape='rect' coords='0,0,1,1'/> <AREA shape="poly" coords="293,133,191,194,183,180,289,126"/> </MAP> <script language="javascript" src="http://192.168.1.108/CFIDE/GraphData.cfm?graphCach...;
Thanks so much!
-Lyle
I had a similar (the same?) issue. Most of the text visible was showing a square instead of the character. Numbers showed up ok. Called up Gpoint and was told to reload the java virtual machine. This did not work. Found out by accident that if instead of running webcharts.bat you find and doubleclick C:\ColdFusion8\lib\wc50.jar all appears to work ok.
Thanks,
Adam
Hope that helps.
-Lyle
But maybe there's something wrong in the server settings. I just thought since I haven't seen the behavior in any other charts it might be related to this specifically. So I was wondering if anyone knew for sure that the watermark shouldn't be there.
Thanks again,
Adam

