This article will show how to create a mySQL driven bar chart using Charts 4 PHP Framework.
Step 1: Configure Database connectivity in config.php
‘
'.wch_stripslashes('define("CHARTPHP_DBTYPE","pdo");
define("CHARTPHP_DBHOST","mysql:host=localhost;dbname=testdb");
define("CHARTPHP_DBUSER","username");
define("CHARTPHP_DBPASS","password");
define("CHARTPHP_DBNAME","");
').'
‘
Step 2: Include Charts 4 PHP Library
‘
'.wch_stripslashes('include("../../lib/inc/chartphp_dist.php");
$p = new chartphp();
').'
‘
Step 3: Configure Data array using jQuery to fetch data from Database
‘
'.wch_stripslashes('$p->data_sql = "select c.categoryname, sum(a.quantity) as Sales
from products b, `order details` a, categories c
where a.productid = b.productid and c.categoryid = b.categoryid
group by c.categoryid
order by c.categoryid";
').'
‘
Step 4: Set Chart type
‘
'.wch_stripslashes('$p->chart_type = "bar";
').'
‘
Step 5: Render Chart
‘
'.wch_stripslashes('$out = $p->render("c1");
').'
‘
For complete code, please visit live demo.