This article will show how to create a mySQL driven line chart using Charts 4 PHP Framework.

Step 1: Configure Database connectivity in config.php

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

include("../../lib/inc/chartphp_dist.php");
$p = new chartphp();

Step 3: Configure Data array using jQuery to fetch data from Database

$p->data_sql = "select strftime('%Y-%m',o.orderdate) as Year, sum(d.quantity) as Sales 
                    from `order details` d, orders o 
                    where o.orderid = d.orderid 
                    group by strftime('%Y-%m',o.orderdate) limit 50";

Step 4: Set Chart type

$p->chart_type = "line";
$p->xlabel = "Month";
$p->ylabel = "Sales";
$p->shape = "linear";

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 3.3 / 5. Vote count: 24

No votes so far! Be the first to rate this post.

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!

Tell us how we can improve this post?