国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php生成餅圖效果的實現代碼

2024-09-04 11:44:31
字體:
來源:轉載
供稿:網友

生成餅圖很多代碼都可以實現,今天我們介紹的這個實例是基于php gd庫的一種生成統計數據的餅圖效果,有需要的同學可以參考一下,代碼如下:

  1. <?php 
  2. //+------------------------+  
  3. //| pie3dfun.PHP//公用函數 |  
  4. //+------------------------+  
  5. define("ANGLE_STEP", 5); //定義畫橢圓弧時的角度步長  
  6. function draw_getdarkcolor($img,$clr//求$clr對應的暗色  
  7. {  
  8. $rgb = imagecolorsforindex($img,$clr);  
  9. return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2);  
  10. }  
  11. function draw_getexy($a$b$d//求角度$d對應的橢圓上的點坐標  
  12. {  
  13. $d = deg2rad($d);  
  14. return array(round($a*Cos($d)), round($b*Sin($d)));  
  15. }  
  16. function draw_arc($img,$ox,$oy,$a,$b,$sd,$ed,$clr//橢圓弧函數  
  17. {  
  18. $n = ceil(($ed-$sd)/ANGLE_STEP);  
  19. $d = $sd;  
  20. list($x0,$y0) = draw_getexy($a,$b,$d);  
  21. for($i=0; $i<$n$i++)  
  22. {  
  23. $d = ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP);  
  24. list($x$y) = draw_getexy($a$b$d);  
  25. imageline($img$x0+$ox$y0+$oy$x+$ox$y+$oy$clr);  
  26. $x0 = $x;  
  27. $y0 = $y;  
  28. }  
  29. }  
  30. function draw_sector($img$ox$oy$a$b$sd$ed$clr//畫扇面  
  31. {  
  32. $n = ceil(($ed-$sd)/ANGLE_STEP);  
  33. $d = $sd;  
  34. list($x0,$y0) = draw_getexy($a$b$d);  
  35. imageline($img$x0+$ox$y0+$oy$ox$oy$clr);  
  36. for($i=0; $i<$n$i++)  
  37. {  
  38. $d = ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP);  
  39. list($x$y) = draw_getexy($a$b$d);  
  40. imageline($img$x0+$ox$y0+$oy$x+$ox$y+$oy$clr);  
  41. $x0 = $x;  
  42. $y0 = $y;  
  43. }  
  44. imageline($img$x0+$ox$y0+$oy$ox$oy$clr);  
  45. list($x$y) = draw_getexy($a/2, $b/2, ($d+$sd)/2);  
  46. imagefill($img$x+$ox$y+$oy$clr);  
  47. }  
  48. function draw_sector3d($img$ox$oy$a$b$v$sd$ed$clr//3d扇面  
  49. {  
  50. draw_sector($img$ox$oy$a$b$sd$ed$clr);  
  51. if($sd<180)  
  52. {  
  53. list($R$G$B) = draw_getdarkcolor($img$clr);  
  54. $clr=imagecolorallocate($img$R$G$B);  
  55. if($ed>180) $ed = 180;  
  56. list($sx$sy) = draw_getexy($a,$b,$sd);  
  57. $sx += $ox;  
  58. $sy += $oy;  
  59. list($ex$ey) = draw_getexy($a$b$ed);  
  60. $ex += $ox;  
  61. $ey += $oy;  
  62. imageline($img$sx$sy$sx$sy+$v$clr);  
  63. imageline($img$ex$ey$ex$ey+$v$clr);  
  64. draw_arc($img$ox$oy+$v$a$b$sd$ed$clr);  
  65. list($sx$sy) = draw_getexy($a$b, ($sd+$ed)/2);  
  66. $sy += $oy+$v/2;  
  67. $sx += $ox;  
  68. imagefill($img$sx$sy$clr);  
  69. }  
  70. }  
  71. function draw_getindexcolor($img$clr//RBG轉索引色  
  72. {  
  73. $R = ($clr>>16) & 0xff;  
  74. $G = ($clr>>8)& 0xff;  
  75. $B = ($clr) & 0xff;  
  76. return imagecolorallocate($img$R$G$B);  
  77. }  
  78. // 繪圖主函數,并輸出圖片  
  79. // $datLst 為數據數組, $datLst 為標簽數組, $datLst 為顏色數組  
  80. // 以上三個數組的維數應該相等  
  81. function draw_img($datLst,$labLst,$clrLst,$a=250,$b=120,$v=20,$font=10)  
  82. {  
  83. $ox = 5+$a;  
  84. $oy = 5+$b;  
  85. $fw = imagefontwidth($font);  
  86. $fh = imagefontheight($font);  
  87. $n = count($datLst);//數據項個數  
  88. $w = 10+$a*2;  
  89. $h = 10+$b*2+$v+($fh+2)*$n;  
  90. $img = imagecreate($w$h);  
  91. //轉RGB為索引色  
  92. for($i=0; $i<$n$i++)  
  93. $clrLst[$i] = draw_getindexcolor($img,$clrLst[$i]);  
  94. $clrbk = imagecolorallocate($img, 0xff, 0xff, 0xff);  
  95. $clrt = imagecolorallocate($img, 0x00, 0x00, 0x00);  
  96. //填充背景色  
  97. imagefill($img, 0, 0, $clrbk);  
  98. //求和  
  99. $tot = 0;  
  100. for($i=0; $i<$n$i++)  
  101. $tot += $datLst[$i];  
  102. $sd = 0;  
  103. $ed = 0; 333  
  104. $ly = 10+$b*2+$v;  
  105. for($i=0; $i<$n$i++)  
  106. {  
  107. $sd = $ed;  
  108. $ed += $datLst[$i]/$tot*360;  
  109. //畫圓餅  
  110. draw_sector3d($img$ox$oy$a$b$v$sd$ed$clrLst[$i]); //$sd,$ed,$clrLst[$i]);  
  111. //畫標簽  
  112. imagefilledrectangle($img, 5, $ly, 5+$fw$ly+$fh$clrLst[$i]);  
  113. imagerectangle($img, 5, $ly, 5+$fw$ly+$fh$clrt);  
  114. //imagestring($img, $font, 5+2*$fw, $ly, $labLst[$i].":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)", $clrt);  
  115. $str = iconv("GB2312""UTF-8"$labLst[$i]);  
  116. ImageTTFText($img$font, 0, 5+2*$fw$ly+13, $clrt"./simsun.ttf"$str.":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)");  
  117. $ly += $fh+2;  
  118. }  
  119. //輸出圖形  
  120. header("Content-type: image/png");  
  121. //輸出生成的圖片  
  122. $imgFileName = "../temp/".time().".png";  
  123. imagepng($img,$imgFileName);  
  124. echo '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  
  125. //開源代碼Vevb.com 
  126. $datLst = array(30, 10, 20, 20, 10, 20, 10, 20); //數據  
  127. $labLst = array("中國科技大學""安徽理工大學""清華大學""北京大學""南京大學""上海大學""河海大學""中山大學"); //標簽  
  128. $clrLst = array(0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999);  
  129. //畫圖  
  130. draw_img($datLst,$labLst,$clrLst);  
  131. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江都市| 新泰市| 册亨县| 梁平县| 略阳县| 伊宁市| 定日县| 海阳市| 房产| 福贡县| 安吉县| 水城县| 奎屯市| 韶关市| 泽库县| 芜湖市| 荔波县| 平和县| 灌阳县| 和田市| 辽中县| 新丰县| 师宗县| 罗平县| 娄底市| 博罗县| 尉氏县| 长岛县| 博野县| 朝阳区| 屯门区| 凤庆县| 那坡县| 胶南市| 高平市| 东方市| 马龙县| 蓝田县| 太仓市| 随州市| 赤水市|