php生成曲线图程序

  1. <?php
  2. /*******************用法*************************
  3. $gg=new build_graph();
  4. $d1=array(0,62,25,20,20,100,99); //曲线一
  5. //$d1=array('15'=>5,'16'=>8,'17'=>7,'18'=>9,'19'=>10,'20'=>15,'21'=>9); 改成这个形式啦
  6. $d2=array(0,80,75,65,100,56,79); //曲线二
  7. $d3=array(0,60,50,25,12,56,45); //曲线三 一下可以继续以此类推
  8. $gg->add_data($d1);
  9. $gg->add_data($d2);
  10. $gg->add_data($d3);
  11. $gg->set_colors("ee00ff,dd8800,00ff00"); //对应曲线的颜色
  12. //生成曲线图
  13. $gg->build("line",0); //参数0表示显示所有曲线,1为显示第一条,依次类推
  14. //生成矩形图
  15. //$gg->build("rectangle","2"); //参数0表示显示第一个矩形,1也为显示第一条,其余依次类推
  16. ///////////////////////////////////////////////////////////
  17. //自定义图形显示,可任意图形叠加显示
  18. header("Content-type: image/png");
  19. $gg->create_cloths(); //画布
  20. $gg->create_frame(); //画个框先
  21. //$gg->build_rectangle(2); //画矩形
  22. $gg->create_line(); //画线
  23. $gg->build_line(0); //画曲线
  24. imagepng($gg->image);
  25. imagedestroy($gg->image);
  26. */
  27. class build_graph {
  28. var $graphwidth=300;
  29. var $graphheight=300;
  30. var $width_num=0; //宽分多少等分
  31. var $height_num=10; //高分多少等分,默认为10
  32. var $height_var=0; //高度增量(用户数据平均数)
  33. var $width_var=0; //宽度增量(用户数据平均数)
  34. var $height_max=0; //最大数据值
  35. var $array_data=array(); //用户待分析的数据的二维数组
  36. var $array_error=array(); //收集错误信息
  37. var $colorBg=array(255,255,255); //图形背景-白色
  38. var $colorGrey=array(192,192,192); //灰色画框
  39. var $colorBlue=array(0,0,255); //蓝色
  40. var $colorRed=array(255,0,0); //红色(点)
  41. var $colorDarkBlue=array(0,0,255); //深色
  42. var $colorLightBlue=array(200,200,255); //浅色
  43. var $array_color; //曲线着色(存储十六进制数)
  44. var $image; //我们的图像
  45. //方法:接受用户数据
  46. function add_data($array_user_data)
  47. {
  48. if(!is_array($array_user_data) or emptyempty($array_user_data))
  49. {
  50. $this->array_error['add_data']="没有可供分析的数据";
  51. return false;
  52. exit();
  53. }
  54. $i=count($this->array_data);
  55. $this->array_data[$i]=$array_user_data;
  56. }
  57. //方法:定义画布宽和长
  58. function set_img($img_width,$img_height){
  59. $this->graphwidth=$img_width;
  60. $this->graphheight=$img_height;
  61. }
  62. //设定Y轴的增量等分,默认为10份
  63. function set_height_num($var_y){
  64. $this->height_num=$var_y;
  65. }
  66. //定义各图形各部分色彩
  67. function get_RGB($color){ //得到十进制色彩
  68. $R=($color>>16) & 0xff;
  69. $G=($color>>8) & 0xff;
  70. $B=($color) & 0xff;
  71. return (array($R,$G,$B));
  72. }
  73. //---------------------------------------------------------------
  74. #定义背景色
  75. function set_color_bg($c1,$c2,$c3){
  76. $this->colorBg=array($c1,$c2,$c3);
  77. }
  78. #定义画框色
  79. function set_color_Grey($c1,$c2,$c3){
  80. $this->colorGrey=array($c1,$c2,$c3);
  81. }
  82. #定义蓝色
  83. function set_color_Blue($c1,$c2,$c3){
  84. $this->colorBlue=array($c1,$c2,$c3);
  85. }
  86. #定义色Red
  87. function set_color_Red($c1,$c2,$c3){
  88. $this->colorRed=array($c1,$c2,$c3);
  89. }
  90. #定义深色
  91. function set_color_DarkBlue($c1,$c2,$c3){
  92. $this->colorDarkBlue=array($c1,$c2,$c3);
  93. }
  94. #定义浅色
  95. function set_color_LightBlue($c1,$c2,$c3){
  96. $this->colorLightBlue=array($c1,$c2,$c3);
  97. }
  98. //---------------------------------------------------------------
  99. //方法:由用户数据将画布分成若干等份宽
  100. //并计算出每份多少像素
  101. function get_width_num(){
  102. $this->width_num=count($this->array_data[0]);
  103. }
  104. function get_max_height(){
  105. //获得用户数据的最大值
  106. $tmpvar=array();
  107. foreach($this->array_data as $tmp_value)
  108. {
  109. $tmpvar[]=max($tmp_value);
  110. }
  111. $this->height_max=max($tmpvar);
  112. return max($tmpvar);
  113. }
  114. function get_height_length(){
  115. //计算出每格的增量长度(用户数据,而不是图形的像素值)
  116. $max_var=$this->get_max_height();
  117. $max_var=round($max_var/$this->height_num);
  118. $first_num=substr($max_var,0,1);
  119. if(substr($max_var,1,1)){
  120. if(substr($max_var,1,1)>=5)
  121. $first_num+=1;
  122. }
  123. for($i=1;$i<strlen($max_var);$i++){
  124. $first_num.="0";
  125. }
  126. return (int)$first_num;
  127. }
  128. function get_var_wh() //得到高和宽的增量
  129. {
  130. $this->get_width_num();
  131. //得到高度增量和宽度增量
  132. $this->height_var=$this->get_height_length();
  133. $this->width_var=round($this->graphwidth/$this->width_num);
  134. }
  135. function set_colors($str_colors){
  136. //用于多条曲线的不同着色,如$str_colors="ee00ff,dd0000,cccccc"
  137. $this->array_color=split(",",$str_colors);
  138. }
  139. ######################################################################################################
  140. function build_line($var_num)
  141. {
  142. if(!emptyempty($var_num))
  143. { //如果用户只选择显示一条曲线
  144. $array_tmp[0]=$this->array_data[$var_num-1];
  145. $this->array_data=$array_tmp;
  146. }
  147. //画线
  148. for($j=0;$j<count($this->array_data);$j++)
  149. {
  150. list($R,$G,$B)=$this->get_RGB(hexdec($this->array_color[$j]));
  151. $colorBlue=imagecolorallocate($this->image,$R,$G,$B);
  152. $i=0;
  153. foreach($this->array_data[$j] as $keys=>$values)
  154. {
  155. $height_next_pix[]=round($this->array_data[$j][$keys]/$this->height_max*$this->graphheight);
  156. }
  157. foreach($this->array_data[$j] as $key=>$value)
  158. {
  159. $height_pix=round(($this->array_data[$j][$key]/$this->height_max)*$this->graphheight);
  160. if($i!=count($this->array_data[$j])-1)
  161. {
  162. imageline($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$this->width_var*($i+1),$this->graphheight-$height_next_pix[$i+1],$colorBlue);
  163. }
  164. $i++;
  165. }
  166. //print_r($height_next_pix);
  167. // exit;
  168. /*
  169. for($i=0;$i<$this->width_num-1;$i++)
  170. {
  171. $height_pix=round(($this->array_data[$j][$i]/$this->height_max)*$this->graphheight);
  172. $height_next_pix=round($this->array_data[$j][$i+1]/$this->height_max*$this->graphheight);
  173. imageline($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$this->width_var*($i+1),$this->graphheight-$height_next_pix,$colorBlue);
  174. }*/
  175. }
  176. //画点
  177. $colorRed=imagecolorallocate($this->image, $this->colorRed[0], $this->colorRed[1], $this->colorRed[2]);
  178. for($j=0;$j<count($this->array_data);$j++)
  179. {
  180. $i=0;
  181. foreach($this->array_data[$j] as $key=>$value)
  182. {
  183. $height_pix=round(($this->array_data[$j][$key]/$this->height_max)*$this->graphheight);
  184. imagearc($this->image,$this->width_var*$i,$this->graphheight-$height_pix,6,5,0,360,$colorRed);
  185. imagefilltoborder($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$colorRed,$colorRed);
  186. $i++;
  187. }
  188. /*
  189. for($i=0;$i<$this->width_num;$i++)
  190. {
  191. $height_pix=round(($this->array_data[$j][$i]/$this->height_max)*$this->graphheight);
  192. imagearc($this->image,$this->width_var*$i,$this->graphheight-$height_pix,6,5,0,360,$colorRed);
  193. imagefilltoborder($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$colorRed,$colorRed);
  194. }
  195. */
  196. }
  197. }
  198. ######################################################################################################
  199. function build_rectangle($select_gra){
  200. if(!emptyempty($select_gra)){ //用户选择显示一个矩形
  201. $select_gra-=1;
  202. }
  203. //画矩形
  204. //配色
  205. $colorDarkBlue=imagecolorallocate($this->image, $this->colorDarkBlue[0], $this->colorDarkBlue[1], $this->colorDarkBlue[2]);
  206. $colorLightBlue=imagecolorallocate($this->image, $this->colorLightBlue[0], $this->colorLightBlue[1], $this->colorLightBlue[2]);
  207. if(emptyempty($select_gra))
  208. $select_gra=0;
  209. for($i=0; $i<$this->width_num; $i++){
  210. $height_pix=round(($this->array_data[$select_gra][$i]/$this->height_max)*$this->graphheight);
  211. imagefilledrectangle($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$this->width_var*($i+1),$this->graphheight, $colorDarkBlue);
  212. imagefilledrectangle($this->image,($i*$this->width_var)+1,($this->graphheight-$height_pix)+1,$this->width_var*($i+1)-5,$this->graphheight-2, $colorLightBlue);
  213. }
  214. }
  215. ######################################################################################################
  216. function create_cloths(){
  217. //创建画布
  218. $this->image=imagecreate($this->graphwidth+20,$this->graphheight+20);
  219. }
  220. function create_frame(){
  221. //创建画框
  222. $this->get_var_wh();
  223. //配色
  224. $colorBg=imagecolorallocate($this->image, $this->colorBg[0], $this->colorBg[1], $this->colorBg[2]);
  225. $colorGrey=imagecolorallocate($this->image, $this->colorGrey[0], $this->colorGrey[1], $this->colorGrey[2]);
  226. //创建图像周围的框
  227. imageline($this->image, 0, 0, 0, $this->graphheight,$colorGrey);
  228. imageline($this->image, 0, 0, $this->graphwidth, 0,$colorGrey);
  229. imageline($this->image, ($this->graphwidth-1),0,($this->graphwidth-1),($this->graphheight-1),$colorGrey);
  230. imageline($this->image, 0,($this->graphheight-1),($this->graphwidth-1),($this->graphheight-1),$colorGrey);
  231. }
  232. function create_line()
  233. {//开源代码phpfensi.com
  234. //创建网格。
  235. $this->get_var_wh();
  236. $colorBg=imagecolorallocate($this->image, $this->colorBg[0], $this->colorBg[1], $this->colorBg[2]);
  237. $colorGrey=imagecolorallocate($this->image, $this->colorGrey[0], $this->colorGrey[1], $this->colorGrey[2]);
  238. $colorRed=imagecolorallocate($this->image, $this->colorRed[0], $this->colorRed[1], $this->colorRed[2]);
  239. for($i=1;$i<=$this->height_num;$i++)
  240. {
  241. //画横线
  242. $y1=($this->graphheight-($this->height_var/$this->height_max*$this->graphheight)*$i);
  243. $y2=($this->graphheight-($this->height_var/$this->height_max*$this->graphheight)*$i);
  244. imageline($this->image,0,$y1,$this->graphwidth,$y2,$colorGrey);
  245. //标出数字
  246. imagestring($this->image,2,0,$this->graphheight-($this->height_var/$this->height_max*$this->graphheight)*$i,$this->height_var*$i,$colorRed);
  247. }
  248. unset($i);
  249. foreach($this->array_data[0] as $key=>$value)
  250. {
  251. //画竖线
  252. imageline($this->image,$this->width_var*$i,0,$this->width_var*$i,$this->graphwidth,$colorGrey);
  253. //标出数字
  254. imagestring($this->image,2,$this->width_var*$i,$this->graphheight-15,$key,$colorRed);
  255. $i++;
  256. }
  257. /*
  258. for($i=1;$i<=$this->width_num;$i++)
  259. {
  260. //画竖线
  261. imageline($this->image,$this->width_var*$i,0,$this->width_var*$i,$this->graphwidth,$colorGrey);
  262. //标出数字
  263. imagestring($this->image,2,$this->width_var*$i,$this->graphheight-15,$i,$colorRed);
  264. }
  265. */
  266. }
  267. function build($graph,$str_var){
  268. //$graph是用户指定的图形种类,$str_var是生成哪个数据的图
  269. header("Content-type: image/jpeg");
  270. $this->create_cloths(); //先要有画布啊~~
  271. switch ($graph){
  272. case "line":
  273. $this->create_frame(); //画个框先:)
  274. $this->create_line(); //打上底格线
  275. $this->build_line($str_var); //画曲线
  276. break;
  277. case "rectangle":
  278. $this->create_frame(); //画个框先:)
  279. $this->build_rectangle($str_var); //画矩形
  280. $this->create_line(); //打上底格线
  281. break;
  282. }
  283. //输出图形并清除内存
  284. imagepng($this->image);
  285. imagedestroy($this->image);
  286. }
  287. }
  288. ?>