PHP生成條形圖的方法
更新時間:2014年12月10日 14:30:26 投稿:shichen2014
這篇文章主要介紹了PHP生成條形圖的方法,可實現(xiàn)生成柱狀的條形圖,適用于一些類似柱狀圖顯示報表的場合,具有一定的實用價值,需要的朋友可以參考下
本文實例講述了PHP生成條形圖的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
<?php
// create an array of values for the chart. These values
// could come from anywhere, POST, GET, database etc.
$values = array(23,32,35,57,12,3,36,54,32,15,43,24,30);
// now we get the number of values in the array. this will
// tell us how many columns to plot
$columns = count($values);
// set the height and width of the graph image
$width = 300;
$height = 200;
// Set the amount of space between each column
$padding = 5;
// Get the width of 1 column
$column_width = $width / $columns ;
// set the graph color variables
$im = imagecreate($width,$height);
$gray = imagecolorallocate ($im,0xcc,0xcc,0xcc);
$gray_lite = imagecolorallocate ($im,0xee,0xee,0xee);
$gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f);
$white = imagecolorallocate ($im,0xff,0xff,0xff);
// set the background color of the graph
imagefilledrectangle($im,0,0,$width,$height,$white);
// Calculate the maximum value we are going to plot
$max_value = max($values);
// loop over the array of columns
for($i=0;$i<$columns;$i++)
{
// set the column hieght for each value
$column_height = ($height / 100) * (( $values[$i] / $max_value)
*100);
// now the coords
$x1 = $i*$column_width;
$y1 = $height-$column_height;
$x2 = (($i+1)*$column_width)-$padding;
$y2 = $height;
// write the columns over the background
imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray);
// This gives the columns a little 3d effect
imageline($im,$x1,$y1,$x1,$y2,$gray_lite);
imageline($im,$x1,$y2,$x2,$y2,$gray_lite);
imageline($im,$x2,$y1,$x2,$y2,$gray_dark);
}
// set the correct png headers
header ("Content-type: image/png");
// spit the image out the other end
imagepng($im);
?>
運行效果如下圖所示:

希望本文所述對大家的PHP程序設(shè)計有所幫助。
相關(guān)文章
詳解WordPress開發(fā)中g(shù)et_header()獲取頭部函數(shù)的用法
這篇文章主要介紹了詳解WordPress開發(fā)中g(shù)et_header()獲取頭部的用法,get_header()函數(shù)在WordPress主題的制作中一定會用到,需要的朋友可以參考下2016-01-01
Fatal error: Call to undefined function curl_init()解決方法
Fatal error: Call to undefined function curl_init()解決方法2010-04-04
PHP WebSocket的技術(shù)解析與使用指南詳解
在這篇文章中我們將深入討論從建立連接、綁定到監(jiān)聽等各方面的操作,并提供易于理解和實踐的指導(dǎo),希望可以幫助大家掌握在PHP中使用WebSocket的關(guān)鍵概念和技術(shù)2024-02-02
php中call_user_func函數(shù)使用注意事項
這篇文章主要介紹了php中call_user_func函數(shù)使用注意事項,較為詳細的講述了call_user_func函數(shù)的用法實例與注意事項,具有一定的參考借鑒價值,需要的朋友可以參考下2014-11-11
php foreach 使用&(與運算符)引用賦值要注意的問題
foreach 通過在 $value 之前加上 & 很容易就能修改數(shù)組的單元,在 foreach 使用引用時要注意了。也可以在處理完后立即斷開引用關(guān)系,后面就不會有上述情況了。2010-02-02

