使用php中的imagecreatetruecolor()函數(shù)、imagefilledarc()、imagecolorallocate()、imagettftext()、imagejpeg()函數(shù)以及imagedestroy()函數(shù)來(lái)實(shí)現(xiàn)一個(gè)類似圓餅的百分比統(tǒng)計(jì)圖表,也就是大家所熟悉的餅形圖,多用于一些Php統(tǒng)計(jì)系統(tǒng)中,用來(lái)顯示數(shù)據(jù)非常直觀,使用廣泛。
view sourcePRint?01 | <?php |
02 | //php創(chuàng)建圓餅圖表 |
03 | $image = imagecreatetruecolor(200,200); //創(chuàng)建畫布,長(zhǎng)寬分別為200*200 |
04 | //創(chuàng)建圓餅中每個(gè)要顯示項(xiàng)目所代表的顏色 |
05 | $red = imagecolorallocate($image,255,0,0); |
06 | $blue = imagecolorallocate($image,0,0,255); |
07 | $yellow = imagecolorallocate($image,255,255,0); |
08 | $violet = imagecolorallocate($image,255,0,255); |
09 | $white = imagecolorallocate($image,255,255,255); |
10 | $black = imagecolorallocate($image,0,0,0); |
11 | //創(chuàng)建3D底層 |
12 | for($i=120;$i>100;$i--){ |
13 | imagefilledarc($image,100,$i,200,120,0,30,$red,IMG_ARC_PIE);//IMG_ARC_PIE注釋如下: |
14 | imagefilledarc($image,100,$i,200,120,30,80,$blue,IMG_ARC_PIE); |
15 | imagefilledarc($image,100,$i,200,120,80,360,$yellow,IMG_ARC_PIE); |
16 | } |
17 | /* |
18 | imagefilledarc()函數(shù)在 image 所代表的圖像中以 cx,cy(圖像左上角為 0, 0)畫一橢圓弧。w 和 h 分別指定了橢圓的寬和高,s 和 e 參數(shù)以角度指定了起始和結(jié)束點(diǎn)。style的值可以是下列值按位或(OR)后的值: |
19 | IMG_ARC_PIE |
20 | IMG_ARC_CHORD |
21 | IMG_ARC_NOFILL |
22 | IMG_ARC_EDGED */ |
23 | //立體效果的最上層,顯示視覺(jué)效果的重要一層 |
24 | imagearc($image,100,100,200,120,0,360,$black);//添加黑色邊框,以突出3D效果 |
25 | imagefilledarc($image,100,100,200,120,0,30,$red,IMG_ARC_PIE); |
26 | imagefilledarc($image,100,100,200,120,30,80,$blue,IMG_ARC_PIE); |
27 | imagefilledarc($image,100,100,200,120,80,360,$yellow,IMG_ARC_PIE); |
28 | //添加百分比數(shù)據(jù)到圓餅圖表中 |
29 | $str = iconv ("gbk","UTF-8","36%");//主要針對(duì)中文輸入example:占用:60%; |
30 | imagettftext($image,10,360-15,100+70,115,$white,"simhei.ttf",$str); |
31 | imagejpeg($image); |
32 | imagedestroy($image); |
33 | ?> |
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注