FusionCharts and PHP > Using dataXML method |
Previously, we had seen how to use the dataURL method to create a dynamic column chart using FusionCharts. Here, we'll be creating the same chart but using dataXML method. As we had earlier seen, the dataXML method just requires one page:
The above page is present in the downloads under the folder FusionCharts2_3 > Sample Code > DBExamples > PHP > dataXML. We'll also need two other pages:
The above two include files are present under FusionCharts2_3 > Sample Code > DBExamples > PHP folder. FC_Colors.php looks as under: |
<?php |
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. |
Connection_ini.php is the page which contains the database connection details. When you run the code on your machine, after downloading the package, you should ensure that this page is updated so as to reflect the connection details to your SQL server. This page looks as under: |
<?php |
The page Chart.php is responsible for everything - rendering the year down box, getting the selected year, creating the XML data for that year and then rendering the chart for that year. The code for this page can be reproduced as under: |
<HTML> <HEAD> <TITLE>FusionCharts - dataXML Example</TITLE> <LINK REL='Stylesheet' HREF='../Style.css'> </HEAD> <?php //Include connection and colors file include '../Connection_ini.php'; include '../FC_Colors.php'; //Connect to the ODBC Source $sqlconnect=odbc_connect($dsn,$username,$password); //Generate the query to get a list of unique years $strSQL="SELECT DISTINCT YEAR(OrderDate) As Year FROM Orders ORDER BY 1"; //Get
the list of years |
In the above code, we're doing the following:
When you now see Chart.php in your browser, you'll get the following output: |