Stock Chart allows you to use dynamic data. This means that the data will be generated at the moment when the chart accesses the data file. 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 data file names for every data set is set in settings file:
<data_sets> <data_set> ................................ <file_name>data1.csv</file_name> ................................ </data_set> <data_set> ................................ <file_name>data2.csv</file_name> ................................ </data_set>
You can replace data1.csv and data2.csv 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.
You can pass parameters to the data file. For example:
<file_name>data1.csv?stock=NASDAQ&start_date=1980-09-03</file_name>
We have now passed two variables to our data file: "stock" and "start_date". Now you need to extract them in your data file. Different programming languages use different syntax for extracting variables from the URL.
PHP: var $stock = $_GET["stock"]); ASP.NET (C#): string stock = Request.QueryString["stock"];
Since you know these variables, you can access certain tables in your database and output the required data.
Here is a simple example of a PHP script that receives stock name and start date via the query string, accesses the database, gets the required data and generates CSV data out of it. This database contains only one table with 3 fiels: date, value and volume.
<?php $host="localhost"; $user="root"; $password=""; $database="stocks"; // retrieves variables from a query string $stock = $_GET["stock"]; $start_date = $_GET["start_date"]; // connect to database mysql_connect($host, $user, $password) or die ('Unable to connect to server.'); // select database mysql_select_db($database) or die ('Unable to select database.'); // escape special characters in a string for use in a SQL statement (security) $stock = mysql_real_escape_string($stock); $start_date = mysql_real_escape_string($start_date); // select entries from table with the same name as stock charts name ($stock), and the dates that comes after $start_date only $query = "SELECT * FROM $stock WHERE date >= '$start_date'"; $res = mysql_query($query); // echo data while($obj = mysql_fetch_object($res)){ $date = $obj->date; $value = $obj->value; $volume = $obj->volume; echo "$date;$value;$volume\n"; } ?>
When you work with dynamic data, the easiest way to check whether your data code works properly is to open the file in the browser (it should be located in a web server). If you have bugs 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 CSV data there.
| © Antanas Marcelionis Contact and feedback: info@amcharts.com | Subscribe to amCharts news |