当前位置:首页 > PHP教程 > php图像处理 > 列表

PHP生成饼图的示例代码

发布:smiling 来源: PHP粉丝网  添加日期:2023-06-23 15:51:12 浏览: 评论:0 

这篇文章主要为大家详细介绍了如何利用PHP实现饼图的绘制,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下。

首先要把php_iconv.dll和inconv.dll COPY到c:\winnt\system32下,直接上代码:

  1. <? 
  2. define("ANGLE_STEP", 5); //定义画椭圆弧时的角度步长 
  3.  
  4. function draw_getdarkcolor($img,$clr) //求$clr对应的暗色 
  5. { 
  6. $rgb = imagecolorsforindex($img,$clr); 
  7. return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2); 
  8. } 
  9.  
  10. function draw_getexy($a, $b, $d) //求角度$d对应的椭圆上的点坐标 
  11. { 
  12. $d = deg2rad($d); 
  13. return array(round($a*Cos($d)), round($b*Sin($d))); 
  14. } 
  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.  
  31. function draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr) //画扇面 
  32. { 
  33. $n = ceil(($ed-$sd)/ANGLE_STEP); 
  34. $d = $sd; 
  35. list($x0,$y0) = draw_getexy($a, $b, $d); 
  36. imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr); 
  37. for($i=0; $i<$n; $i++) 
  38. { 
  39. $d = ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP); 
  40. list($x, $y) = draw_getexy($a, $b, $d); 
  41. imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr); 
  42. $x0 = $x; 
  43. $y0 = $y; 
  44. } 
  45. imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr); 
  46. list($x, $y) = draw_getexy($a/2, $b/2, ($d+$sd)/2); 
  47. imagefill($img, $x+$ox, $y+$oy, $clr); 
  48. } 
  49.  
  50. function draw_sector3d($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clr) //3d扇面 
  51. { 
  52. draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr); 
  53. if($sd<180) 
  54. { 
  55. list($R, $G, $B) = draw_getdarkcolor($img, $clr); 
  56. $clr=imagecolorallocate($img, $R, $G, $B); 
  57. if($ed>180) $ed = 180; 
  58. list($sx, $sy) = draw_getexy($a,$b,$sd); 
  59. $sx += $ox; 
  60. $sy += $oy; 
  61. list($ex, $ey) = draw_getexy($a, $b, $ed); 
  62. $ex += $ox; 
  63. $ey += $oy; 
  64. imageline($img, $sx, $sy, $sx, $sy+$v, $clr); 
  65. imageline($img, $ex, $ey, $ex, $ey+$v, $clr); 
  66. draw_arc($img, $ox, $oy+$v, $a, $b, $sd, $ed, $clr); 
  67. list($sx, $sy) = draw_getexy($a, $b, ($sd+$ed)/2); 
  68. $sy += $oy+$v/2; 
  69. $sx += $ox; 
  70. imagefill($img, $sx, $sy, $clr); 
  71. } 
  72. } 
  73. function draw_getindexcolor($img, $clr) //RBG转索引色 
  74.  
  75. { 
  76. $R = ($clr>>16) & 0xff; 
  77. $G = ($clr>>8)& 0xff; 
  78. $B = ($clr) & 0xff; 
  79. return imagecolorallocate($img, $R, $G, $B); 
  80. } 
  81.  
  82. // 绘图主函数,并输出图片 
  83. // $datLst 为数据数组, $datLst 为标签数组, $datLst 为颜色数组 
  84. // 以上三个数组的维数应该相等 
  85. function draw_img($datLst,$labLst,$clrLst,$a=250,$b=120,$v=20,$font=10) 
  86. { 
  87. $ox = 5+$a; 
  88. $oy = 5+$b; 
  89. $fw = imagefontwidth($font); 
  90. $fh = imagefontheight($font); 
  91.  
  92. $n = count($datLst);//数据项个数 
  93.  
  94. $w = 10+$a*2; 
  95. $h = 10+$b*2+$v+($fh+2)*$n; 
  96.  
  97. $img = imagecreate($w, $h); 
  98.  
  99. //转RGB为索引色 
  100. for($i=0; $i<$n; $i++) 
  101. $clrLst[$i] = draw_getindexcolor($img,$clrLst[$i]); 
  102.  
  103. $clrbk = imagecolorallocate($img, 0xff, 0xff, 0xff); 
  104. $clrt = imagecolorallocate($img, 0x00, 0x00, 0x00); 
  105.  
  106. //填充背景色 
  107. imagefill($img, 0, 0, $clrbk); 
  108.  
  109. //求和 
  110. $tot = 0; 
  111. for($i=0; $i<$n; $i++) 
  112. $tot += $datLst[$i]; 
  113. $sd = 0; 
  114. $ed = 0; 
  115. $ly = 10+$b*2+$v; 
  116. for($i=0; $i<$n; $i++) 
  117. { 
  118. $sd = $ed; 
  119. $ed += $datLst[$i]/$tot*360; 
  120.  
  121. //画圆饼 
  122. draw_sector3d($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clrLst[$i]); //$sd,$ed,$clrLst[$i]); 
  123.  
  124. //画标签 
  125. imagefilledrectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrLst[$i]); 
  126. imagerectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrt); 
  127. //imagestring($img, $font, 5+2*$fw, $ly, $labLst[$i].":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)", $clrt); 
  128.  
  129. $str=iconv("GB2312", "UTF-8", $labLst[$i]); 
  130. ImageTTFText($img, $font, 0, 5+2*$fw, $ly+13, $clrt, "C:\WINNT\Fonts\simsun.ttc", $str.":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)"); 
  131. $ly += $fh+2; 
  132. } 
  133.  
  134. //输出图形 
  135. header("Content-type: image/png"); 
  136.  
  137. //输出生成的图片 
  138. $imgFileName = "../temp/".time().".png"; 
  139. imagepng($img,$imgFileName); 
  140.  
  141. } 
  142.  
  143. $datLst = array(30, 10, 20, 20, 10, 20, 10, 20); //数据 
  144. $labLst = array("中国科技大学", "安徽理工大学", "清华大学", "北京大学", "南京大学", "上海大学", "河海大学", "中山大学"); //标签 
  145. $clrLst = array(0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999); 
  146.  
  147. //画图 
  148. draw_img($datLst,$labLst,$clrLst); 
  149. ?> 

要注意的是,ImageTTFText函数中的字体C:\WINNT\Fonts\simsun.ttc

Tags: PHP生成饼图

分享到:

相关文章