Monday, November 7, 2016

Bar Chart using jpgraph,PHP and MySQL.

///////////////////////////////////////////////////////////////////////////////////////////////////
//// index.php
///////////////////////////////////////////////////////////////////////////////////////////////////
<?php // content="text/plain; charset=utf-8"
//
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');

include("db.php");
$obj=new DB;
$sql="select * from icecream";
$obj->getSqlTable($sql);
$products=$obj->arr;
foreach($products as $product){
$montharr[]=$product['month'];
$week1arr[]=$product['week1'];
$week2arr[]=$product['week2'];
$week3arr[]=$product['week3'];
$week4arr[]=$product['week4'];
}

$data1y=$week1arr;
$data2y=$week2arr;
$data3y=$week3arr;
$data4y=$week4arr;

// Create the graph. These two calls are always required
$graph = new Graph(500,350,'auto');
$graph->SetScale("textlin");

$theme_class=new UniversalTheme;
$graph->SetTheme($theme_class);

//$graph->yaxis->SetTickPositions(array(0,30,60,90,120,150), array(15,45,75,105,135));
$graph->SetBox(false);

$graph->ygrid->SetFill(false);
//$graph->xaxis->SetTickLabels(array('Jan','Feb','March','April','May'));
$graph->xaxis->SetTickLabels($montharr);  // 12345
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);

// Create the bar plots
$b1plot = new BarPlot($data1y);
$b2plot = new BarPlot($data2y);
$b3plot = new BarPlot($data3y);
$b4plot = new BarPlot($data4y);

// Create the grouped bar plot
$gbplot = new GroupBarPlot(array($b1plot,$b2plot,$b3plot,$b4plot));
// ...and add it to the graPH
$graph->Add($gbplot);


$b1plot->SetColor("white");
$b1plot->SetFillColor("#cc1111");

$b2plot->SetColor("white");
$b2plot->SetFillColor("#11cccc");

$b3plot->SetColor("white");
$b3plot->SetFillColor("#1111cc");

$graph->title->Set("Bar Plots");

// Display the graph
$graph->Stroke();
?>
///////////////////////////////////////////////////////////////////////////////////////////////////
//// End index.php
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
//// update.php
///////////////////////////////////////////////////////////////////////////////////////////////////
<?php


if(isset($_REQUEST['fetchdata'])){
include("db.php");
$obj=new DB;
$sql="select * from icecream";
$obj->getSqlTable($sql);
$products=$obj->arr;


//echo'<pre>';
//print_r($products as $product);
foreach($products as $product){
$montharr[]=$product['month'];
$week1arr[]=$product['week1'];
$week2arr[]=$product['week2'];
$week3arr[]=$product['week3'];
$week4arr[]=$product['week4'];
}
print_r($montharr);
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//// End update.php
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
//// db.php
///////////////////////////////////////////////////////////////////////////////////////////////////
<?php

  class DB{
     private $dbhost = "localhost";
     private $dbuser = "root";
     private $dbpass = "";
     private $dbname = "abc";
public $error='';
/**********************************************
  get sql connection
***********************************************/
public function getCon(){
 $con=mysql_connect($this->dbhost,$this->dbuser,$this->dbpass);
 if(!$con){
 die("cant connect to db");
 }
 mysql_select_db($this->dbname, $con);
 return $con;

}
/**********************************************
get sql row from db
***********************************************/
function getSqlRow($sql){
 $arr=array();
 $con=$this->getCon();
 $result=mysql_query($sql, $con);
 $row=mysql_fetch_array($result);
 mysql_close($con);
      return $row;
}
/**********************************************
get sql table from db
***********************************************/
function getSqlTable($sql){
 $arr=array();
 $con=$this->getCon();
 $result=mysql_query($sql, $con);
 while($row=mysql_fetch_assoc($result)){
 $arr[]=$row;
 }
 mysql_close($con);
     $this->arr=$arr;
}


    /**********************************************
***********************************************/
}
?>
///////////////////////////////////////////////////////////////////////////////////////////////////
//// End db.php
///////////////////////////////////////////////////////////////////////////////////////////////////

No comments:

Post a Comment