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