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


Step 2: Include Charts 4 PHP Library

$p = new chartphp();

Step 3: Configure Data array using jQuery to fetch data from Database. The first column selected will serve as x-axis. Second and onwards will become y-axis traces.

$p->data_sql = "select c.categoryname, sum(a.quantity) as 'Sales 1997', sum(a.quantity)+1000 as 'Sales 1998'
                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

$p->chart_type = "bar";

Step 5: Render Chart

$out = $p->render("c1");

For complete code, please visit live demo.

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!