|
Tutorial for using AnyChart with PHPIf you want to use AnyChart with PHP you can start with downloading the following sample package: AnyChart with PHP Samples Package These are samples of AnyChart Flash Charts usage with PHP, please download the zip file, unpack it and follow the instructions in readme.txt All samples work as they are, but MySQL Sample requires MySQL configuration in PHP Code and attaching data to the database, please see "03_simple_chart_mysql/data.php" source code from the package for more. Below we will explain in details sample of usage AnyChart with PHP and MySQL Database (03_simple_chart_mysql), all other samples in package contain a lot of comments in source code and samples themselves are really simple and comprehensive. Simple Chart created using AnyChart and PHP
IntroductionAnyChart is a flexible component for creation charts in Web, you can easily create a lot of charts using it, PHP is one of the most server popular scripting engines, MySQL - is the most popular database to be used with PHP, so let's see step by step how to put them all together in a small sample. Package File StructureWhen you unpack anychart-php-samples.zip you will get the following file structure:
Sample File StructureOur point of interest is MySQL Sample, so we will focus only on two folders:
AnyChart Engine Filesanychart_files folder contains two folders and two files: anychart.swf and anychart.js. SWF file is a file that is used to render charts by a Flash Player on client-side, anychart.js is a JavaScript file that is used to make easier embedding of AnyChart Flash Charts into HTML page. JavaScript file is not mandatory, but it makes embedding much easier and is recommended. You don't have to understand what's inside these two files - they just have to be uploaded to your server to some folder of your choice. In this sample they are placed in anychart_files folder, please do not move them until you understand what you are doing. Sample File structure03_simple_chart_mysql contains 3 files: dump.sql, index.html and data.php, they are important, so we will talk about them in details: Sample Databasedump.sql file contains SQL commands to create table named anychart_sample_products and anychart_sample_orders in your database. These table are used in sample to show fetching data from MySQL and passing it to AnyChart. The structure and content of these tables:
NOTE: To make this sample work you should add these tables to some database in your MySQL Server. HTML Code Explainedindex.html is a simple HTML page, here is its code:
<html>
<head> <title>AnyChart Sample of Simple Using AnyChart with PHP and MySQL Database</title> <script type="text/javascript" language="javascript" src="../anychart_files/js/AnyChart.js"></script> </head> <body> <h1>AnyChart Sample of Simple Using AnyChart with PHP and MySQL Database</h1> <p>Please see data.php code to see how xml for AnyChart is created using PHP.</p> <div id="container"></div> <script type="text/javascript" language="javascript"> //<![CDATA[ var chart = new AnyChart('../anychart_files/swf/AnyChart.swf'); chart.setXMLFile('./data.php'); chart.write('container'); //]]> </script> </body> </html> What you should know about this code:
PHP Code Explaineddata.php is the PHP script that gets data from MySQL Database and converts it into XML format acceptable by AnyChart, this script shows only some basics (learn more about XML). To understand what exactly data.php does it is better to view its source code and read comments, we will highlight only main points.
FinalWhen configuring the sample it may be useful to launch data.php and look at its output, prior to trying to see the chart at index.html If you did everything right you will see data.php put like this: Click to view data.php output. And chart you will get will look like that: Click to view index.html. Please contact us at contact@anychart.com if you have any questions. Please note that this sample (and those provided in downloadable package) uses only some of AnyChart features. AnyChart provides a number of other chart types and options, which are described in evaluation download. To learn more about AnyChart, get User's Guide and XML Reference: Download Trial Buy Now |