Back to amCharts.com

Dynamic Data

AmCharts allow you to use dynamic data. This means that the data will be generated at the moment when you load the page. The data generation is done by an external script - PHP, ASP, .NET, Perl, Ruby on Rails or any other Web programming environment or Ajax data source.

You will need some knowledge of programming to create the data-generating script. Because the script would be specific to your particular task, it is impossible to provide a tutorial for creating it. This chapter contains tips on attaching the data script to the chart, and passing parameters to the script in the URL.

The HTML code for the chart contains the following line:

so.addVariable("data_file", escape("some_data_file.xml"));

You can replace some_data_file.xml with a different URL. This can be a PHP file, or any other online file. It can even contain HTTP parameters. As long as the software can call that URL, and get back the data in an expected format (XML or CSV), the chart will work.

Parameters in the URL

You can pass parameters to the data file. For example:

so.addVariable("data_file", escape("amline/amline_data.php?chart_id=1&city_id=2"));

We have now passed two variables to our data file: chart_id and city_id. Now you need to extract them in your data file. Different programming languages use different syntax for extracting variables from the URL.

PHP: var $title= $_GET["title"]); ASP.NET (C#): string title = Request.QueryString["title"];

Since you know these variables, you can access certain tables in your database and output the required data.

Examples

Here is a simple example of a PHP script that accesses the database, gets the data and generates CSV data out of it. This database contains only one table with 3 fiels: date, visits and page_views.

<?php
 
$host="localhost";
$user="root";
$password="";
$database="stats";
 
// connect to database
mysql_connect($host, $user, $password)
or die ('Unable to connect to server.');
 
mysql_select_db($database)
or die ('Unable to select database.');
 
// select last 20 entries
$query = "SELECT * FROM qstats ORDER BY date DESC LIMIT 20";
$res = mysql_query($query);
 
 
// echo data
while($obj = mysql_fetch_object($res)){  
  $date = $obj->date;
  $visits =  $obj->value;
  $page_views =  $obj->volume;
 
  echo "$date;$visits;$page_views\n";
} 
?>

Another example with the same database, but this script generates XML data:

<?php
header('Content-Type: text/xml');
 
$host="localhost";
$user="root";
$password="";
$database="stats";
 
// connect to database
mysql_connect($host, $user, $password)
or die ('Unable to connect to server.');
 
mysql_select_db($database)
or die ('Unable to select database.');
 
// select last 20 entries
$query = "SELECT * FROM qstats ORDER BY date DESC LIMIT 20";
$res = mysql_query($query);
 
// put all the data inside an array
$arr = array();
while($obj = mysql_fetch_object($res)){
  $arr[] = $obj;
}
 
// echo xml
echo "<"."?xml version=\"1.0\" encoding=\"UTF-8\"?".">\n";
echo "<chart>\n";
echo "<series>\n";
 
// echo series
for ($i = sizeof($arr) - 1; $i > 0; $i--) {  
  $date = $arr[$i]->date; 
  echo "<value xid=\"$i\">$date</value>\n";
}
echo "</series>\n";
 
// echo graphs
echo "<graphs>\n";
// first graph
echo "<graph title=\"visits\" gid=\"0\">\n";
for ($i = sizeof($arr) - 1; $i > 0; $i--) {  
  $visits = $arr[$i]->visits; 
  echo "<value xid=\"$i\">$visits</value>\n";
}
echo "</graph>\n";
 
// second graph
echo "<graph title=\"page views\" gid=\"1\">\n";
for ($i = sizeof($arr) - 1; $i > 0; $i--) {  
  $page_views = $arr[$i]->page_views; 
  echo "<value xid=\"$i\">$page_views</value>\n";
}
echo "</graph>\n";
echo "</graphs>\n";
echo "</chart>";
 
?>

Checking the data file

When you work with dynamic data, the most easy way to check whether your data code doesn't have mistakes is this: simply point the browser to this file (it should be located in a web server). If you have programming mistakes in your code, you will see them displayed. If there are no mistakes, try to view the source of the page. You should see the proper XML or CSV data there.

AmCharts-PHP Class Library

Good people from Fusonic wrote AmCharts-PHP Class Library, which is published under the GNU Lesser General Public Licence. You are welcome to use this library.

Back to top