2013-01-15 83 views
0

我有一個類似於附加圖像的數據庫。 enter image description here在php和mysql中創建圖表

到那個表我要畫這樣enter image description here

圖怎麼做呢?我試着用下面的代碼:

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); 

但它有些不同。它不是分開計算。 請幫幫我。

回答

2

嗯,我會reccomend你難道圖表。最好的!

Am Charts

您可以使用json_encode轉換您的查詢結果,使JavaScript可以讀取它。