使用php中的imagecreatetruecolor()函數、imagefilledarc()、imagecolorallocate()、imagettftext()、imagejpeg()函數以及imagedestroy()函數來實現一個類似圓餅的百分比統計圖表,也就是大家所熟悉的餅形圖,多用于一些Php統計系統中,用來顯示數據非常直觀,使用廣泛。
view sourcePRint?01 | <?php |
02 | //php創建圓餅圖表 |
03 | $image = imagecreatetruecolor(200,200); //創建畫布,長寬分別為200*200 |
04 | //創建圓餅中每個要顯示項目所代表的顏色 |
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 | //創建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()函數在 image 所代表的圖像中以 cx,cy(圖像左上角為 0, 0)畫一橢圓弧。w 和 h 分別指定了橢圓的寬和高,s 和 e 參數以角度指定了起始和結束點。style的值可以是下列值按位或(OR)后的值: |
19 | IMG_ARC_PIE |
20 | IMG_ARC_CHORD |
21 | IMG_ARC_NOFILL |
22 | IMG_ARC_EDGED */ |
23 | //立體效果的最上層,顯示視覺效果的重要一層 |
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 | //添加百分比數據到圓餅圖表中 |
29 | $str = iconv ( "gbk" , "UTF-8" , "36%" ); //主要針對中文輸入example:占用:60%; |
30 | imagettftext($image,10,360-15,100+70,115,$white, "simhei.ttf" ,$str); |
31 | imagejpeg($image); |
32 | imagedestroy($image); |
33 | ?> |
新聞熱點
疑難解答
圖片精選