FusionCharts and ColdFusion> Using dataURL method |
As with all other languages, FusionCharts can be used with ColdFusion too using both dataURL and dataXML method. In this part, we stick to CF (ColdFusion ) as our choice of scripting language and revisit the process with CF code samples. We've an entire blueprint application using FusionCharts and ColdFusion - you can download the source from our website (http://www.InfoSoftGlobal.com/FusionCharts) to learn more. As we had earlier seen the dataURL method, it requires two pages:
The above two are present in the downloads under the folder FusionCharts2_3 > Sample Code > DBExamples > CF > dataURL. The application will use a ColdFusion array called Colors, defined in Application.cfm, that looks as follows: |
<cfset Colors[1] = "0099FF"> |
Basically, this file is a list of hex colors that we'll be using for the chart. In the above list, we've stored only 20 colors. You can extend it to any number. You should note that all the above colors are without the #. We'll include this file in all our pages, where we generate the XML data for FusionCharts. The application uses also a database connection defined by the ColdFusion server, called “q01nw”. This datasource refers to the NorthWind MS SQL database. This will be used in the Datasource attribute of the CFQUERY object. The page Data.cfm is responsible for relaying virtual XML documents to the FusionCharts embedded in Chart.cfm. You should note that Data.cfm does NOT write XML data documents to the server disk physically. It relays the XML data document virtually (i.e., no XML files are created and saved on the server). The code for this page can be reproduced as under: |
<cfset curYear = url.Year> <!--- 'strXMLData - String variable to contain the entire XML data document for the chart ---> <cfset strXMLData = ""> <!--- --------------------XML Data for TOP 5 COUNTRIES------------------------- ---> <cfquery name="oRs" datasource="q01nw"> SELECT TOP 5 Country, SUM(ExtendedPrice) As Total, COUNT(DISTINCT OrderID) As orderNumber FROM Invoices WHERE YEAR(OrderDate)=#curYear# GROUP BY Country ORDER BY SUM(ExtendedPrice) DESC </cfquery> <!--- 'Create the <graph> element ---> <cfset strXMLData = "<graph caption='Top 5 Countries for the Year " & curYear & "' shownames='1' showvalues='0' decimalPrecision='0' numberPrefix='$'>" & chr(10) > <cfset intCounter = 0> <cfloop query="oRs"> <!--- 'Append the value in format <set name='...' value='...' color='...' /> ---> <cfset strXMLData = strXMLData & "<set name='" & Country &"' value='" & Total & "' color='" &Colors[(intCounter mod ArrayLen(Colors))+1] & "'/>" &chr(10)> <cfset intCounter=intCounter+1> </cfloop> <!--- 'Entire XML - concatenation ---> <cfset strXMLData = strXMLData & "</graph>"> <cfoutput>#strXMLData#</cfoutput> |
In the above code, we're doing the following:
When you try viewing this page individually in the browser (in the format http://localhost/...Path.../DBExamples/CF/dataURL/Data.cfm?year=1996), you'll get the following XML output. You should always make sure, that while using dataURL method, your dataURL script should output just XML data and no HTML tags. |
<graph caption='Top 5 Countries for the Year
1996' shownames='1' showvalues='0' decimalPrecision='0' numberPrefix='$'> <set name='USA' value='38105.68' color='AFD8F8'/> <set name='Germany' value='35407.15' color='F6BD0F'/> <set name='Austria' value='25601.34' color='8BBA00'/> <set name='Brazil' value='20148.82' color='A66EDD'/> <set name='France' value='17372.76' color='F984A1'/> </graph> |
This is exactly the XML that we need for our 3D Column Chart. So, let's now shift our attention to Chart.cfm and see the code it contains to attain the following tasks:
Chart.cfm contains the following code: |
<HTML> <HEAD> <TITLE>FusionCharts</TITLE> <LINK REL='Stylesheet' HREF='../Style.css'></HEAD> <!---'Request the current year from querystring ---> <cfset curYear = Year> <!--- 'define the query object ---> <cfquery name="oRsYears" datasource="q01nw"> SELECT DISTINCT YEAR(OrderDate) As Year FROM Orders ORDER BY 1 </cfquery> <cfif curYear eq ""> <cfset curYear = oRsYears.Year> </cfif> <!--- 'Define dataURL ---> <cfset strDataURL = "Data.cfm?Year=" & curYear> <!--- 'URL Encode the dataURL - Important Step ---> <cfset strDataURL = URLEncodedFormat(strDataURL)> <table width="500" border="0" cellpadding="2" cellspacing="0" class="tableWithBorder" align='center'> <tr> <td colspan="3" class="trdark"> <div align="center"> <span class="textboldlight">FusionCharts</span> </div> </td> </tr> <cfoutput> <form action="Chart.cfm" method="post"> <tr> <td colspan="3" class="text" align="center">Please select the year for which you want to see the chart: <SELECT name='year' class='select' onChange="this.form.submit();"> <cfloop query="oRsYears"> <option value="#oRsYears.Year#" <cfif int(curYear) eq int(oRsYears.Year)>selected</cfif>> #oRsYears.Year# </option> </cfloop> </SELECT> </td> </tr> </form> </cfoutput> <tr> <td colspan="3"> </td> </tr> <tr> <td valign="top"> <table width="98%" border="0" cellspacing="0" cellpadding="2" align='center'> <tr> <td> <div align="center" class="text"> <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="450" HEIGHT="300" id="FusionCharts" ALIGN=""> <PARAM NAME="FlashVars" value="&dataURL=<cfoutput>#strDataURL#</cfoutput>" <!-- We specify the dataURL here --> <PARAM NAME=movie VALUE="../Charts/FC_2_3_Column3D.swf?chartWidth=450&chartHeight=300"> <PARAM NAME=quality VALUE=high> <EMBED src="../Charts/FC_2_3_Column3D.swf?chartWidth=450&chartHeight=300" FlashVars="&dataURL=<cfoutput>#strDataURL#</cfoutput>" quality=high bgcolor=#FFFFFF WIDTH="450" HEIGHT="300" NAME="FusionCharts" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED> </OBJECT> </div> </td> </tr> </table> </td> </tr> </table> <p align="center" class="text">©All Rights Reserved - InfoSoft Global Private Limited - 2005 - <a href="http://www.InfoSoftGlobal.com" target="_blank">www.InfoSoftGlobal.com</a> </p> </BODY> </html> |
Here, in the above code, we're doing the following:
When you now see Chart.cfm in your browser, you'll get the following output: |