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 957 2007-12-01 14:00:29Z ljp $
|
---|
7 | //
|
---|
8 | // Copyright (c) Aditus Consulting. All rights reserved.
|
---|
9 | //========================================================================
|
---|
10 | */
|
---|
11 |
|
---|
12 | DEFINE('LOGLABELS_PLAIN',0);
|
---|
13 | DEFINE('LOGLABELS_MAGNITUDE',1);
|
---|
14 |
|
---|
15 | //===================================================
|
---|
16 | // CLASS LogScale
|
---|
17 | // Description: Logarithmic scale between world and screen
|
---|
18 | //===================================================
|
---|
19 | class LogScale extends LinearScale {
|
---|
20 | //---------------
|
---|
21 | // CONSTRUCTOR
|
---|
22 |
|
---|
23 | // Log scale is specified using the log of min and max
|
---|
24 | function LogScale($min,$max,$type="y") {
|
---|
25 | $this->LinearScale($min,$max,$type);
|
---|
26 | $this->ticks = new LogTicks();
|
---|
27 | $this->name = 'log';
|
---|
28 | }
|
---|
29 |
|
---|
30 | //----------------
|
---|
31 | // PUBLIC METHODS
|
---|
32 |
|
---|
33 | // Translate between world and screen
|
---|
34 | function Translate($a) {
|
---|
35 | if( !is_numeric($a) ) {
|
---|
36 | if( $a != '' && $a != '-' && $a != 'x' )
|
---|
37 | JpGraphError::RaiseL(11001);
|
---|
38 | //('Your data contains non-numeric values.');
|
---|
39 | return 1;
|
---|
40 | }
|
---|
41 | if( $a < 0 ) {
|
---|
42 | JpGraphError::RaiseL(11002);
|
---|
43 | //("Negative data values can not be used in a log scale.");
|
---|
44 | exit(1);
|
---|
45 | }
|
---|
46 | if( $a==0 ) $a=1;
|
---|
47 | $a=log10($a);
|
---|
48 | return ceil($this->off + ($a*1.0 - $this->scale[0]) * $this->scale_factor);
|
---|
49 | }
|
---|
50 |
|
---|
51 | // Relative translate (don't include offset) usefull when we just want
|
---|
52 | // to know the relative position (in pixels) on the axis
|
---|
53 | function RelTranslate($a) {
|
---|
54 | if( !is_numeric($a) ) {
|
---|
55 | if( $a != '' && $a != '-' && $a != 'x' )
|
---|
56 | JpGraphError::RaiseL(11001);
|
---|
57 | //('Your data contains non-numeric values.');
|
---|
58 | return 1;
|
---|
59 | }
|
---|
60 | if( $a==0 ) $a=1;
|
---|
61 | $a=log10($a);
|
---|
62 | return round(($a*1.0 - $this->scale[0]) * $this->scale_factor);
|
---|
63 | }
|
---|
64 |
|
---|
65 | // Use bcpow() for increased precision
|
---|
66 | function GetMinVal() {
|
---|
67 | if( function_exists("bcpow") )
|
---|
68 | return round(bcpow(10,$this->scale[0],15),14);
|
---|
69 | else
|
---|
70 | return round(pow(10,$this->scale[0]),14);
|
---|
71 | }
|
---|
72 |
|
---|
73 | function GetMaxVal() {
|
---|
74 | if( function_exists("bcpow") )
|
---|
75 | return round(bcpow(10,$this->scale[1],15),14);
|
---|
76 | else
|
---|
77 | return round(pow(10,$this->scale[1]),14);
|
---|
78 | }
|
---|
79 |
|
---|
80 | // Logarithmic autoscaling is much simplier since we just
|
---|
81 | // set the min and max to logs of the min and max values.
|
---|
82 | // Note that for log autoscale the "maxstep" the fourth argument
|
---|
83 | // isn't used. This is just included to give the method the same
|
---|
84 | // signature as the linear counterpart.
|
---|
85 | function AutoScale($img,$min,$max,$maxsteps,$majend=true) {
|
---|
86 | if( $min==0 ) $min=1;
|
---|
87 |
|
---|
88 | if( $max <= 0 ) {
|
---|
89 | JpGraphError::RaiseL(11004);
|
---|
90 | //('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.');
|
---|
91 | }
|
---|
92 | if( is_numeric($this->autoscale_min) ) {
|
---|
93 | $smin = round($this->autoscale_min);
|
---|
94 | $smax = ceil(log10($max));
|
---|
95 | if( $min >= $max ) {
|
---|
96 | 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.');
|
---|
97 | }
|
---|
98 | }
|
---|
99 | else {
|
---|
100 | $smin = floor(log10($min));
|
---|
101 | if( is_numeric($this->autoscale_max) ) {
|
---|
102 | $smax = round($this->autoscale_max);
|
---|
103 | if( $smin >= $smax ) {
|
---|
104 | 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.');
|
---|
105 | }
|
---|
106 | }
|
---|
107 | else
|
---|
108 | $smax = ceil(log10($max));
|
---|
109 | }
|
---|
110 |
|
---|
111 | $this->Update($img,$smin,$smax);
|
---|
112 | }
|
---|
113 | //---------------
|
---|
114 | // PRIVATE METHODS
|
---|
115 | } // Class
|
---|
116 |
|
---|
117 | //===================================================
|
---|
118 | // CLASS LogTicks
|
---|
119 | // Description:
|
---|
120 | //===================================================
|
---|
121 | class LogTicks extends Ticks{
|
---|
122 | private $label_logtype=LOGLABELS_MAGNITUDE;
|
---|
123 | private $ticklabels_pos = array();
|
---|
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 | ?>
|
---|