home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 October / CMCD1004.ISO / Software / Shareware / Programare / cnstats / graph / lines.php < prev    next >
Encoding:
PHP Script  |  2004-06-21  |  4.8 KB  |  190 lines

  1. <?
  2. session_start();
  3. session_register("DATA");
  4. $DATA=$HTTP_SESSION_VARS["DATA"];
  5.  
  6. include "../_funct.php";
  7.  
  8. function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1) 
  9. {
  10.    /* this way it works well only for orthogonal lines
  11.    imagesetthickness($image, $thick);
  12.    return imageline($image, $x1, $y1, $x2, $y2, $color);
  13.    */
  14.    if ($thick == 1) {
  15.        return imageline($image, $x1, $y1, $x2, $y2, $color);
  16.    }
  17.    $t = $thick / 2 - 0.5;
  18.    if ($x1 == $x2 || $y1 == $y2) {
  19.        return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
  20.    }
  21.    $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
  22.    $a = $t / sqrt(1 + pow($k, 2));
  23.    $points = array(
  24.        round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
  25.        round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
  26.        round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
  27.        round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
  28.    );    
  29.    imagefilledpolygon($image, $points, 4, $color);
  30.    return imagepolygon($image, $points, 4, $color);
  31. }
  32.  
  33. $GDVERSION=gdVersion();
  34. if ($HTTP_GET_VARS["antialias"]==0) $GDVERSION=1;
  35.  
  36. // ╨ατ≡σ°σφΦσ
  37. $W=$IMGW;
  38. $H=$IMGH;
  39.  
  40. // ╬≥±≥≤∩√
  41. $MB=20; // bottom
  42. $ML=8; // left
  43. $M=5; // ε±≥αδⁿφ√σ
  44.  
  45. // ╪Φ≡Φφα εΣφεπε ±Φ∞Γεδα
  46. $LW=imagefontwidth(2);
  47.  
  48. $THICK=1;
  49.  
  50. // ┼±δΦ Γσ≡±Φ  GD ßεδⁿ°σ ≈σ∞ 2.0, ≥ε Γ±σ Γ ΣΓα ≡ατα ßεδⁿ°σ (Σδ  ±πδαµΦΓαφΦ )
  51. if ($GDVERSION>=2) {
  52.     $W*=2;$H*=2;
  53.     $DX*=2;$DY*=2;
  54.     $LW*=2;$MB*=2;$M*=2;$ML*=2;
  55.     $THICK*=2;
  56.     }
  57.  
  58. // ╩εδΦ≈σ±≥Γε ²δσ∞σφ≥εΓ
  59. $count=count($DATA[0]);
  60. if (count($DATA[1])>$count) $count=count($DATA[1]);
  61. if (count($DATA[2])>$count) $count=count($DATA[2]);
  62.  
  63. if ($count==0) $count=1;
  64.  
  65. // ╤πδαµΦΓασ∞ π≡α⌠ΦΩΦ ##########################################################
  66. if ($HTTP_GET_VARS["s"]==1) {
  67.     for ($i=2;$i<$count-2;$i++) {
  68.         for ($j=0;$j<$count;$j++) {
  69.             $DATA[$j][$i]=($DATA[$j][$i-1]+$DATA[$j][$i-2]+$DATA[$j][$i]+$DATA[$j][$i+1]+$DATA[$j][$i+2])/5;
  70.             }
  71.         }
  72.     }
  73.  
  74. // ╠αΩ±Φ∞αδⁿφεσ τφα≈σφΦσ
  75. $max=0;
  76.  
  77. for ($i=0;$i<$count;$i++) {
  78.     $max=$max<$DATA[0][$i]?$DATA[0][$i]:$max;
  79.     $max=$max<$DATA[1][$i]?$DATA[1][$i]:$max;
  80.     $max=$max<$DATA[2][$i]?$DATA[2][$i]:$max;
  81.     }
  82.  
  83. include "shared.php";
  84.  
  85. $county=10;
  86. $max=$nmax;
  87.  
  88. // ╧εΣ≡αΓφ σ∞ δσΓ≤■ π≡αφΦ÷≤
  89. $text_width=strlen(cNumber($max))*$LW;
  90. $ML+=$text_width;
  91.  
  92. // ╨σαδⁿφ√σ ≡ατ∞σ≡√ π≡α⌠ΦΩα
  93. $RW=$W-$ML-$M;
  94. $RH=$H-$MB-$M;
  95.  
  96. // ╩εε≡ΣΦφα≥√ φ≤δ 
  97. $X0=$ML;
  98. $Y0=$H-$MB;
  99.  
  100. $step=$RH/$county;
  101.  
  102. imagefilledrectangle($im, $X0, $Y0-$RH, $X0+$RW, $Y0, $bg[1]);
  103. imagerectangle($im, $X0, $Y0, $X0+$RW, $Y0-$RH, $c);
  104.  
  105. // ┬√ΓεΣ ±σ≥ΩΦ ∩ε ε±Φ Y
  106. for ($i=1;$i<=$county;$i++) {
  107.     $y=$Y0-$step*$i;
  108.     imageline($im,$X0,$y,$X0+$RW,$y,$c);
  109.     imageline($im,$X0,$y,$X0-($ML-$text_width)/4,$y,$text);
  110.     }
  111.  
  112. // ┬√ΓεΣ ±σ≥ΩΦ ∩ε ε±Φ X
  113. // ┬√ΓεΣ Φτ∞σφ σ∞εΘ ±σ≥ΩΦ
  114. for ($i=0;$i<$count;$i++) {
  115.     imageline($im,$X0+$i*($RW/$count),$Y0,$X0+$i*($RW/$count),$Y0,$c);
  116.     imageline($im,$X0+$i*($RW/$count),$Y0,$X0+$i*($RW/$count),$Y0-$RH,$c);
  117.     }
  118.  
  119. // ┬√ΓεΣ ±≥εδß÷σΓ
  120. $dx=($RW/$count)/2;
  121.  
  122. $pi=$Y0-($RH/$max*$DATA[0][0]);
  123. $po=$Y0-($RH/$max*$DATA[1][0]);
  124. $pu=$Y0-($RH/$max*$DATA[2][0]);
  125. $px=intval($X0+$dx);
  126.  
  127. for ($i=1;$i<$count;$i++) {
  128.     $x=intval($X0+$i*($RW/$count)+$dx);
  129.  
  130.     $y=$Y0-($RH/$max*$DATA[0][$i]);
  131.     imagelinethick($im,$px,$pi,$x,$y,$bar[0][2],$THICK);
  132.     $pi=$y;
  133.  
  134.     $y=$Y0-($RH/$max*$DATA[1][$i]);
  135.     imagelinethick($im,$px,$po,$x,$y,$bar[1][2],$THICK);
  136.     $po=$y;
  137.  
  138.     $y=$Y0-($RH/$max*$DATA[2][$i]);
  139.     imagelinethick($im,$px,$pu,$x,$y,$bar[2][2],$THICK);
  140.     $pu=$y;
  141.     $px=$x;
  142.     }
  143.  
  144. // ╙∞σφⁿ°σφΦσ Φ ∩σ≡σ±≈σ≥ Ωε≡≡ΣΦφα≥
  145. $ML-=$text_width;
  146. if ($GDVERSION>=2) {                                                                                        
  147.     $im1=imagecreatetruecolor($W/2,$H/2);
  148.     imagecopyresampled($im1,$im,0,0,0,0,$W/2,$H/2,$W,$H);                                                   
  149.     imagedestroy($im);
  150.     $im=$im1;                                                                                               
  151.  
  152.     $W/=2;$H/=2;
  153.     $DX/=2;$DY/=2;
  154.     $LW/=2;$MB/=2;$M/=2;$ML/=2;
  155.     $X0/=2;$Y0/=2;$step/=2;
  156.     $RW/=2;$RH/=2;
  157.     }
  158.  
  159. $text=imagecolorallocate($im,136,197,145);
  160.  
  161. $text=imagecolorallocate($im,136,197,145);
  162.  
  163. // ┬√ΓεΣ ∩εΣ∩Φ±σΘ ∩ε ε±Φ Y
  164. for ($i=1;$i<=$county;$i++) {
  165.     $str=cNumber(($max/$county)*$i);
  166.     imagestring($im,2, $X0-strlen($str)*$LW-$ML/4-2,$Y0-$step*$i-imagefontheight(2)/2,$str,$text);
  167.     }
  168.  
  169. // ┬√ΓεΣ ∩εΣ∩Φ±σΘ ∩ε ε±Φ X
  170. $prev=100000;
  171. $twidth=$LW*strlen($DATA["x"][0])+6;
  172. $i=$X0+$RW;
  173.  
  174. while ($i>$X0) {
  175.     if ($prev-$twidth>$i) {
  176.         $drawx=$i-($RW/$count)/2;
  177.         if ($drawx>$X0) {
  178.             $str=$DATA["x"][intval(($i-$X0)/($RW/$count))-1];
  179.             imageline($im,$drawx,$Y0,$i-($RW/$count)/2,$Y0+5,$text);
  180.             imagestring($im,2, $drawx-(strlen($str)*$LW)/2 ,$Y0+7,$str,$text);
  181.             }
  182.         $prev=$i;
  183.         }
  184.     $i-=$RW/$count;
  185.     }
  186.  
  187. header("Content-Type: image/png");
  188. imagepng($im);
  189. imagedestroy($im);
  190. ?>