Using with Database and scripts > Overview |
FusionCharts is meant for plotting dynamic data on the web. It can plot charts with data stored in any form of data storage system. Since, FusionCharts doesn't directly interact with your data storage system, it doesn't matter what form of data storage you're using. That means, regardless of which app server and database you pair it with, FusionCharts will always be able to create data-driven interactive charts. Cruising through an example initially will make the picture much more clearer for you. Let's quickly get to it and see how the entire process can be put into action. |
An Example |
We'll quickly get into an example of creating
a database driven chart utilizing the Northwind database. The Northwind
database is the default database provided with MS SQL Server and MS Access.
This database contains the sales data for a fictitious company called Northwind
Traders, which imports and exports speciality foods from all around
the world.
Say, we wanted to plot a chart indicating the Top 5 countries,
for a particular year. The pertinent data to be extracted is stored in
a SQL Server View called Invoices. First, we need to query this view to
get this data. To do so, we utilize the following SQL query: |
SELECT TOP 5 Country, SUM(ExtendedPrice) As Total FROM Invoices WHERE YEAR(OrderDate)=1997 GROUP BY Country ORDER BY SUM(ExtendedPrice) DESC |
In the above code, we've temporarily set the year as 1997 to show the output. The query executes to give the following data: |
Now, that we've the data in a Recordset object for instance, our next job is to create an XML document out of this Recordset object, as FusionCharts accepts only XML as it's data source. |
<graph decimalPrecision='0' numberPrefix='$'> <set name='Germany' value='117320.16' color='0099FF'/> <set name='USA' value='114845.27' color='66CC66'/> <set name='Austria' value='57401.85' color='CD6AC0'/> <set name='France' value='45263.37' color='FF5904'/> <set name='Brazil' value='41941.19' color='996600'/> </graph> |
So, the task of converting the data in database to XML is to be performed by your server side scripts. The entire process can be visualized using the following diagram: |
As in the above diagram, your server side scripts (ASP.NET/ASP/PHP Pages/ColdFusion scripts etc) interact with the database using ODBC/OLE DB (or any other data stream) and generate a recordset/iterator object which contains the required data that is to be charted. Next, it converts the requested data into XML format (as required by FusionCharts) using either simple string concatenation or using the XML object and its builder properties. Simple string concatenation is a easy and preferable way to generate the XML document out of your data, as FusionCharts XML document has been kept as simple as possible eliminating the need of excessive branching. |
Generating the XML Document for the chart in your script |
The easiest and the most preferable method to generate the XML data document for FusionCharts is to use simple string concatenations. To make it very generic, the process of creating the XML document for a single series chart is shown in a general perspective i.e., not coded in any specific language. The entire sequence for a single series XML document generation can be simulated as under: |
(Initialize your page)
|
Methods of providing XML data to FusionCharts |
Now, that you've the XML data ready to be served, you need to know the ways to serve it to FusionCharts. FusionCharts can accept the XML data in two ways:
Both the above methods accept the same XML data document, but in different ways. Let's see each of them in detail. |
dataXML method |
dataXML method helps you embed the XML data
document in the same HTML (or ASP/ASP.NET/PHP/CF... ) page in which FusionCharts
is embedded. This method is used when the data document size isn't too big
(normally for single series charts). However, with larger data chunks, this
method starts behaving irregularly with older versions of browsers.
dataXML method is used as under: |
<HTML> <HEAD> ... </HEAD> <BODY bgcolor="#FFFFFF"> ... Other Content here ... <!-- Chart Object starts here --> <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="565" HEIGHT="420" id="FC_2_3_Column3D" ALIGN=""> <PARAM NAME=FlashVars VALUE="&dataXML=<graph decimalPrecision='0' numberPrefix='$'> <set name='Germany' value='117320.16' color='0099FF'/> <set name='USA' value='114845.27' color='66CC66'/> <set name='Austria' value='57401.85' color='CD6AC0'/> <set name='France' value='45263.37' color='FF5904'/> <set name='Brazil' value='41941.19' color='996600'/> </graph>"> <PARAM NAME=movie VALUE="FC_2_3_Column3D.swf"> <PARAM NAME=quality VALUE=high> <EMBED FlashVars="&dataXML=<graph decimalPrecision='0' numberPrefix='$'> <set name='Germany' value='117320.16' color='0099FF'/> <set name='USA' value='114845.27' color='66CC66'/> <set name='Austria' value='57401.85' color='CD6AC0'/> <set name='France' value='45263.37' color='FF5904'/> <set name='Brazil' value='41941.19' color='996600'/> </graph>" src="FC_2_3_Column3D.swf" quality=high WIDTH="565" HEIGHT="420" NAME="FC_2_3_Column3D" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED> </OBJECT> <!-- Chart Object Ends here --> ... Other Content here ... </BODY> </HTML> |
As you can see above, the entire XML data document
is present inside the HTML page alongside the FusionCharts Flash object.
We use the FlashVars property of the flash
object to specify the XML data under dataXML
parameter.
Note: The FlashVars must be passed in both Param tag (for Internet Explorer-based browsers) as well as the Embed tag (for Netscape/Mozilla-based browsers); any non-URL safe characters, such as spaces, must be replaced with a % sign followed by a two-digit hexadecimal value (like %20 in place of a space, %25 in place of a percentage or %26 in place of an ampersand). Let's now quickly see how to mould your server side script to use FusionCharts
using the dataXML method. |
dataXML and your server side script |
Step 1:
Initialize your page
Step 5: Render other contents of
the page |
So, in the above code, when your page load event or data binding event is invoked, strFCXMLDoc in both instances is replaced by the actual XML data generated by your server. You'll get the following chart output for the above code (when run against Northwind database): |
Next, we'll see the other method - dataURL method. |
dataURL Method |
Using this method, you just provide the URL (Uniform Resource Locator) of the XML document (or the server side script which would provide the XML document). The following diagram would assist you understand better: |
As visible in the above diagram, two pages are
needed to create a chart using this method:
|
dataURL method and your server side script |
Let's quickly modify our "Top 5 Countries"
example above to create a dynamic chart, wherein the user could select the
year for which he wants to view the chart. For this example, there would
be 2 pages:
Chart.ext can be coded under the following guidelines: |
(Initialize your page)
(Render other Content) |
And, Data.ext should function as below: |
(Initialize your page)
Make sure that in this page, you do not output anything apart from the XML document - that is no HTML content should be present in the document. And, the XML output should be a valid XML structure. |
And, when you now see the chart, you'll get the required output. Here, we've seen a very generalized and conceptual explanation of using FusionCharts with a database. The next few sections span into details of using FusionCharts with your server side script and database. Based on the server side script (ASP, ASP.NET, CF, PHP etc.) that you're using, you can directly jump to the pertinent section next. |