- /*
- * PHP generates a line chart
- * by bbs.it-home.org
- */
- function Line_stats_pic ($value _y, $width, $high, $strong =1, $fix =0) {
- Y-Value processing function
- function line_point_y ($num, $width, $high, $max _num_add, $min _num_add, $y _pxdensity) {
- $return = $high-floor (($num-$min _num_add+ $y _pxdensity)/(($max _num_add-$min _num_add)/$high));
- return $return;
- }
- Parameter handling
- $allnum =sizeof ($value _y);
- $max _num=max ($value _y); Maximum Value
- $min _num=min ($value _y); Minimum value
- $limit _m= $max _num-$min _num; Very poor
- $max _num_add= $max _num+ $limit _m*0.1; Axis Maximum Value
- $min _num_add= $min _num-$limit _m*0.1; Minimum axis value
- $limit = $max _num_add-$min _num_add; Extreme difference-Axis y
- $y _pxdensity= ($max _num_add-$min _num_add)/$high; Y-Axis density
- $x _pxdensity=floor ($width/$allnum); X-Axis Density
- Reset ($value _y); Zeroing array Pointer to zero
- $i = 0;
- foreach ($value _y as $val) {
- $point _y[$i]=line_point_y ($val, $width, $high, $max _num_add, $min _num_add, $y _pxdensity);
- $i + +;
- }
- $zero _y=line_point_y (0, $width, $high, $max _num_add, $min _num_add, $y _pxdensity); Y value of 0 points
- $empty _size_x= (strlen ($max _num) > strlen ($min _num)? strlen ($max _num): strlen ($min _num)) *5+3; Left blank
- Picture Stream Start
- Header ("Content-type:image/png");
- $pic =imagecreate ($width + $empty _size_x+10, $high +13);
- Imagecolorallocate ($pic, 255,255,255); Background color
- $color _1=imagecolorallocate ($pic, 30,144,255); Line Color
- $color _2=imagecolorallocate ($pic, 0,0,0); Black
- $color _3=imagecolorallocate ($pic, 194,194,194);//Grey
- Draw Grid
- Imagesetthickness ($pic, 1); Grid line width
- $y _line_width=floor ($width/100); Number of vertical grid lines
- $y _line_density= $y _line_width==0? 0:floor ($width/$y _line_width); Vertical Grid line Density
- $point _zero_y= $zero _y > $high? $high: $zero _y;
- Imagestring ($pic, 1, $empty _size_x-1, $high +4, "0", $color _2); 0-Point Axis mark
- for ($i =1; $i <= $y _line_width; $i + +) {//Draw vertical grid lines
- Imagesetthickness ($pic, 1); Grid line width
- Imageline ($pic, $y _line_density* $i + $empty _size_x,0, $y _line_density* $i + $empty _size_x, $high, $color _3);
- Imagesetthickness ($pic, 2); Axis Point line width
- Imageline ($pic, $y _line_density* $i + $empty _size_x, $point _zero_y-4, $y _line_density* $i + $empty _size_x, $point _zero_y , $color _2);
- Imagestring ($pic, 1,100* $i + $empty _size_x-5, $high +4, $allnum/$y _line_width* $i, $color _2); Axis Mark
- }
- $x _line_width=floor ($high/30); Number of horizontal grid lines
- $x _line_density= $x _line_width==0? 0:floor ($high/$y _line_width); Horizontal Grid line Density
- if ($zero _y > $high) {//Draw horizontal grid lines
- Imagestring ($pic, 1,0, $high -3,round ($min _num_add, $fix), $color _2); 0-Point Axis mark
- for ($i =1; $i <= $x _line_width; $i + +) {
- Imagesetthickness ($pic, 1); Grid line width
- Imageline ($pic, 0+ $empty _size_x, $high-$x _line_density* $i, $width + $empty _size_x, $high-$x _line_density* $i, $color _3 );
- Imagesetthickness ($pic, 2); Axis Point line width
- Imageline ($pic, 0+ $empty _size_x, $high-$x _line_density* $i, $empty _size_x, $high-$x _line_density* $i, $color _2);
- Imagestring ($pic, 1,0, $high-$x _line_density* $i -3,round ($limit/$x _line_width* $i + $min _num_add, $fix), $color _2); Axis Mark
- }
- }else{
- Imagestring ($pic, 1, $empty _size_x-8, $zero _y, "0", $color _2); 0-Point Axis mark
- for ($i =1; $i <= ceil ($x _line_width/2); $i + +) {
- Imagesetthickness ($pic, 1); Grid line width
- Imageline ($pic, 0+ $empty _size_x, $zero _y-$x _line_density* $i, $width + $empty _size_x, $zero _y-$x _line_density* $i, $ Color_3);
- if ($zero _y+ $x _line_density* $i < $high) {
- Imageline ($pic, 0+ $empty _size_x, $zero _y+ $x _line_density* $i, $width + $empty _size_x, $zero _y+ $x _line_density* $i, $ Color_3);
- }
- Imagesetthickness ($pic, 2); Axis Point line width
- Imageline ($pic, 0+ $empty _size_x, $zero _y-$x _line_density* $i, $empty _size_x, $zero _y-$x _line_density* $i, $color _2) ;
- if ($zero _y+ $x _line_density* $i < $high) {
- Imageline ($pic, 0+ $empty _size_x, $zero _y+ $x _line_density* $i, $empty _size_x, $zero _y+ $x _line_density* $i, $color _2) ;
- }
- Imagestring ($pic, 1,0, $zero _y-$x _line_density* $i -3,round ($limit/$x _line_width* $i, $fix), $color _2); Axis Mark
- if ($zero _y+ $x _line_density* $i < $high) {
- Imagestring ($pic, 1,0, $zero _y+ $x _line_density* $i -3,round (-$limit/$x _line_width* $i, $fix), $color _2); Axis Mark
- }
- }
- }
- Drawing axes
- Imagesetthickness ($pic, 2); Axis width
- Imageline ($pic, 1+ $empty _size_x,0,1+ $empty _size_x, $high, $color _2);
- if ($zero _y > $high) {//x axis position
- Imageline ($pic, 0+ $empty _size_x, $high, $width + $empty _size_x, $high, $color _2);
- }else{
- Imageline ($pic, 0+ $empty _size_x, $zero _y, $width + $empty _size_x, $zero _y, $color _2);
- }
- Create a polyline
- $point _x=0;
- $j = 0;
- Imagesetthickness ($pic, $strong); Line thickness
- while ($j +1 < $allnum) {
- Imageline ($pic, $point _x+2+ $empty _size_x, $point _y[$j], $point _x+ $x _pxdensity+2+ $empty _size_x, $point _y[$j +1],$ color_1);
- $point _x+= $x _pxdensity;
- $j + +;
- }
- Imagepng ($pic);
- Imagedestroy ($pic);
- }
- for ($i =0; $i <100; $i + +) {
- $value = rand (1,200);
- $value _y[]= $value;
- }
- Line_stats_pic ($value _y,500,100,1,1);
- ?>
Copy Code |