Using ChartDirector with ColdFusion
There are several methods to use ChartDirector with ColdFusion. For ColdFusion MX (version 6 or above), it is recommended using ChartDirector for JSP/Java with ColdFusion. For old versions of ColdFusion (eg. Ver 5.x), one may use ChartDirector for ASP/COM/VB. ColdFusion MX with ChartDirector for JSP/Java
Installation To allow ColdFusion CFM scripts to access the ChartDirector for JSP/Java library, one needs to inform ColdFusion where is the "ChartDirector.jar". The steps are:
Using ChartDirector in CFM
ChartDirector for JSP/Java comes with plenty of sample code in JSP. It is easy to translate them to ColdFusion scripts. The following is an example based on the "Simple Bar Chart" (simplebar.jsp) sample code that comes with ChartDirector for JSP/Java. <html> <body topmargin="5" leftmargin="5" rightmargin="0"> <div style="font-size:18pt; font-family:verdana; font-weight:bold"> My First ColdFusion ChartDirector Chart </div> <hr color="#000080"> <cfscript> //The data for the bar chart data = arrayNew(1); data[1] = 85; data[2] = 156; data[3] = 179.5; data[4] = 211; data[5] = 123; //The labels for the bar chart labels = arrayNew(1); labels[1] = "Mon"; labels[2] = "Tue"; labels[3] = "Wed"; labels[4] = "Thu"; labels[5] = "Fri"; //Create a XYChart object of size 250 x 250 pixels c = createObject("java", "ChartDirector.XYChart"); c.init(250, 250); //Set the plotarea at (30, 20) and of size 200 x 200 pixels c.setPlotArea(30, 20, 200, 200); //Add a bar chart layer using the given data c.addBarLayer(data); //Set the labels on the x axis. c.xAxis().setLabels(labels); //output the chart chart1URL = c.makeSession(getPageContext().getRequest(), "chart1"); //include tool tip for the chart imageMap1 = c.getHTMLImageMap("", "", "title='{xLabel}: US${value}K'"); </cfscript> <cfoutput> <img src="getchart.jsp?#chart1URL#" usemap="##map1" border="0"> <map name="map1">#imageMap1#</map> </cfoutput> </body> </html>The key points to note for translating the sample code are:
ColdFusion with ChartDirector for ASP/COM/VB
On Windows, ColdFusion supports COM components by using the CFOBJECT tag. So one way to use ChartDirector with ColdFusion is to use ChartDirector for ASP/COM/VB as a COM component. However, ColdFusion only provides basic COM support. Special considerations are needed to use CFOBJECT to access the ChartDirector API through COM. For this reason, if you are using ColdFusion MX (version 6 or above), it is recommended that you use ChartDirector for JSP/Java instead. The followings are the key points when ColdFusion is used to access the ChartDirector API through COM:
Cold Fusion MX Sample Code
The following is the "Simple Bar Chart" example in the ChartDirector documentation, translated to Cold Fusion MX. <HTML> <BODY> <H1>Hello World - My First Chart</H1> <CFOBJECT action = "Create" type = "COM" class = ChartDirector.API name = "cd"> <!--- The data for the bar chart ---> <CFSET data = ListToArray("85, 156, 179.5, 211, 123")> <!--- The labels for the bar chart ---> <CFSET labels = ListToArray("Mon, Tue, Wed, Thu, Fri")> <!--- Create a XYChart object of size 250 x 250 pixels ---> <CFSET c = cd.XYChart(250, 250)> <!--- Set the plotarea at (30, 20) and of size 200 x 200 pixels ---> <CFSET c.setPlotArea(30, 20, 200, 200)> <!--- Add a bar chart layer using the given data ---> <CFSET c.addBarLayer("Array" & Chr(31) & ArrayToList(data, Chr(31)))> <!--- Set the x axis labels using the given labels ---> <CFSET c.xAxis().setLabels("Array" & Chr(31) & ArrayToList(labels, Chr(31)))> <!--- output the chart ---> <CFSET filename = c.makeTmpFile(ExpandPath("tmpcharts"))> <CFOUTPUT> <IMG SRC="tmpcharts/#filename#"> </CFOUTPUT> </BODY> </HTML> Cold Fusion 5.0 Sample Code
The following is the "Simple Bar Chart" example in the ChartDirector documentation, translated to Cold Fusion 5.0. <HTML> <BODY> <H1>Hello World - My First Chart</H1> <CFOBJECT action = "Create" type = "COM" class = ChartDirector.API name = "cd"> <!--- The data for the bar chart ---> <CFSET data = ListToArray("85, 156, 179.5, 211, 123")> <!--- The labels for the bar chart ---> <CFSET labels = ListToArray("Mon, Tue, Wed, Thu, Fri")> <!--- Create a XYChart object of size 250 x 250 pixels ---> <CFSET c = cd.XYChart(250, 250, "&Hffffff", "&Hffffff", 0)> <!--- Set the plotarea at (30, 20) and of size 200 x 200 pixels ---> <CFSET c.setPlotArea(30, 20, 200, 200, cd.Transparent, -1, cd.LineColor, "&Hc0c0c0", cd.Transparent)> <!--- Add a bar chart layer using the given data ---> <CFSET c.addBarLayer("Array" & Chr(31) & ArrayToList(data, Chr(31)), -1, "", 0)> <!--- Set the x axis labels using the given labels ---> <CFSET xAxis = c.xAxis()> <CFSET xAxis.setLabels("Array" & Chr(31) & ArrayToList(labels, Chr(31)))> <!--- output the chart ---> <CFSET filename = c.makeTmpFile(ExpandPath("tmpcharts"), cd.PNG, 600)> <CFOUTPUT> <IMG SRC="tmpcharts/#filename#"> </CFOUTPUT> </BODY> </HTML> |