Charts 4 PHP - How-To Guides

HTML5 powered PHP Charts for Web and Mobile devices

Creating a Bar Chart using mySQL in PHP

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 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 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 $p->chart_type = "bar"; Step 5: Render Chart $out = $p->render("c1"); For complete code, please visit live...

Creating Dashboard with Charts 4 PHP

This article will show how to create a dashboard using Charts 4 PHP Framework with or without database (mySQL, Oracle, PDO etc.) at back end. I. Creating first chart in Dashboard Step 1: Configure Database connectivity in config.php define("CHARTPHP_DBTYPE","pdo"); define("CHARTPHP_DBHOST","sqlite:../../sampledb/Northwind.db"); define("CHARTPHP_DBUSER",""); define("CHARTPHP_DBPASS",""); define("CHARTPHP_DBNAME",""); Step 2: Include Charts 4 PHP Library in Dashboard HTML include("../../lib/inc/chartphp_dist.php"); $p = new chartphp(); Step 3: Select any one of the substep below to configure Data Array Step 3a: Configure Data array $p->data = array(array( array('Heavy Industry', 12), array('Retail', 9), array('Light Industry', 14), array('Out of home', 16), array('Commuting', 7), array('Orientation', 9))); Step 3b: Configure Data array using jQuery to fetch data from Database (e.g. for SQLite DB) $p->data_sql = "select c.categoryname, sum(a.UnitPrice * 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 $p->chart_type = "pie"; Step 5: Render Chart $out = $p->render("c1"); This will give you a single chart in your dashboard something like this. II. Creating multiple charts in your Dashboard Step 1: Configure multiple charts into your Dashboard HTML $p = new chartphp(); $p->data = array(array(array("2010/10",48.25),array("2011/01",238.75),array("2011/02",95.50),array("2011/03",300.50),array("2011/04",286.80),array("2011/05",400))); $p->chart_type = "bar"; $out_bar = $p->render('c2'); $p = new chartphp(); $p->data = array(array(array(60, 123, 1067, "Acura"), array(11, 92, 1236, "Alfa Romeo"), array(30, 200, 610, "AM General"), array(50, 23, 800, "Aston"), array(18, 17, 539, "Audi"), array(50, 89, 1026, "BMW"), array(99, 13, 864, "Bugatti"))); $p->chart_type = "bubble"; $out_bubble = $p->render('c3'); $p = new chartphp(); $p->intervals = array(200,300,400,600); $p->chart_type = "meter"; $p->width = "200px"; $p->height = "150px"; $p->data = array(array(rand(200,600))); $out_meter = $p->render('c4'); $p->data = array(array(rand(200,600))); $out_meter2 = $p->render('c41'); $p->data = array(array(rand(200,600))); $out_meter3 = $p->render('c42'); $p->data = array(array(rand(200,600))); $out_meter4 = $p->render('c43'); Step 2: Render multiple charts in your dashboard to get the final look like this:   For complete...

Creating a Line Chart using mySQL in PHP

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"; Step 5: Render Chart $out = $p->render("c1"); For complete code, please visit live...

Creating a Funnel Chart using PHP

This article will show how to create a simple funnel chart using Charts 4 PHP Framework. Step 1: Include Charts 4 PHP Library include("../../lib/inc/chartphp_dist.php"); $p = new chartphp(); Step 2: Configure Data array along with Intervals array $p->data = array(array( array('Heavy Industry', 12), array('Retail', 9), array('Light Industry', 14), array('Out of home', 16), array('Commuting', 7), array('Orientation', 9) )); Step 3: Set Chart type $p->chart_type = "funnel"; Step 4: Render Chart $out = $p->render("c1"); For complete code, please visit live...

Creating a Meter Chart using PHP

This article will show how to create a simple meter chart using Charts 4 PHP Framework. Step 1: Include Charts 4 PHP Library include("../../lib/inc/chartphp_dist.php"); $p = new chartphp(); Step 2: Configure Data array along with Intervals array $p->data = array(array(266)); $p->intervals = array(200,300,400,600); Step 3: Set Chart type $p->chart_type = "meter"; Step 4: Render Chart $out = $p->render("c1"); For complete code, please visit live...

Creating a Pie Chart using PHP

This article will show how to create a simple pie chart using Charts 4 PHP Framework. Step 1: Include Charts 4 PHP Library include("../../lib/inc/chartphp_dist.php"); $p = new chartphp(); Step 2: Configure Data array $p->data = array(array( array('Heavy Industry', 12), array('Retail', 9), array('Light Industry', 14), array('Out of home', 16), array('Commuting', 7), array('Orientation', 9) )); Step 3: Set Chart type $p->chart_type = "pie"; Step 4: Render Chart $out = $p->render("c1"); For complete code, please visit live...

Creating a Donut Chart using PHP

This article will show how to create a simple donut chart using Charts 4 PHP Framework. Step 1: Include Charts 4 PHP Library include("../../lib/inc/chartphp_dist.php"); $p = new chartphp(); Step 2: Configure Data array $p->data = array(array(array('a',6), array('b',8), array('c',14), array('d',20))); Step 3: Set Chart type $p->chart_type = "donut"; Step 4: Render Chart $out = $p->render("c1"); For complete code, please visit live...

Creating a Bubble Chart using PHP

This article will show how to create a simple bubble chart using Charts 4 PHP Framework. Step 1: Include Charts 4 PHP Library include("../../lib/inc/chartphp_dist.php"); $p = new chartphp(); Step 2: Configure Data array $p->data = array(array( array(11, 123, 1236, "Acura"), array(45, 92, 1067, "Alfa Romeo"), array(24, 104, 1176, "AM General"), array(50, 23, 610, "Aston Martin Lagonda"), array(18, 17, 539, "Audi"), array(7, 89, 864, "BMW"), array(2, 13, 1026, "Bugatti") )); Step 3: Set Chart type $p->chart_type = "bubble"; Step 4: Render Chart $out = $p->render("c1"); For complete code, please visit live...

Creating a Bar Chart using PHP

This article will show how to create a simple bar chart using Charts 4 PHP Framework. Step 1: Include Charts 4 PHP Library include("../../lib/inc/chartphp_dist.php"); $p = new chartphp(); Step 2: Configure Data array $p->data = array(array( array("2010/10",48.25), array("2011/01",238.75), array("2011/02",95.50), array("2011/03",300.50), array("2011/04",286.80), array("2011/05",400) )); Step 3: Set Chart type $p->chart_type = "bar"; Step 4: Render Chart $out = $p->render("c1"); For complete code, please visit live...

Creating an Area Chart using PHP

This article will show how to create a simple area chart using Charts 4 PHP Framework. Step 1: Include Charts 4 PHP Library include("../../lib/inc/chartphp_dist.php"); $p = new chartphp(); Step 2: Configure Data array $p->data = array(array(3,7,9,1,4,6,8,2,5),array(5,3,8,2,6,2,9,2,6)); Step 3: Set Chart type $p->chart_type = "area"; Step 4: Render Chart $out = $p->render("c1"); For complete code, please visit live...

Creating a Line Chart using PHP

This article will show how to create a simple line chart using Charts 4 PHP Framework. Step 1: Include Charts 4 PHP Library include("../../lib/inc/chartphp_dist.php"); $p = new chartphp(); Step 2: Configure Data array $p->data = array(array(3,7,9,1,4,6,8,2,5),array(5,3,8,2,6,2,9,2,6)); Step 3: Set Chart type $p->chart_type = "line"; Step 4: Render Chart $out = $p->render("c1"); For complete code, please visit live...