php 圓餅圖統計程式碼用法

WBOY
發布: 2016-07-25 08:52:15
原創
926 人瀏覽過
  1. define("ANGLE_STEP", 5); //定義畫橢圓弧時的角度步長
  2. function draw_getgetkcolor($ img,$clr) //求$clr對應的暗色
  3. {
  4. $rgb = imagecolorsforindex($img,$clr);
  5. return array($rgb["red"]/2,$rgb ["green"]/2,$rgb["blue"]/2);
  6. }
  7. function draw_getexy($a, $b, $d) //求角度$d對應的橢圓上的點座標
  8. {
  9. $d = deg2rad($d);
  10. return array(round($a*Cos($d)), round($b*Sin($d))) ;
  11. }
  12. function draw_arc($img,$ox,$oy,$a,$b,$sd,$ed,$clr) //橢圓弧函數
  13. {
  14. $n = ceil(($ed-$sd)/ANGLE_STEP);
  15. $d = $sd;
  16. list($x0,$y0) = draw_getexy($a,$b,$d);
  17. for($i=0; $i {
  18. $d = ($d ANGLE_STEP)>$ed?$ed:($d ANGLE_STEP);
  19. list ($x, $y) = draw_getexy($a, $b, $d);
  20. imageline($img, $x0 $ox, $y0 $oy, $x $ox, $y $oy, $clr );
  21. $x0 = $x;
  22. $y0 = $y;
  23. }
  24. }
  25. function draw_sector($img, $ox, $oy, $a, $ b, $sd, $ed, $clr) //畫扇面
  26. {
  27. $n = ceil(($ed-$sd)/ANGLE_STEP);
  28. $d = $sd;
  29. list($x0,$y0) = draw_getexy($a, $b, $d);
  30. imageline($img, $x0 $ox, $y0 $oy, $ox, $oy, $clr);
  31. for($i=0; $i {
  32. $d = ($d ANGLE_STEP)>$ed?$ed:($d ANGLE_STEP);
  33. list ($x, $y) = draw_getexy($a, $b, $d);
  34. imageline($img, $x0 $ox, $y0 $oy, $x $ox, $y $oy, $clr );
  35. $x0 = $x;
  36. $y0 = $y;
  37. }
  38. imageline($img, $x0 $ox, $y0 $oy, $ox, $oy, $clr );
  39. list($x, $y) = draw_getexy($a/2, $b/2, ($d $sd)/2);
  40. imagefill($img, $x $ox, $ y $oy, $clr);
  41. }
  42. function draw_sector3d($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clr) // 3d扇面
  43. {
  44. draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr);
  45. if($sd {
  46. list($R, $G, $B) = draw_getdarkcolor($img, $clr);
  47. $clr=imagecolorallocate($img, $R, $G, $B);
  48. if( $ed>180) $ed = 180;
  49. list($sx, $sy) = draw_getexy($a,$b,$sd);
  50. $sx = $ox;
  51. $sy = $ oy;
  52. list($ex, $ey) = draw_getexy($a, $b, $ed);
  53. $ex = $ox;
  54. $ey = $oy;
  55. imageline($ img, $sx, $sy, $sx, $sy $v, $clr);
  56. imageline($img, $ex, $ey, $ex, $ey $v, $clr);
  57. draw_arc ($img, $ox, $oy $v, $a, $b, $sd, $ed, $clr);
  58. list($sx, $sy) = draw_getexy($a, $b, ($ sd $ed)/2);
  59. $sy = $oy $v/2;
  60. $sx = $ox;
  61. imagefill($img, $sx, $sy, $clr);
  62. }
  63. }
  64. function draw_getindexcolor($img, $clr) //RBG轉索引色
  65. {
  66. $R = ($clr>>16) & 0xff;
  67. $G = ($clr>>8)& 0xff;
  68. $B = ($clr) & 0xff;
  69. return imagecolorallocate($img, $R, $G, $B);
  70. }
  71. // 繪圖主函數,並輸出圖片
  72. // $datLst 為資料數組, $datLst 為標籤數組, $datLst 為顏色數組
  73. // 以上三個數組的維數應該相等
  74. function draw_img($datLst,$labLst,$clrLst,$a=250,$b=120,$v=20,$font=10)
  75. {
  76. $ox = 5 $a;
  77. $oy = 5 $b;
  78. $fw = imagefontwidth($font);
  79. $fh = imagefontheight($font);
  80. $n = count($datLst);/ /資料項數
  81. $w = 10 $a*2;
  82. $h = 10 $b*2 $v ($fh 2)*$n;
  83. $img = imagecreate($w, $h);
  84. //轉RGB為索引色
  85. for($i=0; $i $clrLst[$i ] = draw_getindexcolor($img,$clrLst[$i]);
  86. $clrbk = imagecolorallocate($img, 0xff, 0xff, 0xff);
  87. $allclrt = imagecolor. , 0x00);
  88. //填充背景色
  89. imagefill($img, 0, 0, $clrbk);
  90. //求和
  91. $tot = 0
  92. //求和
  93. $tot = 0;
  94. for($i=0; $i $tot = $datLst[$i];
  95. $sd = 0;
  96. $ed = 0;
  97. $ly = 10 $b*2 $v;
  98. for($i=0; $i {
  99. $sd = $ed;
  100. $ ed = $datLst[$i]/$tot*360;
  101. //畫圓餅
  102. draw_sector3d($img, $ox, $oy, $a, $b, $v, $sd , $ed, $clrLst[$i]); //$sd,$ed,$clrLst[$i]);
  103. //畫標籤
  104. imagefilledrectangle($img, 5, $ly , 5 $fw, $ly $fh, $clrLst[$i]);
  105. imagerectangle($img, 5, $ly, 5 $fw, $ly $fh, $clrt);
  106. //imagestring ($img, $font, 5 2*$fw, $ly, $labLst[$i].":".$datLst[$i]."(".(round(10000*($datLst[$i] /$tot))/100)."%)", $clrt);
  107. $str = iconv("GB2312", "UTF-8", $labLst[$i]);
  108. ImageTTFText($img, $font, 0, 5 2*$fw, $ly 13, $clrt, "./simsun.ttf", $str.":".$datLst[$i]."(".( round(10000*($datLst[$i]/$tot))/100)."%)");
  109. $ly = $fh 2;
  110. }
  111. //輸出圖形
  112. header("Content-type: image/png");
  113. //輸出產生的圖片
  114. $imgFileName = "temp/".time().".png";
  115. imagepng($img,$imgFileName);
  116. echo 'php 圓餅圖統計程式碼用法';
}
?>
複製程式碼

2、呼叫方法:
  1. require_once ("piefunction.php");
  2. $datLst = array(10,110,3000); 🎜>$labLst = array("好評","中評","負評"); //標籤
  3. $clrLst = array(0x99ff00, 0xff6666, 0x0099ff);
  4. ?>
複製程式碼

說明: 只要資料的數量與後面的標籤,顏色數一致就可以輸出正確的餅狀圖效果了。


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!