1

I have a database like the attached image.enter image description here

By that table I need to draw a graph like thisenter image description here

How to do that? I have tried with the following code:

function get_number($hour){
$sql = "SELECT count(POPUP)+count(`DETAIL`)+count(`DETAIL`)+count(`CALL`)+count(`MESSAGE`)+count(`ROUTE`)+count(`SHARE`)+count(`MAP`)+count(`INFO`)+ count(`WEBSITE`) as cp
FROM campaign_logs WHERE EXTRACT(HOUR FROM logDate)=".$hour;    
    $res = mysql_fetch_array(mysql_query($sql));
    return $res['cp'];
}

$sql = "SELECT count(POPUP)+count(`DETAIL`)+count(`DETAIL`)+count(`CALL`)+count(`MESSAGE`)+count(`ROUTE`)+count(`SHARE`)+count(`MAP`)+count(`INFO`)+ count(`WEBSITE`) as cp,EXTRACT(HOUR FROM logDate) as h
FROM campaign_logs GROUP BY EXTRACT(HOUR FROM logDate)";
$res = mysql_query($sql);

$values=array(
"1" => get_number(1),
"2" => get_number(2),
"3" => get_number(3),
"4" => get_number(4),
"5" => get_number(5),
"6" => get_number(6),
"7" => get_number(7),
"8" => get_number(8),
"9" => get_number(9),
"10" => get_number(10),
"11" => get_number(11),
"12" => get_number(12),
"13" => get_number(13),
"14" => get_number(14),
"15" => get_number(15),
"16" => get_number(16),
"17" => get_number(17),
"18" => get_number(18),
"19" => get_number(19),
"20" => get_number(20),
"21" => get_number(21),
"22" => get_number(22),
"23" => get_number(23),
"24" => get_number(24)
);


$img_width=600;
$img_height=400; 
$margins=20;
$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2; 
$img=imagecreate($img_width,$img_height);
$bar_width=20;
$total_bars=count($values);
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1); 
$bar_color=imagecolorallocate($img,0,64,128);
$background_color=imagecolorallocate($img,240,240,255);
$border_color=imagecolorallocate($img,200,200,200);
$line_color=imagecolorallocate($img,220,220,220); 
imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color); 
$max_value=max($values);
$ratio= $graph_height/$max_value; 
$horizontal_lines=20;
$horizontal_gap=$graph_height/$horizontal_lines;
for($i=1;$i<=$horizontal_lines;$i++){
$y=$img_height - $margins - $horizontal_gap * $i ;
imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
$v=intval($horizontal_gap * $i /$ratio);
imagestring($img,0,5,$y-5,$v,$bar_color);
}
for($i=0;$i< $total_bars; $i++){
list($key,$value)=each($values);
$x1= $margins + $gap + $i * ($gap+$bar_width) ;
$x2= $x1 + $bar_width;
$y1=$margins +$graph_height- intval($value * $ratio) ;
$y2=$img_height-$margins;
imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);
} 

header("Content-type:image/png");
imagepng($img); 

But it is something different. It is calculating not separately. Please help me.

2 Answers 2

2

Well i will reccomend you Am Charts. The best!

Am Charts

You can use json_encode to convert your query result so that javascript can read it.

Sign up to request clarification or add additional context in comments.

Comments

1

There is a Jquery plugin called Highcharts. You can use this. Please refer the below link

http://www.highcharts.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.