[ Index ]
 

Code source de jpGraph 2.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/src/ -> jpgraph_log.php (source)

   1  <?php
   2  /*=======================================================================
   3  // File:     JPGRAPH_LOG.PHP
   4  // Description:    Log scale plot extension for JpGraph
   5  // Created:     2001-01-08
   6  // Ver:        $Id: jpgraph_log.php 821 2006-12-14 01:04:34Z ljp $
   7  //
   8  // Copyright (c) Aditus Consulting. All rights reserved.
   9  //========================================================================
  10  */
  11  
  12  
  13  DEFINE('LOGLABELS_PLAIN',0);
  14  DEFINE('LOGLABELS_MAGNITUDE',1);
  15  
  16  //===================================================
  17  // CLASS LogScale
  18  // Description: Logarithmic scale between world and screen
  19  //===================================================
  20  class LogScale extends LinearScale {
  21  //---------------
  22  // CONSTRUCTOR
  23  
  24      // Log scale is specified using the log of min and max
  25      function LogScale($min,$max,$type="y") {
  26      $this->LinearScale($min,$max,$type);
  27      $this->ticks = new LogTicks();
  28      $this->name = 'log';
  29      }
  30  
  31  //----------------
  32  // PUBLIC METHODS    
  33  
  34      // Translate between world and screen
  35      function Translate($a) {
  36      if( !is_numeric($a) ) {
  37          if( $a != '' && $a != '-' && $a != 'x' ) 
  38          JpGraphError::RaiseL(11001);
  39  //('Your data contains non-numeric values.');
  40          return 1;
  41      }
  42      if( $a < 0 ) {
  43          JpGraphError::RaiseL(11002);
  44  //("Negative data values can not be used in a log scale.");
  45          exit(1);
  46      }
  47      if( $a==0 ) $a=1;
  48      $a=log10($a);
  49      return ceil($this->off + ($a*1.0 - $this->scale[0]) * $this->scale_factor); 
  50      }
  51  
  52      // Relative translate (don't include offset) usefull when we just want
  53      // to know the relative position (in pixels) on the axis    
  54      function RelTranslate($a) {
  55      if( !is_numeric($a) ) {
  56          if( $a != '' && $a != '-' && $a != 'x' ) 
  57          JpGraphError::RaiseL(11001);
  58  //('Your data contains non-numeric values.');
  59          return 1;
  60      }
  61      if( $a==0 ) $a=1;
  62      $a=log10($a);
  63      return round(($a*1.0 - $this->scale[0]) * $this->scale_factor); 
  64      }
  65          
  66      // Use bcpow() for increased precision
  67      function GetMinVal() {
  68      if( function_exists("bcpow") )
  69          return round(bcpow(10,$this->scale[0],15),14);
  70      else
  71          return round(pow(10,$this->scale[0]),14);
  72      }
  73      
  74      function GetMaxVal() {
  75      if( function_exists("bcpow") )
  76          return round(bcpow(10,$this->scale[1],15),14);
  77      else
  78          return round(pow(10,$this->scale[1]),14);
  79      }
  80      
  81      // Logarithmic autoscaling is much simplier since we just
  82      // set the min and max to logs of the min and max values.
  83      // Note that for log autoscale the "maxstep" the fourth argument
  84      // isn't used. This is just included to give the method the same
  85      // signature as the linear counterpart.
  86      function AutoScale($img,$min,$max,$maxsteps,$majend=true) {
  87      if( $min==0 ) $min=1;
  88      
  89      if( $max <= 0 ) {
  90          JpGraphError::RaiseL(11004);
  91  //('Scale error for logarithmic scale. You have a problem with your data values. The max value must be greater than 0. It is mathematically impossible to have 0 in a logarithmic scale.');
  92      }
  93      if( is_numeric($this->autoscale_min) ) {
  94          $smin = round($this->autoscale_min);
  95          $smax = ceil(log10($max));
  96          if( $min >= $max ) {
  97          JpGraphError::RaiseL(25071);//('You have specified a min value with SetAutoMin() which is larger than the maximum value used for the scale. This is not possible.');
  98          }
  99      }
 100      else {
 101          $smin = floor(log10($min));
 102          if( is_numeric($this->autoscale_max) ) {
 103          $smax = round($this->autoscale_max);
 104          if( $smin >= $smax ) {
 105              JpGraphError::RaiseL(25072);//('You have specified a max value with SetAutoMax() which is smaller than the miminum value used for the scale. This is not possible.');
 106          }
 107          }
 108          else
 109          $smax = ceil(log10($max));
 110      }
 111  
 112      $this->Update($img,$smin,$smax);                    
 113      }
 114  //---------------
 115  // PRIVATE METHODS    
 116  } // Class
 117  
 118  //===================================================
 119  // CLASS LogTicks
 120  // Description: 
 121  //===================================================
 122  class LogTicks extends Ticks{
 123      private $label_logtype=LOGLABELS_MAGNITUDE;
 124  //---------------
 125  // CONSTRUCTOR
 126      function LogTicks() {
 127      }
 128  //---------------
 129  // PUBLIC METHODS    
 130      function IsSpecified() {
 131      return true;
 132      }
 133  
 134      function SetLabelLogType($aType) {
 135      $this->label_logtype = $aType;
 136      }
 137      
 138      // For log scale it's meaningless to speak about a major step
 139      // We just return -1 to make the framework happy (specifically
 140      // StrokeLabels() )
 141      function GetMajor() {
 142      return -1;
 143      }
 144  
 145      function SetTextLabelStart($aStart) {
 146      JpGraphError::RaiseL(11005);
 147  //('Specifying tick interval for a logarithmic scale is undefined. Remove any calls to SetTextLabelStart() or SetTextTickInterval() on the logarithmic scale.');
 148      }
 149  
 150      function SetXLabelOffset($dummy) {
 151      // For log scales we dont care about XLabel offset
 152      }
 153  
 154      // Draw ticks on image "img" using scale "scale". The axis absolute
 155      // position in the image is specified in pos, i.e. for an x-axis
 156      // it specifies the absolute y-coord and for Y-ticks it specified the
 157      // absolute x-position.
 158      function Stroke($img,$scale,$pos) {
 159      $start = $scale->GetMinVal();
 160      $limit = $scale->GetMaxVal();
 161      $nextMajor = 10*$start;
 162      $step = $nextMajor / 10.0;
 163          
 164          
 165      $img->SetLineWeight($this->weight);            
 166          
 167      if( $scale->type == "y" ) {
 168          // member direction specified if the ticks should be on
 169          // left or right side.
 170          $a=$pos + $this->direction*$this->GetMinTickAbsSize();
 171          $a2=$pos + $this->direction*$this->GetMajTickAbsSize();    
 172              
 173          $count=1; 
 174          $this->maj_ticks_pos[0]=$scale->Translate($start);
 175          $this->maj_ticklabels_pos[0]=$scale->Translate($start);
 176          if( $this->supress_first )
 177          $this->maj_ticks_label[0]="";
 178          else {
 179          if( $this->label_formfunc != '' ) {
 180              $f = $this->label_formfunc;
 181              $this->maj_ticks_label[0]=call_user_func($f,$start);    
 182          }
 183          elseif( $this->label_logtype == LOGLABELS_PLAIN )
 184              $this->maj_ticks_label[0]=$start;    
 185          else
 186              $this->maj_ticks_label[0]='10^'.round(log10($start));
 187          }
 188          $i=1;
 189          for($y=$start; $y<=$limit; $y+=$step,++$count  ) {
 190          $ys=$scale->Translate($y);    
 191          $this->ticks_pos[]=$ys;
 192          $this->ticklabels_pos[]=$ys;
 193          if( $count % 10 == 0 ) {
 194              if( !$this->supress_tickmarks ) {
 195              if( $this->majcolor!="" ) {
 196                  $img->PushColor($this->majcolor);
 197                  $img->Line($pos,$ys,$a2,$ys);
 198                  $img->PopColor();
 199              }
 200              else
 201                  $img->Line($pos,$ys,$a2,$ys);
 202              }
 203  
 204              $this->maj_ticks_pos[$i]=$ys;
 205              $this->maj_ticklabels_pos[$i]=$ys;
 206              
 207              if( $this->label_formfunc != '' ) {
 208              $f = $this->label_formfunc;
 209              $this->maj_ticks_label[$i]=call_user_func($f,$nextMajor);    
 210              }
 211              elseif( $this->label_logtype == 0 )
 212              $this->maj_ticks_label[$i]=$nextMajor;    
 213              else
 214              $this->maj_ticks_label[$i]='10^'.round(log10($nextMajor));
 215              ++$i;                        
 216              $nextMajor *= 10;
 217              $step *= 10;    
 218              $count=1;                 
 219          }
 220          else {
 221              if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks) {
 222              if( $this->mincolor!="" ) $img->PushColor($this->mincolor);
 223              $img->Line($pos,$ys,$a,$ys);        
 224              if( $this->mincolor!="" ) $img->PopColor();
 225              }
 226          }
 227          }        
 228      }
 229      else {
 230          $a=$pos - $this->direction*$this->GetMinTickAbsSize();
 231          $a2=$pos - $this->direction*$this->GetMajTickAbsSize();    
 232          $count=1; 
 233          $this->maj_ticks_pos[0]=$scale->Translate($start);
 234          $this->maj_ticklabels_pos[0]=$scale->Translate($start);
 235          if( $this->supress_first )
 236          $this->maj_ticks_label[0]="";
 237          else {
 238          if( $this->label_formfunc != '' ) {
 239              $f = $this->label_formfunc;
 240              $this->maj_ticks_label[0]=call_user_func($f,$start);    
 241          }
 242          elseif( $this->label_logtype == 0 )
 243              $this->maj_ticks_label[0]=$start;    
 244          else
 245              $this->maj_ticks_label[0]='10^'.round(log10($start));
 246          }
 247          $i=1;            
 248          for($x=$start; $x<=$limit; $x+=$step,++$count  ) {
 249          $xs=$scale->Translate($x);    
 250          $this->ticks_pos[]=$xs;
 251          $this->ticklabels_pos[]=$xs;
 252          if( $count % 10 == 0 ) {
 253              if( !$this->supress_tickmarks ) {
 254              $img->Line($xs,$pos,$xs,$a2);
 255              }
 256              $this->maj_ticks_pos[$i]=$xs;
 257              $this->maj_ticklabels_pos[$i]=$xs;
 258  
 259              if( $this->label_formfunc != '' ) {
 260              $f = $this->label_formfunc;
 261              $this->maj_ticks_label[$i]=call_user_func($f,$nextMajor);    
 262              }
 263              elseif( $this->label_logtype == 0 )
 264              $this->maj_ticks_label[$i]=$nextMajor;    
 265              else
 266              $this->maj_ticks_label[$i]='10^'.round(log10($nextMajor));
 267              ++$i;
 268              $nextMajor *= 10;
 269              $step *= 10;    
 270              $count=1;                 
 271          }
 272          else {
 273              if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks) {
 274              $img->Line($xs,$pos,$xs,$a);        
 275              }
 276          }
 277          }        
 278      }
 279      return true;
 280      }
 281  } // Class
 282  /* EOF */
 283  ?>


Généré le : Sat Nov 24 09:27:55 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics