LinearScale   F
last analyzed

Complexity

Total Complexity 98

Size/Duplication

Total Lines 598
Duplicated Lines 0 %

Test Coverage

Coverage 79.8%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 310
dl 0
loc 598
ccs 241
cts 302
cp 0.798
rs 2
c 2
b 0
f 0
wmc 98

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 8 2
A __set() 0 3 1
A IsSpecified() 0 8 2
F IntAutoScale() 0 114 20
A CalcTicksFreeze() 0 23 3
A __construct() 0 9 2
A Update() 0 5 1
A GetMaxVal() 0 3 1
A SetIntScale() 0 3 1
A SetGrace() 0 7 3
A GetMinVal() 0 3 1
A RelTranslate() 0 11 5
A InitConstants() 0 20 4
A IntCalcTicksFreeze() 0 26 5
A Translate() 0 11 5
A SetConstants() 0 16 2
A MatchMin3() 0 14 4
B CalcTicks() 0 41 6
B IntCalcTicks() 0 43 8
A SetAutoTicks() 0 3 1
A SetAutoMin() 0 3 1
A SetAutoMax() 0 3 1
F AutoScale() 0 102 19

How to fix   Complexity   

Complex Class

Complex classes like LinearScale often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use LinearScale, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * JPGraph v4.0.3
5
 */
6
7
namespace Amenadiel\JpGraph\Graph;
8
9
use Amenadiel\JpGraph\Util;
10
11
/**
12
 * @class LinearScale
13
 * // Description: Handle linear scaling between screen and world
14
 */
