Thursday 6 February 2014

ColdFusion-UI-the-Right-Way: <cfchart>

Yay. I've finally participated in an open source project on Github. I've done a "chapter" on "ColdFusion UI the Right Way", covering <cfchart>. Well: using ZingCharts directly instead of <cfchart>. An easy win for me for today's article is to repeat the chapter here.


The <cfchart> tag provides the ability to create charts in CFML. As a very minimal example:

Listing 1 : min.cfm

<cfchart format="html">
    <cfchartseries type="Bar" label="Numbers">
         <cfchartdata item="e" value="2.71828">
        <cfchartdata item="&pi;" value="3.14159">

<cfchart> can implement charts as images in JPG or PNG format, as well as Flash, and - the focus of this chapter - JS/CSS/HTML. In versions of ColdFusion from 6.x to 9.x the chart engine was restricted to server-side-generated JPG/PNG/Flash-based charts; ColdFusion 10 added client-side charting capabilities.

For this chapter we will make use of the ZingCharts library, which is the same library ColdFusion uses. However we will simply use ZingCharts directly. Coincidentally the examples will also use jQuery to provide AJAX access to the data for the example, but this is just to demonstrate separation of model and view concerns, and is not a requirement of ZingCharts itself. Please note that only ColdFusion 10 enterprise includes licensing for ZingCharts; if one is not using that version, a separate licence will be required from ZingCharts. For unlicensed developmental purposes ZingCharts is fully-functional, but includes a watermark, which is removed once a licence is applied.

As a baseline, here is how one might implement a quick stacked bar chart with <cfchart>:

Listing 2 : cfchart.cfm

<cfset records = new DAO().getDataForCfmlVersion()>
<cfset seriesColours = ["Green","Yellow","Purple"]>
<cfset seriesColour = 1>

<cfchart chartwidth="1000" chartheight="750" seriesplacement="stacked" format="html">
    <cfloop query="records" group="item">
        <cfchartseries type="Bar" label="#item#" seriescolor="#seriesColours[seriesColour++]#">
                <cfchartdata item="#day#" value="#total#">

With the data returned from the DAO, this renders:

Now here is an analogous chart, totally written in simple HTML and JavaScript.

Listing 3 : zingChart.html

<!doctype html>
        <script type="text/javascript" src="lib/js/zingchart_trial/html5_scripts/zingchart-html5-min.js"></script>
        <script type="text/javascript" src=""></script>
        <div id="myChartDiv"></div>
    <script type="text/javascript" src="lib/js/renderChart.js"></script>

Listing 4 : renderChart.js


    var applyLabels = function(records){
        seriesColours = ["Green","Yellow","Purple"];
        records.forEach(function(series, index){
            series["background-color"] = seriesColours[index];
        return records;

    var configureChartParams = function(data){
        return {
            id      : "myChartDiv",
            height  : 750,
            width   : 1000,
            data    : myChart = {
                type    : "bar",
                stacked : true,
                series  : data.records,
                "scale-x"   : {
                legend      : {}


As you can see, the only mark-up needed is the myChartDiv <div>. The rest is done with JavaScript. It might seem like the JavaScript is complicated, but it boils down to a call to zingchart.render(). passing it an object containing data and config. As I said above I am using AJAX here to fetch the raw data from the server, but that is not a requirement, it could just as easily be inline JavaScript. Here's the render:

The styling is not exactly the same, but that's due to ColdFusion using different "default" styling than ZingCharts does.

For completeness, here is the code for DAO.cfc:

Listing 5 : DAO.cfc

component {

    remote struct function getDataForJSVersion() returnformat="json" {
        return {
            "labels"    = "Mon,Tue,Wed,Thu,Fri",
            "records"    = [
                    "text"    ="Apples",
                    "text"    ="Bananas",
                    "text"    ="Cherries",

    public query function getDataForCfmlVersion(){
        return queryNew(
            "integer,varchar,varchar,integer", [
                [1, "Mon", "Apples", 1],
                [4, "Tue", "Apples", 2],
                [7, "Wed", "Apples", 4],
                [10, "Thu", "Apples", 8],
                [13, "Fri", "Apples", 4],
                [2, "Mon", "Bananas", 10],
                [5, "Tue", "Bananas", 8],
                [8, "Wed", "Bananas", 6],
                [11, "Thu", "Bananas", 4],
                [14, "Fri", "Bananas", 6],
                [3, "Mon", "Cherries", 1],
                [6, "Tue", "Cherries", 3],
                [9, "Wed", "Cherries", 9],
                [12, "Thu", "Cherries", 3],
                [15, "Fri", "Cherries", 1]


That's it. We want to keep these things short and sweet: offer an alternative, not an exhaustive list of options, or demonstrating every single compatibility consideration. The effort is not to emulate what CFML does exactly, but to point out there are easy options that just don't use CFML at all.