[3594] | 1 | <?php |
---|
| 2 | /*======================================================================= |
---|
| 3 | // File: JPGRAPH_LOG.PHP |
---|
| 4 | // Description: Log scale plot extension for JpGraph |
---|
| 5 | // Created: 2001-01-08 |
---|
| 6 | // Author: Johan Persson (johanp@aditus.nu) |
---|
| 7 | // Ver: $Id: jpgraph_log.php 18250 2005-05-07 14:13:43Z ralfbecker $ |
---|
| 8 | // |
---|
| 9 | // License: This code is released under GPL 2.0 |
---|
| 10 | // Copyright (C) 2001 Johan Persson |
---|
| 11 | //======================================================================== |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | //=================================================== |
---|
| 15 | // CLASS LogScale |
---|
| 16 | // Description: Logarithmic scale between world and screen |
---|
| 17 | //=================================================== |
---|
| 18 | class LogScale extends LinearScale { |
---|
| 19 | //--------------- |
---|
| 20 | // CONSTRUCTOR |
---|
| 21 | |
---|
| 22 | // Log scale is specified using the log of min and max |
---|
| 23 | function LogScale($min,$max,$type="y") { |
---|
| 24 | $this->LinearScale($min,$max,$type); |
---|
| 25 | $this->ticks = new LogTicks(); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | //---------------- |
---|
| 29 | // PUBLIC METHODS |
---|
| 30 | |
---|
| 31 | // Translate between world and screen |
---|
| 32 | function Translate($a) { |
---|
| 33 | if( $a==0 ) $a=1; |
---|
| 34 | $a=log10($a); |
---|
| 35 | return floor($this->off + ($a*1.0 - $this->scale[0]) * $this->scale_factor); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | // Relative translate (don't include offset) usefull when we just want |
---|
| 39 | // to know the relative position (in pixels) on the axis |
---|
| 40 | function RelTranslate($a) { |
---|
| 41 | if( $a==0 ) $a=1; |
---|
| 42 | $a=log10($a); |
---|
| 43 | return ($a*1.0 - $this->scale[0]) * $this->scale_factor; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | function GetMinVal() { |
---|
| 47 | return pow(10,$this->scale[0]); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | function GetMaxVal() { |
---|
| 51 | return pow(10,$this->scale[1]); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | // Logarithmic autoscaling is much simplier since we just |
---|
| 55 | // set the min and max to logs of the min and max values. |
---|
| 56 | // Note that for log autoscale the "maxstep" the fourth argument |
---|
| 57 | // isn't used. This is just included to give the method the same |
---|
| 58 | // signature as the linear counterpart. |
---|
| 59 | function AutoScale(&$img,$min,$max,$dummy) { |
---|
| 60 | if( $min==0 ) $min=1; |
---|
| 61 | assert($max>0); |
---|
| 62 | $smin = floor(log10($min)); |
---|
| 63 | $smax = ceil(log10($max)); |
---|
| 64 | $this->Update($img,$smin,$smax); |
---|
| 65 | } |
---|
| 66 | //--------------- |
---|
| 67 | // PRIVATE METHODS |
---|
| 68 | } // Class |
---|
| 69 | |
---|
| 70 | //=================================================== |
---|
| 71 | // CLASS LogTicks |
---|
| 72 | // Description: |
---|
| 73 | //=================================================== |
---|
| 74 | class LogTicks extends Ticks{ |
---|
| 75 | //--------------- |
---|
| 76 | // CONSTRUCTOR |
---|
| 77 | function LogTicks() { |
---|
| 78 | } |
---|
| 79 | //--------------- |
---|
| 80 | // PUBLIC METHODS |
---|
| 81 | function IsSpecified() { |
---|
| 82 | return true; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | // For log scale it's meaningless to speak about a major step |
---|
| 86 | // We just return -1 to make the framework happy specificall |
---|
| 87 | // StrokeLabels() |
---|
| 88 | function GetMajor() { |
---|
| 89 | return -1; |
---|
| 90 | } |
---|
| 91 | // Draw ticks on image "img" using scale "scale". The axis absolute |
---|
| 92 | // position in the image is specified in pos, i.e. for an x-axis |
---|
| 93 | // it specifies the absolute y-coord and for Y-ticks it specified the |
---|
| 94 | // absolute x-position. |
---|
| 95 | function Stroke(&$img,&$scale,$pos) { |
---|
| 96 | $start = $scale->GetMinVal(); |
---|
| 97 | $limit = $scale->GetMaxVal(); |
---|
| 98 | $nextMajor = 10*$start; |
---|
| 99 | $step = $nextMajor / 10.0; |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | $img->SetLineWeight($this->weight); |
---|
| 103 | |
---|
| 104 | if( $scale->type == "y" ) { |
---|
| 105 | // member direction specified if the ticks should be on |
---|
| 106 | // left or right side. |
---|
| 107 | $a=$pos + $this->direction*$this->GetMinTickAbsSize(); |
---|
| 108 | $a2=$pos + $this->direction*$this->GetMajTickAbsSize(); |
---|
| 109 | |
---|
| 110 | $count=1; |
---|
| 111 | $this->maj_ticks_pos[0]=$scale->Translate($start); |
---|
| 112 | $this->maj_ticklabels_pos[0]=$scale->Translate($start); |
---|
| 113 | if( $this->supress_first ) |
---|
| 114 | $this->maj_ticks_label[0]=""; |
---|
| 115 | else |
---|
| 116 | $this->maj_ticks_label[0]=$start; |
---|
| 117 | $i=1; |
---|
| 118 | for($y=$start; $y<=$limit; $y+=$step,++$count ) { |
---|
| 119 | $ys=$scale->Translate($y); |
---|
| 120 | $this->ticks_pos[]=$ys; |
---|
| 121 | $this->ticklabels_pos[]=$ys; |
---|
| 122 | if( $count % 10 == 0 ) { |
---|
| 123 | if( $this->majcolor!="" ) $img->PushColor($this->majcolor); |
---|
| 124 | $img->Line($pos,$ys,$a2,$ys); |
---|
| 125 | if( $this->majcolor!="" ) $img->PopColor(); |
---|
| 126 | $this->maj_ticks_pos[$i]=$ys; |
---|
| 127 | $this->maj_ticklabels_pos[$i]=$ys; |
---|
| 128 | $this->maj_ticks_label[$i]=$nextMajor; |
---|
| 129 | ++$i; |
---|
| 130 | $nextMajor *= 10; |
---|
| 131 | $step *= 10; |
---|
| 132 | $count=1; |
---|
| 133 | } |
---|
| 134 | else { |
---|
| 135 | if( $this->mincolor!="" ) $img->PushColor($this->mincolor); |
---|
| 136 | $img->Line($pos,$ys,$a,$ys); |
---|
| 137 | if( $this->mincolor!="" ) $img->PopCOlor(); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | else { |
---|
| 142 | $a=$pos - $this->direction*$this->GetMinTickAbsSize(); |
---|
| 143 | $a2=$pos - $this->direction*$this->GetMajTickAbsSize(); |
---|
| 144 | $count=1; |
---|
| 145 | $this->maj_ticks_pos[0]=$scale->Translate($start); |
---|
| 146 | $this->maj_ticklabels_pos[0]=$scale->Translate($start); |
---|
| 147 | if( $this->supress_first ) |
---|
| 148 | $this->maj_ticks_label[0]=""; |
---|
| 149 | else |
---|
| 150 | $this->maj_ticks_label[0]=$start; |
---|
| 151 | $i=1; |
---|
| 152 | for($x=$start; $x<=$limit; $x+=$step,++$count ) { |
---|
| 153 | $xs=$scale->Translate($x); |
---|
| 154 | $this->ticks_pos[]=$xs; |
---|
| 155 | $this->ticklabels_pos[]=$xs; |
---|
| 156 | if( $count % 10 == 0 ) { |
---|
| 157 | $img->Line($xs,$pos,$xs,$a2); |
---|
| 158 | $this->maj_ticks_pos[$i]=$xs; |
---|
| 159 | $this->maj_ticklabels_pos[$i]=$xs; |
---|
| 160 | $this->maj_ticks_label[$i]=$nextMajor; |
---|
| 161 | ++$i; |
---|
| 162 | $nextMajor *= 10; |
---|
| 163 | $step *= 10; |
---|
| 164 | $count=1; |
---|
| 165 | } |
---|
| 166 | else |
---|
| 167 | $img->Line($xs,$pos,$xs,$a); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | return true; |
---|
| 171 | } |
---|
| 172 | } // Class |
---|
| 173 | /* EOF */ |
---|
| 174 | ?> |
---|