使用php从Mysql表显示图形
如何使用PHP图像函数动态显示MySQL表中存储的数据统计信息?我能够在静态表上使用计数查询以条形图的形式显示计数,但如图所示。我有一个动态存储服务器日志的数据库使用php从Mysql表显示图形,php,mysql,image,dynamic-data,Php,Mysql,Image,Dynamic Data,如何使用PHP图像函数动态显示MySQL表中存储的数据统计信息?我能够在静态表上使用计数查询以条形图的形式显示计数,但如图所示。我有一个动态存储服务器日志的数据库 该图像是由3个表和php的image&draw函数生成的我不知道您是如何尝试的。但是有一个很好的Jquery插件叫做Highcharts。您可以使用它创建各种类型的图形。如果您感兴趣,请参考下面的链接 Rgraph也是一个非常好的HTML5/Javascript图形库,易于使用,并且有很多选项 这就是如何添加PHP数组的方法,$ave
该图像是由3个表和php的image&draw函数生成的我不知道您是如何尝试的。但是有一个很好的Jquery插件叫做Highcharts。您可以使用它创建各种类型的图形。如果您感兴趣,请参考下面的链接
Rgraph也是一个非常好的HTML5/Javascript图形库,易于使用,并且有很多选项
这就是如何添加PHP数组的方法,$average是带有数字的数组,在本例中是雷达图。内爆函数在数组的值之间加一个逗号。我用下面的代码用php创建了一个简单的条形图。 您必须从数据库中获取bar值并将其放入$data数组中。 接下来必须为图像大小设置$height和$width。 根据您的图像大小,您必须定位聊天
<?php
$data = array('400', '2570', '245', '473', '1000', '3456', '780', '5000', '30', '420');
$sum = array_sum($data);
$height = 480;
$width = 640;
$im = imagecreate($width, $height);
$background = imagecolorallocate($im, 255, 255, 255);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0);
$green = imagecolorallocate($im, 51, 153, 0);
$yellow = imagecolorallocate($im, 255, 255, 0);
imageline($im, 10, 5, 10, $height - 20, $black);
imageline($im, 10, $height - 20, 620, $height - 20, $black);
header("Content-type: image/png");
$x = 11;
$y = 459;
$x_width = 20;
$y_ht = 0;
$max_i = count($data);
for ($i = 0; $i < $max_i; $i++) {
$y_ht = ($data[$i] / $sum) * $height;
if ($data[$i] > 1000) {
imagefilledrectangle($im, $x, $y, $x + $x_width, ($y - $y_ht), $green);
} else if ($data[$i] > 500) {
imagefilledrectangle($im, $x, $y, $x + $x_width, ($y - $y_ht), $yellow);
} else {
imagefilledrectangle($im, $x, $y, $x + $x_width, ($y - $y_ht), $red);
}
imagestring($im, 2, $x - 1, ($y - $y_ht - 15), $data[$i], $black);
$x += ($x_width + 2);
}
imagepng($im);
imagedestroy($im);
?>
参考:
<?php
$data = array('400', '2570', '245', '473', '1000', '3456', '780', '5000', '30', '420');
$sum = array_sum($data);
$height = 480;
$width = 640;
$im = imagecreate($width, $height);
$background = imagecolorallocate($im, 255, 255, 255);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0);
$green = imagecolorallocate($im, 51, 153, 0);
$yellow = imagecolorallocate($im, 255, 255, 0);
imageline($im, 10, 5, 10, $height - 20, $black);
imageline($im, 10, $height - 20, 620, $height - 20, $black);
header("Content-type: image/png");
$x = 11;
$y = 459;
$x_width = 20;
$y_ht = 0;
$max_i = count($data);
for ($i = 0; $i < $max_i; $i++) {
$y_ht = ($data[$i] / $sum) * $height;
if ($data[$i] > 1000) {
imagefilledrectangle($im, $x, $y, $x + $x_width, ($y - $y_ht), $green);
} else if ($data[$i] > 500) {
imagefilledrectangle($im, $x, $y, $x + $x_width, ($y - $y_ht), $yellow);
} else {
imagefilledrectangle($im, $x, $y, $x + $x_width, ($y - $y_ht), $red);
}
imagestring($im, 2, $x - 1, ($y - $y_ht - 15), $data[$i], $black);
$x += ($x_width + 2);
}
imagepng($im);
imagedestroy($im);
?>