Axis::StrokeLabels()   F
last analyzed

Complexity

Conditions 35
Paths 1968

Size

Total Lines 137
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 35
eloc 82
c 1
b 0
f 0
nc 1968
nop 3
dl 0
loc 137
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Axis
13
 * // Description: Defines X and Y axis. Notes that at the
14
 * // moment the code is not really good since the axis on
15
 * // several occasion must know wheter it's an X or Y axis.
16
 * // This was a design decision to make the code easier to
17
 * // follow.
18
 */
19
class Axis extends AxisPrototype
20
{
21
    public function __construct($img, $aScale, $color = 'black')
22
    {
23
        parent::__construct($img, $aScale, $color);
24
    }
25
26
    // Stroke the axis.
27
    public function Stroke($aOtherAxisScale, $aStrokeLabels = true)
28
    {
29
        if ($this->hide) {
30
            return;
31
        }
32
33
        if (is_numeric($this->pos)) {
0 ignored issues
show
introduced by
The condition is_numeric($this->pos) is always false.
Loading history...
34
            $pos = $aOtherAxisScale->Translate($this->pos);
35
        } else {
36
            // Default to minimum of other scale if pos not set
37
            if (($aOtherAxisScale->GetMinVal() >= 0 && $this->pos == false) || $this->pos == 'min') {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
38
                $pos = $aOtherAxisScale->scale_abs[0];
39
            } elseif ($this->pos == 'max') {
40
                $pos = $aOtherAxisScale->scale_abs[1];
41
            } else {
42
                // If negative set x-axis at 0
43
                $this->pos = 0;
44
                $pos       = $aOtherAxisScale->Translate(0);
45
            }
46
        }
47
48
        $pos += $this->iDeltaAbsPos;
49
        $this->img->SetLineWeight($this->weight);
50
        $this->img->SetColor($this->color);
51
        $this->img->SetFont($this->font_family, $this->font_style, $this->font_size);
52
53
        if ($this->scale->type == 'x') {
54
            if (!$this->hide_line) {
55
                // Stroke X-axis
56
                $this->img->FilledRectangle(
57
                    $this->img->left_margin,
58
                    $pos,
59
                    $this->img->width - $this->img->right_margin,
60
                    $pos + $this->weight - 1
61
                );
62
            }
63
            if ($this->title_side == SIDE_DOWN) {
64
                $y      = $pos + $this->img->GetFontHeight() + $this->title_margin + $this->title->margin;
65
                $yalign = 'top';
66
            } else {
67
                $y      = $pos - $this->img->GetFontHeight() - $this->title_margin - $this->title->margin;
68
                $yalign = 'bottom';
69
            }
70
71
            if ($this->title_adjust == 'high') {
72
                $this->title->SetPos($this->img->width - $this->img->right_margin, $y, 'right', $yalign);
73
            } elseif ($this->title_adjust == 'middle' || $this->title_adjust == 'center') {
74
                $this->title->SetPos(($this->img->width - $this->img->left_margin - $this->img->right_margin) / 2 + $this->img->left_margin, $y, 'center', $yalign);
75
            } elseif ($this->title_adjust == 'low') {
76
                $this->title->SetPos($this->img->left_margin, $y, 'left', $yalign);
77
            } else {
78
                Util\JpGraphError::RaiseL(25060, $this->title_adjust); //('Unknown alignment specified for X-axis title. ('.$this->title_adjust.')');
79
            }
80
        } elseif ($this->scale->type == 'y') {
81
            // Add line weight to the height of the axis since
82
            // the x-axis could have a width>1 and we want the axis to fit nicely together.
83
            if (!$this->hide_line) {
84
                // Stroke Y-axis
85
                $this->img->FilledRectangle(
86
                    $pos - $this->weight + 1,
87
                    $this->img->top_margin,
88
                    $pos,
89
                    $this->img->height - $this->img->bottom_margin + $this->weight - 1
90
                );
91
            }
92
93
            $x = $pos;
94
            if ($this->title_side == SIDE_LEFT) {
95
                $x -= $this->title_margin;
96
                $x -= $this->title->margin;
97
                $halign = 'right';
98
            } else {
99
                $x += $this->title_margin;
100
                $x += $this->title->margin;
101
                $halign = 'left';
102
            }
103
            // If the user has manually specified an hor. align
104
            // then we override the automatic settings with this
105
            // specifed setting. Since default is 'left' we compare
106
            // with that. (This means a manually set 'left' align
107
            // will have no effect.)
108
            if ($this->title->halign != 'left') {
109
                $halign = $this->title->halign;
110
            }
111
            if ($this->title_adjust == 'high') {
112
                $this->title->SetPos($x, $this->img->top_margin, $halign, 'top');
113
            } elseif ($this->title_adjust == 'middle' || $this->title_adjust == 'center') {
114
                $this->title->SetPos($x, ($this->img->height - $this->img->top_margin - $this->img->bottom_margin) / 2 + $this->img->top_margin, $halign, 'center');
115
            } elseif ($this->title_adjust == 'low') {
116
                $this->title->SetPos($x, $this->img->height - $this->img->bottom_margin, $halign, 'bottom');
117
            } else {
118
                Util\JpGraphError::RaiseL(25061, $this->title_adjust); //('Unknown alignment specified for Y-axis title. ('.$this->title_adjust.')');
119
            }
120
        }
121
        $this->scale->ticks->Stroke($this->img, $this->scale, $pos);
122
        if ($aStrokeLabels) {
123
            if (!$this->hide_labels) {
124
                $this->StrokeLabels($pos);
125
            }
126
            $this->title->Stroke($this->img);
127
        }
128
    }
129
130
    /**
131
     * PRIVATE METHODS
132
     * // Draw all the tick labels on major tick marks.
133
     *
134
     * @param mixed $aPos
135
     * @param mixed $aMinor
136
     * @param mixed $aAbsLabel
137
     */
138
    public function StrokeLabels($aPos, $aMinor = false, $aAbsLabel = false)
0 ignored issues
show
Unused Code introduced by
The parameter $aMinor is not used and could be removed. ( Ignorable by Annotation )

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

138
    public function StrokeLabels($aPos, /** @scrutinizer ignore-unused */ $aMinor = false, $aAbsLabel = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140
        if (is_array($this->label_color) && safe_count($this->label_color) > 3) {
141
            $this->ticks_label_colors = $this->label_color;
142
            $this->img->SetColor($this->label_color[0]);
143
        } else {
144
            $this->img->SetColor($this->label_color);
145
        }
146
        $this->img->SetFont($this->font_family, $this->font_style, $this->font_size);
147
        $yoff = $this->img->GetFontHeight() / 2;
0 ignored issues
show
Unused Code introduced by
The assignment to $yoff is dead and can be removed.
Loading history...
148
149
        // Only draw labels at major tick marks
150
        $nbr = safe_count($this->scale->ticks->maj_ticks_label);
151
152
        // We have the option to not-display the very first mark
153
        // (Usefull when the first label might interfere with another
154
        // axis.)
155
        $i = $this->show_first_label ? 0 : 1;
156
        if (!$this->show_last_label) {
157
            --$nbr;
158
        }
159
        // Now run through all labels making sure we don't overshoot the end
160
        // of the scale.
161
        $ncolor = 0;
162
        if (isset($this->ticks_label_colors)) {
163
            $ncolor = safe_count($this->ticks_label_colors);
164
        }
165
        while ($i < $nbr) {
166
            // $tpos holds the absolute text position for the label
167
            $tpos = $this->scale->ticks->maj_ticklabels_pos[$i];
168
169
            // Note. the $limit is only used for the x axis since we
170
            // might otherwise overshoot if the scale has been centered
171
            // This is due to us "loosing" the last tick mark if we center.
172
            if ($this->scale->type == 'x' && $tpos > $this->img->width - $this->img->right_margin + 1) {
173
                return;
174
            }
175
            // we only draw every $label_step label
176
            if (($i % $this->label_step) == 0) {
177
                // Set specific label color if specified
178
                if ($ncolor > 0) {
179
                    $this->img->SetColor($this->ticks_label_colors[$i % $ncolor]);
180
                }
181
182
                // If the label has been specified use that and in other case
183
                // just label the mark with the actual scale value
184
                $m = $this->scale->ticks->GetMajor();
185
186
                // ticks_label has an entry for each data point and is the array
187
                // that holds the labels set by the user. If the user hasn't
188
                // specified any values we use whats in the automatically asigned
189
                // labels in the maj_ticks_label
190
                if (isset($this->ticks_label[$i * $m])) {
191
                    $label = $this->ticks_label[$i * $m];
192
                } else {
193
                    if ($aAbsLabel) {
194
                        $label = abs($this->scale->ticks->maj_ticks_label[$i]);
195
                    } else {
196
                        $label = $this->scale->ticks->maj_ticks_label[$i];
197
                    }
198
199
                    // We number the scale from 1 and not from 0 so increase by one
200
                    if ($this->scale->textscale &&
201
                        $this->scale->ticks->label_formfunc == '' &&
202
                        !$this->scale->ticks->HaveManualLabels()) {
203
                        ++$label;
204
                    }
205
                }
206
207
                if ($this->scale->type == 'x') {
208
                    if ($this->labelPos == SIDE_DOWN) {
209
                        if ($this->label_angle == 0 || $this->label_angle == 90) {
210
                            if ($this->label_halign == '' && $this->label_valign == '') {
211
                                $this->img->SetTextAlign('center', 'top');
212
                            } else {
213
                                $this->img->SetTextAlign($this->label_halign, $this->label_valign);
214
                            }
215
                        } else {
216
                            if ($this->label_halign == '' && $this->label_valign == '') {
217
                                $this->img->SetTextAlign('right', 'top');
218
                            } else {
219
                                $this->img->SetTextAlign($this->label_halign, $this->label_valign);
220
                            }
221
                        }
222
                        $this->img->StrokeText(
223
                            $tpos,
224
                            $aPos + $this->tick_label_margin,
225
                            $label,
226
                            $this->label_angle,
227
                            $this->label_para_align
228
                        );
229
                    } else {
230
                        if ($this->label_angle == 0 || $this->label_angle == 90) {
231
                            if ($this->label_halign == '' && $this->label_valign == '') {
232
                                $this->img->SetTextAlign('center', 'bottom');
233
                            } else {
234
                                $this->img->SetTextAlign($this->label_halign, $this->label_valign);
235
                            }
236
                        } else {
237
                            if ($this->label_halign == '' && $this->label_valign == '') {
238
                                $this->img->SetTextAlign('right', 'bottom');
239
                            } else {
240
                                $this->img->SetTextAlign($this->label_halign, $this->label_valign);
241
                            }
242
                        }
243
                        $this->img->StrokeText(
244
                            $tpos,
245
                            $aPos - $this->tick_label_margin - 1,
246
                            $label,
247
                            $this->label_angle,
248
                            $this->label_para_align
249
                        );
250
                    }
251
                } else {
252
                    // scale->type == "y"
253
                    //if( $this->label_angle!=0 )
254
                    //Util\JpGraphError::Raise(" Labels at an angle are not supported on Y-axis");
255
                    if ($this->labelPos == SIDE_LEFT) {
256
                        // To the left of y-axis
257
                        if ($this->label_halign == '' && $this->label_valign == '') {
258
                            $this->img->SetTextAlign('right', 'center');
259
                        } else {
260
                            $this->img->SetTextAlign($this->label_halign, $this->label_valign);
261
                        }
262
                        $this->img->StrokeText($aPos - $this->tick_label_margin, $tpos, $label, $this->label_angle, $this->label_para_align);
263
                    } else {
264
                        // To the right of the y-axis
265
                        if ($this->label_halign == '' && $this->label_valign == '') {
266
                            $this->img->SetTextAlign('left', 'center');
267
                        } else {
268
                            $this->img->SetTextAlign($this->label_halign, $this->label_valign);
269
                        }
270
                        $this->img->StrokeText($aPos + $this->tick_label_margin, $tpos, $label, $this->label_angle, $this->label_para_align);
271
                    }
272
                }
273
            }
274
            ++$i;
275
        }
276
    }
277
}
278