Tuesday, July 10, 2012

Pie Chart php MySQL


Pie Chart php MySQL

Previous tutorial we were discussed to create line graph and bar graph in MySQL.For creating pie chart also,i have used PHPGraphLib file where new version is available.you can use new version click here to download pakage comes with 3 files 

For this tutorial we need to have phpgraphlib.php and phpgraphlib_pie.php
This tutorial also i have taken progress1 table 

Car production in year-2012

Following files were created 
pie.html
pie.php


pie.html
<html>
<h3>Car production in year-2012</h3>
<table><tr>
<td><img src="pie.php" /></td></tr>
</table>
</html>
pie.php
<?php
include('phpgraphlib.php');
include('phpgraphlib_pie.php');
$graph = new PHPGraphLibPie(400, 200);
$link = mysql_connect('localhost','root', '', '')
   or die('Could not connect: ' . mysql_error());
  
mysql_select_db('my_database') or die('Could not select database');

$dataArray=array();


$sql="SELECT * FROM progress1";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
  while ($row = mysql_fetch_assoc($result)) {
      $month=$row["Month"];

if ($month == "1") {
$month_name = "January";
} else if ($month == "2"){
$month_name = "February";
} else if ($month == "3"){
$month_name = "March";
} else if ($month == "4"){
$month_name = "April";
}else if ($month == "5"){
$month_name = "May";
} else if ($month == "6"){
$month_name = "June";
} else if ($month == "7"){
$month_name = "July";
}else if ($month == "8"){
$month_name = "August";
} else if ($month == "9"){
$month_name = "September";
} else if ($month == "10"){
$month_name = "October";
}else if ($month == "11"){
$month_name = "November";
} else if ($month == "12"){
$month_name = "December";
}
      $Production=$row["Production"];
 
      $dataArray[$month_name]=$Production;
  }
}

$graph->addData($dataArray);
$graph->setTitle('Car Production percentage Year-2012');
$graph->setLabelTextColor('50,50,50');
$graph->setLegendTextColor('50,50,50');
$graph->createGraph();
?>

3 comments:

  1. pie chart not working image not showing

    ReplyDelete
    Replies
    1. Make sure you have downloaded phpgraphlib.php and phpgraphlib_pie.php and also make mysql table indicated here as progress1.I have rechecked the script.it woks well.

      Delete
  2. shows as it contains error

    ReplyDelete