15
class LinearScale
16
{
17
    public $textscale = false; // Just a flag to let the Plot class find out if
18
    // we are a textscale or not. This is a cludge since
19
    // this information is available in Graph::axtype but
20
    // we don't have access to the graph object in the Plots
21
    // stroke method. So we let graph store the status here
22
    // when the linear scale is created. A real cludge...
23
    public $type; // is this x or y scale ?
24
    public $ticks; // Store ticks
25
    public $text_scale_off = 0;
26
    public $scale_abs      = [0, 0];
27
    public $scale_factor; // Scale factor between world and screen
28
    public $off; // Offset between image edge and plot area
29
    public $scale      = [0, 0];
30
    public $name       = 'lin';
31
    public $auto_ticks = false; // When using manual scale should the ticks be automatically set?
32
    public $world_abs_size; // Plot area size in pixels (Needed public in jpgraph_radar.php)
33
    public $intscale         = false; // Restrict autoscale to integers
34
    protected $autoscale_min = false; // Forced minimum value, auto determine max
35
    protected $autoscale_max = false; // Forced maximum value, auto determine min
36
    private $gracetop        = 0;
37
    private $gracebottom     = 0;
38
39
    private $_world_size; // Plot area size in world coordinates
40
41 19
    public function __construct($aMin = 0, $aMax = 0, $aType = 'y')
42
    {
43 19
        assert($aType == 'x' || $aType == 'y');
44 19
        assert($aMin <= $aMax);
45
46 19
        $this->type       = $aType;
47 19
        $this->scale      = [$aMin, $aMax];
48 19
        $this->world_size = $aMax - $aMin;
49 19
        $this->ticks      = new LinearTicks();
50 19
    }
51
52
    // Check if scale is set or if we should autoscale
53
    // We should do this is either scale or ticks has not been set
54 19
    public function IsSpecified()
55
    {
56 19
        if ($this->GetMinVal() == $this->GetMaxVal()) {
57
            // Scale not set
58 18
            return false;
59
        }
60
61 5
        return true;
62
    }
63
64
    // Set the minimum data value when the autoscaling is used.
65
    // Usefull if you want a fix minimum (like 0) but have an
66
    // automatic maximum
67
    public function SetAutoMin($aMin)
68
    {
69
        $this->autoscale_min = $aMin;
70
    }
71
72
    // Set the minimum data value when the autoscaling is used.
73
    // Usefull if you want a fix minimum (like 0) but have an
74
    // automatic maximum
75
    public function SetAutoMax($aMax)
76
    {
77
        $this->autoscale_max = $aMax;
78
    }
79
80
    // If the user manually specifies a scale should the ticks
81
    // still be set automatically?
82
    public function SetAutoTicks($aFlag = true)
83
    {
84
        $this->auto_ticks = $aFlag;
85
    }
86
87
    // Specify scale "grace" value (top and bottom)
88 2
    public function SetGrace($aGraceTop, $aGraceBottom = 0)
89
    {
90 2
        if ($aGraceTop < 0 || $aGraceBottom < 0) {
91
            Util\JpGraphError::RaiseL(25069); //(" Grace must be larger then 0");
92
        }
93 2
        $this->gracetop    = $aGraceTop;
94 2
        $this->gracebottom = $aGraceBottom;
95 2
    }
96
97
    // Get the minimum value in the scale
98 19
    public function GetMinVal()
99
    {
100 19
        return $this->scale[0];
101
    }
102
103
    // get maximum value for scale
104 19
    public function GetMaxVal()
105
    {
106 19
        return $this->scale[1];
107
    }
108
109
    // Specify a new min/max value for sclae
110 19
    public function Update($aImg, $aMin, $aMax)
111
    {
112 19
        $this->scale      = [$aMin, $aMax];
113 19
        $this->world_size = $aMax - $aMin;
114 19
        $this->InitConstants($aImg);
115 19
    }
116
117
    // Translate between world and screen
118 19
    public function Translate($aCoord)
119
    {
120 19
        if (!is_numeric($aCoord)) {
121 2
            if ($aCoord != '' && $aCoord != '-' && $aCoord != 'x') {
122
                Util\JpGraphError::RaiseL(25070); //('Your data contains non-numeric values.');
123
            }
124
125 2
            return 0;
126
        }
127
128 19
        return round($this->off + ($aCoord - $this->scale[0]) * $this->scale_factor);
129
    }
130
131
    // Relative translate (don't include offset) usefull when we just want
132
    // to know the relative position (in pixels) on the axis
133
    public function RelTranslate($aCoord)
134
    {
135
        if (!is_numeric($aCoord)) {
136
            if ($aCoord != '' && $aCoord != '-' && $aCoord != 'x') {
137
                Util\JpGraphError::RaiseL(25070); //('Your data contains non-numeric values.');
138
            }
139
140
            return 0;
141
        }
142
143
        return ($aCoord - $this->scale[0]) * $this->scale_factor;
144
    }
145
146
    // Restrict autoscaling to only use integers
147 5
    public function SetIntScale($aIntScale = true)
148
    {
149 5
        $this->intscale = $aIntScale;
150 5
    }
151
152
    // Calculate an integer autoscale
153 5
    public function IntAutoScale($img, $min, $max, $maxsteps, $majend = true)
154
    {
155
        // Make sure limits are integers
156 5
        $min = floor($min);
157 5
        $max = ceil($max);
158 5
        if (abs($min - $max) == 0) {
159
            --$min;
160
            ++$max;
161
        }
162 5
        $maxsteps = floor($maxsteps);
163
164 5
        $gracetop    = round(($this->gracetop / 100.0) * abs($max - $min));
165 5
        $gracebottom = round(($this->gracebottom / 100.0) * abs($max - $min));
166 5
        if (is_numeric($this->autoscale_min)) {
0 ignored issues
show
introduced by
The condition is_numeric($this->autoscale_min) is always false.
Loading history...
167
            $min = ceil($this->autoscale_min);
168
            if ($min >= $max) {
169
                Util\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.');
170
            }
171
        }
172
173 5
        if (is_numeric($this->autoscale_max)) {
0 ignored issues
show
introduced by
The condition is_numeric($this->autoscale_max) is always false.
Loading history...
174
            $max = ceil($this->autoscale_max);
175
            if ($min >= $max) {
176
                Util\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.');
177
            }
178
        }
179
180 5
        if (abs($min - $max) == 0) {
181
            ++$max;
182
            --$min;
183
        }
184
185 5
        $min -= $gracebottom;
186 5
        $max += $gracetop;
187
188
        // First get tickmarks as multiples of 1, 10, ...
189 5
        if ($majend) {
190 3
            list($num1steps, $adj1min, $adj1max, $maj1step) = $this->IntCalcTicks($maxsteps, $min, $max, 1);
191
        } else {
192 3
            $adj1min                    = $min;
193 3
            $adj1max                    = $max;
194 3
            list($num1steps, $maj1step) = $this->IntCalcTicksFreeze($maxsteps, $min, $max, 1);
195
        }
196
197 5
        if (abs($min - $max) > 2) {
198
            // Then get tick marks as 2:s 2, 20, ...
199 5
            if ($majend) {
200 3
                list($num2steps, $adj2min, $adj2max, $maj2step) = $this->IntCalcTicks($maxsteps, $min, $max, 5);
201
            } else {
202 3
                $adj2min                    = $min;
203 3
                $adj2max                    = $max;
204 5
                list($num2steps, $maj2step) = $this->IntCalcTicksFreeze($maxsteps, $min, $max, 5);
205
            }
206
        } else {
207
            $num2steps = 10000; // Dummy high value so we don't choose this
208
        }
209
210 5
        if (abs($min - $max) > 5) {
211
            // Then get tickmarks as 5:s 5, 50, 500, ...
212 5
            if ($majend) {
213 3
                list($num5steps, $adj5min, $adj5max, $maj5step) = $this->IntCalcTicks($maxsteps, $min, $max, 2);
214
            } else {
215 3
                $adj5min                    = $min;
216 3
                $adj5max                    = $max;
217 5
                list($num5steps, $maj5step) = $this->IntCalcTicksFreeze($maxsteps, $min, $max, 2);
218
            }
219
        } else {
220 1
            $num5steps = 10000; // Dummy high value so we don't choose this
221
        }
222
223
        // Check to see whichof 1:s, 2:s or 5:s fit better with
224
        // the requested number of major ticks
225 5
        $match1 = abs($num1steps - $maxsteps);
226 5
        $match2 = abs($num2steps - $maxsteps);
227 5
        if (!empty($maj5step) && $maj5step > 1) {
228 5
            $match5 = abs($num5steps - $maxsteps);
229
        } else {
230 1
            $match5 = 10000; // Dummy high value
231
        }
232
233
        // Compare these three values and see which is the closest match
234
        // We use a 0.6 weight to gravitate towards multiple of 5:s
235 5
        if ($match1 < $match2) {
236 5
            if ($match1 < $match5) {
237 5
                $r = 1;
238
            } else {
239 5
                $r = 3;
240
            }
241
        } else {
242 3
            if ($match2 < $match5) {
243 3
                $r = 2;
244
            } else {
245
                $r = 3;
246
            }
247
        }
248
        // Minsteps are always the same as maxsteps for integer scale
249
        switch ($r) {
250 5
            case 1:
251 5
                $this->ticks->Set($maj1step, $maj1step);
252 5
                $this->Update($img, $adj1min, $adj1max);
253
254 5
                break;
255 4
            case 2:
256 3
                $this->ticks->Set($maj2step, $maj2step);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $maj2step does not seem to be defined for all execution paths leading up to this point.
Loading history...
257 3
                $this->Update($img, $adj2min, $adj2max);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $adj2min does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $adj2max does not seem to be defined for all execution paths leading up to this point.
Loading history...
258
259 3
                break;
260 3
            case 3:
261 3
                $this->ticks->Set($maj5step, $maj5step);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $maj5step does not seem to be defined for all execution paths leading up to this point.
Loading history...
262 3
                $this->Update($img, $adj5min, $adj5max);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $adj5max does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $adj5min does not seem to be defined for all execution paths leading up to this point.
Loading history...
263
264 3
                break;
265
            default:
266
                Util\JpGraphError::RaiseL(25073, $r); //('Internal error. Integer scale algorithm comparison out of bound (r=$r)');
267
        }
268 5
    }
269
270
    // Calculate autoscale. Used if user hasn't given a scale and ticks
271
    // $maxsteps is the maximum number of major tickmarks allowed.
272 19
    public function AutoScale($img, $min, $max, $maxsteps, $majend = true)
273
    {
274 19
        if (!is_numeric($min) || !is_numeric($max)) {
275
            Util\JpGraphError::Raise(25044);
276
        }
277
278 19
        if ($this->intscale) {
279 5
            $this->IntAutoScale($img, $min, $max, $maxsteps, $majend);
280
281 5
            return;
282
        }
283 18
        if (abs($min - $max) < 0.00001) {
284
            // We need some difference to be able to autoscale
285
            // make it 5% above and 5% below value
286 1
            if ($min == 0 && $max == 0) {
287
                // Special case
288
                $min = -1;
289
                $max = 1;
290
            } else {
291 1
                $delta = (abs($max) + abs($min)) * 0.005;
292 1
                $min -= $delta;
293 1
                $max += $delta;
294
            }
295
        }
296
297 18
        $gracetop    = ($this->gracetop / 100.0) * abs($max - $min);
298 18
        $gracebottom = ($this->gracebottom / 100.0) * abs($max - $min);
299 18
        if (is_numeric($this->autoscale_min)) {
0 ignored issues
show
introduced by
The condition is_numeric($this->autoscale_min) is always false.
Loading history...
300
            $min = $this->autoscale_min;
301
            if ($min >= $max) {
302
                Util\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.');
303
            }
304
            if (abs($min - $max) < 0.001) {
305
                $max *= 1.2;
306
            }
307
        }
308
309 18
        if (is_numeric($this->autoscale_max)) {
0 ignored issues
show
introduced by
The condition is_numeric($this->autoscale_max) is always false.
Loading history...
310
            $max = $this->autoscale_max;
311
            if ($min >= $max) {
312
                Util\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.');
313
            }
314
            if (abs($min - $max) < 0.001) {
315
                $min *= 0.8;
316
            }
317
        }
318
319 18
        $min -= $gracebottom;
320 18
        $max += $gracetop;
321
322
        // First get tickmarks as multiples of 0.1, 1, 10, ...
323 18
        if ($majend) {
324 17
            list($num1steps, $adj1min, $adj1max, $min1step, $maj1step) = $this->CalcTicks($maxsteps, $min, $max, 1, 2);
325
        } else {
326 4
            $adj1min                               = $min;
327 4
            $adj1max                               = $max;
328 4
            list($num1steps, $min1step, $maj1step) = $this->CalcTicksFreeze($maxsteps, $min, $max, 1, 2, false);
0 ignored issues
show
Unused Code introduced by
The call to Amenadiel\JpGraph\Graph\...cale::CalcTicksFreeze() has too many arguments starting with false. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

328
            /** @scrutinizer ignore-call */ 
329
            list($num1steps, $min1step, $maj1step) = $this->CalcTicksFreeze($maxsteps, $min, $max, 1, 2, false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
329
        }
330
331
        // Then get tick marks as 2:s 0.2, 2, 20, ...
332 18
        if ($majend) {
333 17
            list($num2steps, $adj2min, $adj2max, $min2step, $maj2step) = $this->CalcTicks($maxsteps, $min, $max, 5, 2);
334
        } else {
335 4
            $adj2min                               = $min;
336 4
            $adj2max                               = $max;
337 4
            list($num2steps, $min2step, $maj2step) = $this->CalcTicksFreeze($maxsteps, $min, $max, 5, 2, false);
338
        }
339
340
        // Then get tickmarks as 5:s 0.05, 0.5, 5, 50, ...
341 18
        if ($majend) {
342 17
            list($num5steps, $adj5min, $adj5max, $min5step, $maj5step) = $this->CalcTicks($maxsteps, $min, $max, 2, 5);
343
        } else {
344 4
            $adj5min                               = $min;
345 4
            $adj5max                               = $max;
346 4
            list($num5steps, $min5step, $maj5step) = $this->CalcTicksFreeze($maxsteps, $min, $max, 2, 5, false);
347
        }
348
349
        // Check to see whichof 1:s, 2:s or 5:s fit better with
350
        // the requested number of major ticks
351 18
        $match1 = abs($num1steps - $maxsteps);
352 18
        $match2 = abs($num2steps - $maxsteps);
353 18
        $match5 = abs($num5steps - $maxsteps);
354
355
        // Compare these three values and see which is the closest match
356
        // We use a 0.8 weight to gravitate towards multiple of 5:s
357 18
        $r = $this->MatchMin3($match1, $match2, $match5, 0.8);
358
        switch ($r) {
359 18
            case 1:
360 9
                $this->Update($img, $adj1min, $adj1max);
361 9
                $this->ticks->Set($maj1step, $min1step);
362
363 9
                break;
364 15
            case 2:
365 8
                $this->Update($img, $adj2min, $adj2max);
366 8
                $this->ticks->Set($maj2step, $min2step);
367
368 8
                break;
369 9
            case 3:
370 9
                $this->Update($img, $adj5min, $adj5max);
371 9
                $this->ticks->Set($maj5step, $min5step);
372
373 9
                break;
374
        }
375 18
    }
376
377
    /**
378
     * PRIVATE METHODS.
379
     *
380
     * @param mixed $img
381
     */
382
383
    // This method recalculates all constants that are depending on the
384
    // margins in the image. If the margins in the image are changed
385
    // this method should be called for every scale that is registred with
386
    // that image. Should really be installed as an observer of that image.
387 19
    public function InitConstants($img)
388
    {
389 19
        if ($this->type == 'x') {
390 19
            $this->world_abs_size = $img->width - $img->left_margin - $img->right_margin;
391 19
            $this->off            = $img->left_margin;
392 19
            $this->scale_factor   = 0;
393 19
            if ($this->world_size > 0) {
394 19
                $this->scale_factor = $this->world_abs_size / ($this->world_size * 1.0);
395
            }
396
        } else {
397
            // y scale
398 19
            $this->world_abs_size = $img->height - $img->top_margin - $img->bottom_margin;
399 19
            $this->off            = $img->top_margin + $this->world_abs_size;
400 19
            $this->scale_factor   = 0;
401 19
            if ($this->world_size > 0) {
402 19
                $this->scale_factor = -$this->world_abs_size / ($this->world_size * 1.0);
403
            }
404
        }
405 19
        $size            = $this->world_size * $this->scale_factor;
406 19
        $this->scale_abs = [$this->off, $this->off + $size];
407 19
    }
408
409
    // Initialize the conversion constants for this scale
410
    // This tries to pre-calculate as much as possible to speed up the
411
    // actual conversion (with Translate()) later on
412
    // $start =scale start in absolute pixels (for x-scale this is an y-position
413
    //     and for an y-scale this is an x-position
414
    // $len   =absolute length in pixels of scale
415
    public function SetConstants($aStart, $aLen)
416
    {
417
        $this->world_abs_size = $aLen;
418
        $this->off            = $aStart;
419
420
        if ($this->world_size <= 0) {
421
            // This should never ever happen !!
422
            Util\JpGraphError::RaiseL(25074);
423
            //("You have unfortunately stumbled upon a bug in JpGraph. It seems like the scale range is ".$this->world_size." [for ".$this->type." scale] <br> Please report Bug #01 to [email protected] and include the script that gave this error. This problem could potentially be caused by trying to use \"illegal\" values in the input data arrays (like trying to send in strings or only NULL values) which causes the autoscaling to fail.");
424
        }
425
426
        // scale_factor = number of pixels per world unit
427
        $this->scale_factor = $this->world_abs_size / ($this->world_size * 1.0);
428
429
        // scale_abs = start and end points of scale in absolute pixels
430
        $this->scale_abs = [$this->off, $this->off + $this->world_size * $this->scale_factor];
431
    }
432
433
    // Calculate number of ticks steps with a specific division
434
    // $a is the divisor of 10**x to generate the first maj tick intervall
435
    // $a=1, $b=2 give major ticks with multiple of 10, ...,0.1,1,10,...
436
    // $a=5, $b=2 give major ticks with multiple of 2:s ...,0.2,2,20,...
437
    // $a=2, $b=5 give major ticks with multiple of 5:s ...,0.5,5,50,...
438
    // We return a vector of
439
    //  [$numsteps,$adjmin,$adjmax,$minstep,$majstep]
440
    // If $majend==true then the first and last marks on the axis will be major
441
    // labeled tick marks otherwise it will be adjusted to the closest min tick mark
442 17
    public function CalcTicks($maxsteps, $min, $max, $a, $b, $majend = true)
443
    {
444 17
        $diff = $max - $min;
445 17
        if ($diff == 0) {
446
            $ld = 0;
447
        } else {
448 17
            $ld = floor(log10($diff));
449
        }
450
451
        // Gravitate min towards zero if we are close
452 17
        if ($min > 0 && $min < pow(10, $ld)) {
453 10
            $min = 0;
454
        }
455
456
        //$majstep=pow(10,$ld-1)/$a;
457 17
        $majstep = pow(10, $ld) / $a;
458 17
        $minstep = $majstep / $b;
459
460 17
        $adjmax   = ceil($max / $minstep) * $minstep;
461 17
        $adjmin   = floor($min / $minstep) * $minstep;
462 17
        $adjdiff  = $adjmax - $adjmin;
463 17
        $numsteps = $adjdiff / $majstep;
464
465 17
        while ($numsteps > $maxsteps) {
466 16
            $majstep  = pow(10, $ld) / $a;
467 16
            $numsteps = $adjdiff / $majstep;
468 16
            ++$ld;
469
        }
470
471 17
        $minstep = $majstep / $b;
472 17
        $adjmin  = floor($min / $minstep) * $minstep;
473 17
        $adjdiff = $adjmax - $adjmin;
0 ignored issues
show
Unused Code introduced by
The assignment to $adjdiff is dead and can be removed.
Loading history...
474 17
        if ($majend) {
475 17
            $adjmin  = floor($min / $majstep) * $majstep;
476 17
            $adjdiff = $adjmax - $adjmin;
477 17
            $adjmax  = ceil($adjdiff / $majstep) * $majstep + $adjmin;
478
        } else {
479
            $adjmax = ceil($max / $minstep) * $minstep;
480
        }
481
482 17
        return [$numsteps, $adjmin, $adjmax, $minstep, $majstep];
483
    }
484
485 4
    public function CalcTicksFreeze($maxsteps, $min, $max, $a, $b)
486
    {
487
        // Same as CalcTicks but don't adjust min/max values
488 4
        $diff = $max - $min;
489 4
        if ($diff == 0) {
490
            $ld = 0;
491
        } else {
492 4
            $ld = floor(log10($diff));
493
        }
494
495
        //$majstep=pow(10,$ld-1)/$a;
496 4
        $majstep  = pow(10, $ld) / $a;
497 4
        $minstep  = $majstep / $b;
0 ignored issues
show
Unused Code introduced by
The assignment to $minstep is dead and can be removed.
Loading history...
498 4
        $numsteps = floor($diff / $majstep);
499
500 4
        while ($numsteps > $maxsteps) {
501 2
            $majstep  = pow(10, $ld) / $a;
502 2
            $numsteps = floor($diff / $majstep);
503 2
            ++$ld;
504
        }
505 4
        $minstep = $majstep / $b;
506
507 4
        return [$numsteps, $minstep, $majstep];
508
    }
509
510 3
    public function IntCalcTicks($maxsteps, $min, $max, $a, $majend = true)
511
    {
512 3
        $diff = $max - $min;
513 3
        if ($diff == 0) {
514
            Util\JpGraphError::RaiseL(25075); //('Can\'t automatically determine ticks since min==max.');
515
        } else {
516 3
            $ld = floor(log10($diff));
517
        }
518
519
        // Gravitate min towards zero if we are close
520 3
        if ($min > 0 && $min < pow(10, $ld)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ld does not seem to be defined for all execution paths leading up to this point.
Loading history...
521
            $min = 0;
522
        }
523 3
        if ($ld == 0) {
524 1
            $ld = 1;
525
        }
526 3
        if ($a == 1) {
527 3
            $majstep = 1;
528
        } else {
529 3
            $majstep = pow(10, $ld) / $a;
530
        }
531 3
        $adjmax = ceil($max / $majstep) * $majstep;
532
533 3
        $adjmin   = floor($min / $majstep) * $majstep;
534 3
        $adjdiff  = $adjmax - $adjmin;
535 3
        $numsteps = $adjdiff / $majstep;
536 3
        while ($numsteps > $maxsteps) {
537 2
            $majstep  = pow(10, $ld) / $a;
538 2
            $numsteps = $adjdiff / $majstep;
539 2
            ++$ld;
540
        }
541
542 3
        $adjmin  = floor($min / $majstep) * $majstep;
543 3
        $adjdiff = $adjmax - $adjmin;
0 ignored issues
show
Unused Code introduced by
The assignment to $adjdiff is dead and can be removed.
Loading history...
544 3
        if ($majend) {
545 3
            $adjmin  = floor($min / $majstep) * $majstep;
546 3
            $adjdiff = $adjmax - $adjmin;
547 3
            $adjmax  = ceil($adjdiff / $majstep) * $majstep + $adjmin;
548
        } else {
549
            $adjmax = ceil($max / $majstep) * $majstep;
550
        }
551
552 3
        return [$numsteps, $adjmin, $adjmax, $majstep];
553
    }
554
555 3
    public function IntCalcTicksFreeze($maxsteps, $min, $max, $a)
556
    {
557
        // Same as IntCalcTick but don't change min/max values
558 3
        $diff = $max - $min;
559 3
        if ($diff == 0) {
560
            Util\JpGraphError::RaiseL(25075); //('Can\'t automatically determine ticks since min==max.');
561
        } else {
562 3
            $ld = floor(log10($diff));
563
        }
564 3
        if ($ld == 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ld does not seem to be defined for all execution paths leading up to this point.
Loading history...
565 1
            $ld = 1;
566
        }
567 3
        if ($a == 1) {
568 3
            $majstep = 1;
569
        } else {
570 3
            $majstep = pow(10, $ld) / $a;
571
        }
572
573 3
        $numsteps = floor($diff / $majstep);
574 3
        while ($numsteps > $maxsteps) {
575 3
            $majstep  = pow(10, $ld) / $a;
576 3
            $numsteps = floor($diff / $majstep);
577 3
            ++$ld;
578
        }
579
580 3
        return [$numsteps, $majstep];
581
    }
582
583
    // Determine the minimum of three values witha  weight for last value
584 18
    public function MatchMin3($a, $b, $c, $weight)
585
    {
586 18
        if ($a < $b) {
587 14
            if ($a < ($c * $weight)) {
588 9
                return 1; // $a smallest
589
            }
590
591 9
            return 3; // $c smallest
592
        }
593 8
        if ($b < ($c * $weight)) {
594 8
            return 2; // $b smallest
595
        }
596
597
        return 3; // $c smallest
598
    }
599
600 19
    public function __get($name)
601
    {
602 19
        $variable_name = '_' . $name;
603
604 19
        if (isset($this->{$variable_name})) {
605 19
            return $this->{$variable_name} * SUPERSAMPLING_SCALE;
606
        }
607
        Util\JpGraphError::RaiseL('25132', $name);
608
    }
609
610 19
    public function __set($name, $value)
611
    {
612 19
        $this->{'_' . $name} = $value;
613 19
    }
614
} // @class
615