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

In this entry you speak of both a WCP file and a MCP file. Is this a typo or are these two different files?
# Posted By Joey Krabacher | 1/18/08 4:25 PM
I meant WCP. Edited - and thanks.
# Posted By Raymond Camden | 1/18/08 4:27 PM
Yeah, Matt and I talked about this way back in ColdFusion Weekly Podcast version 1.2b in April of 2006.

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...
# Posted By Peter J. Farrell | 1/18/08 4:44 PM
Ray waves hand:
These are not the Droids you are looking for....
# Posted By SigEpJedi | 1/18/08 4:48 PM
BTW, the sample: 0 is the label for the row. Since the gauge chart isn't using the label -- it's not shown.
# Posted By Peter J. Farrell | 1/18/08 4:51 PM
You can also have CF talk to Batik (which comes installed with CF7+8). Batik renders SVG files, which are just XML, into PNG or JPEG. I did exactly that to generate speedometer-type graphs (among many other things) for nowrists.com.
# Posted By Rick O | 1/18/08 5:42 PM
Quite slick. I bet this is one of those tricks that doesn't work with CF7, though. I made a guage-style .wcp file, and set up the example here, and ran it. With CF7, I get "Invalid style type: gauge".

Got to get our copies of CF8 installed...
# Posted By Alan McCollough | 1/18/08 6:52 PM
I've most definitely done the "gauge" style charts on CF7, however I never used the .wcp files instead using the java classes that represent data and charting styles.
# Posted By Peter J. Farrell | 1/18/08 8:13 PM
Dont forget the designer that is included with CF7, so you have a GUI you can design your chart with, then save it off and use it. =)

http://www.cfcode.net/index.cfm/2007/10/16/Web-Cha...
# Posted By Justice | 1/21/08 7:03 AM
@Justice: Errr - isn't that I mentioned in the article? ;) I said that I used that - but it still failed when I passed the XML to cfchart.

@Peter: Sorry I missed your earlier posts!
# Posted By Raymond Camden | 1/21/08 7:11 AM
Ray

Sorry bud, thats what I get for reading too quick =)
# Posted By Justice | 1/21/08 1:02 PM
Anyone have this issue:
When we (three of us now) open webcharts.bat in CF8, all the text is binary zero characters. Totally unreadable.
# Posted By randy | 1/22/08 10:04 AM
Hello,

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
# Posted By J. Lyle Martin | 7/9/08 11:45 AM
I spoke with J off line - the issue was forgetting to update the URL in the code.
# Posted By Raymond Camden | 7/9/08 12:41 PM
To randy:
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.
# Posted By Larry | 7/18/08 12:26 PM
Am I correct in assuming the 'Adobe ColdFusion 8 Developer Edition' water mark will show up regardless of your Coldfusion server license?

Thanks,
Adam
# Posted By adam | 7/25/08 4:10 PM
You can develop charts on the Developer Edition, you will see the watermark there. When you move the same code to a licensed version of CF you will no longer see the watermark.

Hope that helps.

-Lyle
# Posted By J. Lyle Martin | 7/25/08 4:18 PM
I understand that in general. I'm asking because I'm on a server that should be legitimately licensed, and doesn't show the watermark for normal graphs, but still shows it for any of the graphs that directly address webcharts3d classes.

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
# Posted By adam | 7/25/08 4:24 PM
I haven't tested this yet on production. It may be that it does indeed show the watermark. Has anyone else tried it?
# Posted By Raymond Camden | 7/28/08 7:55 PM