PHP生成條形圖的方法
更新時間:2014年12月10日 14:30:26 投稿:shichen2014
這篇文章主要介紹了PHP生成條形圖的方法,可實(shí)現(xiàn)生成柱狀的條形圖,適用于一些類似柱狀圖顯示報表的場合,具有一定的實(shí)用價值,需要的朋友可以參考下
本文實(shí)例講述了PHP生成條形圖的方法。分享給大家供大家參考。具體實(shí)現(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);
?>
運(yùn)行效果如下圖所示:
希望本文所述對大家的PHP程序設(shè)計有所幫助。
您可能感興趣的文章:
- php實(shí)現(xiàn)根據(jù)url自動生成縮略圖的方法
- PHP生成自適應(yīng)大小的縮略圖類及使用方法分享
- PHP使用imagick讀取PDF生成png縮略圖的兩種方法
- 使用PHP生成二維碼的兩種方法(帶logo圖像)
- php生成圖形驗(yàn)證碼幾種方法小結(jié)
- PHP生成驗(yàn)證碼時“圖像因其本身有錯無法顯示”的解決方法
- 基于PHP服務(wù)端圖片生成縮略圖的方法詳解
- PHP實(shí)現(xiàn)將HTML5中Canvas圖像保存到服務(wù)器的方法
- PHP判斷一個gif圖片是否為動態(tài)圖片的方法
- php使用ob_start()實(shí)現(xiàn)圖片存入變量的方法
相關(guān)文章
PHP二維數(shù)組排序簡單實(shí)現(xiàn)方法
這篇文章主要介紹了PHP二維數(shù)組排序簡單實(shí)現(xiàn)方法,涉及PHP針對數(shù)組的遍歷與排序操作常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-02-02php獲取服務(wù)器信息的實(shí)現(xiàn)代碼
php獲取服務(wù)器信息的一段代碼,包括獲取mysql數(shù)據(jù)庫版本信息、GD庫信息、php上傳相關(guān)信息等。有需要的朋友,可以參考下2013-02-02php5中date()得出的時間為什么不是當(dāng)前時間的解決方法
從php5.10開始,php中加入了時區(qū)的設(shè)置,在php中顯示的時間都是格林威治標(biāo)準(zhǔn)時間,這就造成了我們 中國的用戶會差八個小時的問題!2008-06-06PHP 開發(fā)環(huán)境配置(Zend Server安裝)
運(yùn)行安裝文件(ZendServer-CE-php-5.3.2-5.0.1-Windows_x86.exe)開始安裝,選項(xiàng)請參照我的選擇。2010-04-04phpmyadmin中配置文件現(xiàn)在需要絕密的短語密碼的解決方法
phpmyadmin中配置文件現(xiàn)在需要絕密的短語密碼的解決方法...2007-02-02