在php和mysql中创建图表 [英] create graph in php and mysql

查看:171
本文介绍了在php和mysql中创建图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,如附图所示。



通过该表,我需要画出这样的图 p>

如何做到这一点?我已经尝试了以下代码:

$ p $ function get_number($ hour){
$ sql =SELECT count( POPUP)+计数(`DETAIL`)+计数(`DETAIL`)+计数(`CALL`)+计数(`MESSAGE`)+计数(`ROUTE`)+计数(`SHARE`)+计数(`MAP` )+ count(`INFO`)+ count(`WEBSITE`)作为cp
FROM campaign_logs WHERE EXTRACT(HOUR FROM logDate)=。$ hour;
$ res = mysql_fetch_array(mysql_query($ sql));
return $ res ['cp']; ('DETAIL')+ count('DETAIL`)+ count('CALL`)+ count('MESSAGE')。 + count(`ROUTE`)+ count(`SHARE`)+ count(`MAP`)+ count(`INFO`)+ count(`WEBSITE`)为cp,EXTRACT(HOUR FROM logDate)为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;
$ margin = 20;
$ graph_width = $ img_width - $ margin * 2;
$ graph_height = $ img_height - $ margin * 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,$ margin,$ margin,$ img_width-1- $ margin,$ img_height-1- $ margin,$ background_color);
$ max_value = max($ values);
$ ratio = $ graph_height / $ max_value;
$ horizo​​ntal_lines = 20;
$ horizo​​ntal_gap = $ graph_height / $ horizo​​ntal_lines;
($ i = 1; $ i <= $ horizo​​ntal_lines; $ i ++){
$ y = $ img_height - $ margin - $ horizo​​ntal_gap * $ i;
imageline($ img,$ margin,$ y,$ img_width- $ margin,$ y,$ line_color);
$ v = intval($ horizo​​ntal_gap * $ i / $ ratio);
imagestring($ img,0,5,$ y-5,$ v,$ bar_color); ($ i = 0; $ i <$ total_bars; $ i ++){
list($ key,$ value)= each($ values);
}
;
$ x1 = $ margin + $ gap + $ i *($ gap + $ bar_width);
$ x2 = $ x1 + $ bar_width;
$ y1 = $ margin + $ graph_height- intval($ value * $ ratio);
$ y2 = $ img_height- $边际;
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);

但它有些不同。它不是分开计算。
请帮助我。

解决方案

有一个叫做Highcharts的Jquery插件。你可以使用这个。请参阅下面的链接



http://www.highcharts。 com /


I have a database like the attached image.

By that table I need to draw a graph like this

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.

解决方案

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

http://www.highcharts.com/

这篇关于在php和mysql中创建图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