Friday, December 2, 2016

Pie Chart Using Google Chart


///////////////////////////////////////////////////////////////////////////////////////////////////
//// index.php
///////////////////////////////////////////////////////////////////////////////////////////////////
<?php
include("pdo.php");
$obj=new DB;
$row=$obj->geTable();

?>

<html>
  <head>
    <script type="text/javascript" src="loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);
   
 function drawChart() {
      new google.visualization.PieChart(document.getElementById('routinechart')).draw(google.visualization.arrayToDataTable(<?php echo $row?>), {
            'legend':'center',
            'title':'360 Spectrum of my routine!',
'colors': ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6'],
                     'width':900,
                     'height':900,
'is3D': true});
      }
    </script>
  </head>
  <body>
    <div id="routinechart" style="width: 900px; height: 900px;"></div>
  </body>
</html>
///////////////////////////////////////////////////////////////////////////////////////////////////
//// End index.php
///////////////////////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////////////////////
//// pdo.php
///////////////////////////////////////////////////////////////////////////////////////////////////
<?php
//connecting database using pdo
//$obj=new DB;
//$obj->geTable1();
//$obj->geTable();
//print_r($x);
class DB{

private $hostname;
private $username;
private $password;
private $dbname;

      function __construct(){
   $this->hostname='localhost';
$this->username='root';
$this->password='';
$this->dbname="chart";
 }
/* function geTable1(){
echo "good to go";
}  
*/function geTable(){
     $this->connect();
     $dbh=$this->dbh;
     $sql = "SELECT * FROM activity";
$res[]=array("Task", "Hours per Day");
foreach ($dbh->query($sql) as $row)
    {
//$res[]=$row["task"]." , ". $row["hourperday"];
//["Work , 11","Eat , 2","Commute , 3","Watch TV , 4","Walk , 2","Teach , 3","Sleep , 3"]DT634ER6 T
   // echo $row["task"] ." - ". $row["hourperday"] ."<br/>";
 //  ["Work,11","Eat,2","Commute,3","Watch TV,4","Walk,2","Teach,3","Sleep,3"]
   
    //['Task', 'Hours per Day'],['Work',     11],
 $res[]=array($row["task"],$row["hourperday"]);
    }
$x=json_encode($res,JSON_NUMERIC_CHECK);
//print_r($x);
return $x;

}
function connect(){
try {
    $dbh = new PDO("mysql:host=$this->hostname;dbname=$this->dbname",$this->username,$this->password);

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
    //echo 'Connected to Database<br/>';
   
    $this->dbh=$dbh;
    $dbh = null;
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
}
}
?> 

///////////////////////////////////////////////////////////////////////////////////////////////////
//// End pdo.php
///////////////////////////////////////////////////////////////////////////////////////////////////

No comments:

Post a Comment