Grid::SetWeight()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
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 Grid
13
 * // Description: responsible for drawing grid lines in graph
14
 */
15
class Grid
16
{
17
    protected $img;
18
    protected $scale;
19
    protected $majorcolor  = '#CCCCCC';
20
    protected $minorcolor  = '#DDDDDD';
21
    protected $majortype   = 'solid';
22
    protected $minortype   = 'solid';
23
    protected $show        = false;
24
    protected $showMinor   = false;
25
    protected $majorweight = 1;
26
    protected $minorweight = 1;
27
    protected $fill        = false;
28
    protected $fillcolor   = ['#EFEFEF', '#BBCCFF'];
29
30 19
    public function __construct($aAxis)
31
    {
32 19
        $this->scale = $aAxis->scale;
33 19
        $this->img   = $aAxis->img;
34 19
    }
35
36 19
    public function SetColor($aMajColor, $aMinColor = false)
37
    {
38 19
        $this->majorcolor = $aMajColor;
39 19
        if ($aMinColor === false) {
40 19
            $aMinColor = $aMajColor;
41
        }
42 19
        $this->minorcolor = $aMinColor;
43 19
    }
44
45
    public function SetWeight($aMajorWeight, $aMinorWeight = 1)
46
    {
47
        $this->majorweight = $aMajorWeight;
48
        $this->minorweight = $aMinorWeight;
49
    }
50
51
    // Specify if grid should be dashed, dotted or solid
52 5
    public function SetLineStyle($aMajorType, $aMinorType = 'solid')
53
    {
54 5
        $this->majortype = $aMajorType;
55 5
        $this->minortype = $aMinorType;
56 5
    }
57
58 1
    public function SetStyle($aMajorType, $aMinorType = 'solid')
59
    {
60 1
        $this->SetLineStyle($aMajorType, $aMinorType);
61 1
    }
62
63
    // Decide if both major and minor grid should be displayed
64 19
    public function Show($aShowMajor = true, $aShowMinor = false)
65
    {
66 19
        $this->show      = $aShowMajor;
67 19
        $this->showMinor = $aShowMinor;
68 19
    }
69
70 19
    public function SetFill($aFlg = true, $aColor1 = 'lightgray', $aColor2 = 'lightblue')
71
    {
72 19
        $this->fill      = $aFlg;
73 19
        $this->fillcolor = [$aColor1, $aColor2];
74 19
    }
75
76
    // Display the grid
77 19
    public function Stroke()
78
    {
79 19
        if ($this->showMinor && !$this->scale->textscale) {
80 3
            $this->DoStroke($this->scale->ticks->ticks_pos, $this->minortype, $this->minorcolor, $this->minorweight);
81 3
            $this->DoStroke($this->scale->ticks->maj_ticks_pos, $this->majortype, $this->majorcolor, $this->majorweight);
82
        } else {
83 18
            $this->DoStroke($this->scale->ticks->maj_ticks_pos, $this->majortype, $this->majorcolor, $this->majorweight);
84
        }
85 19
    }
86
87
    /**
88
     * Private methods
89
     * // Draw the grid.
90
     *
91
     * @param mixed $aTicksPos
92
     * @param mixed $aType
93
     * @param mixed $aColor
94
     * @param mixed $aWeight
95
     */
96 19
    public function DoStroke($aTicksPos, $aType, $aColor, $aWeight)
97
    {
98 19
        $nbrgrids = safe_count($aTicksPos);
99 19
        if (!$this->show || $nbrgrids === 0) {
100 13
            return;
101
        }
102
103 19
        if ($this->scale->type == 'y') {
104 19
            $xl = $this->img->left_margin;
105 19
            $xr = $this->img->width - $this->img->right_margin;
106
107 19
            if ($this->fill) {
108
                // Draw filled areas
109 18
                $y2 = $aTicksPos[0];
110 18
                $i  = 1;
111 18
                while ($i < $nbrgrids) {
112 18
                    $y1 = $y2;
113 18
                    $y2 = $aTicksPos[$i++];
114 18
                    $this->img->SetColor($this->fillcolor[$i & 1]);
115 18
                    $this->img->FilledRectangle($xl, $y1, $xr, $y2);
116
                }
117
            }
118
119 19
            $this->img->SetColor($aColor);
120 19
            $this->img->SetLineWeight($aWeight);
121
122
            // Draw grid lines
123 19
            switch ($aType) {
124 19
                case 'solid':
125 17
                    $style = LINESTYLE_SOLID;
126
127 17
                    break;
128 5
                case 'dotted':
129 3
                    $style = LINESTYLE_DOTTED;
130
131 3
                    break;
132 2
                case 'dashed':
133 2
                    $style = LINESTYLE_DASHED;
134
135 2
                    break;
136
                case 'longdashed':
137
                    $style = LINESTYLE_LONGDASH;
138
139
                    break;
140
                default:
141
                    $style = LINESTYLE_SOLID;
142
143
                    break;
144
            }
145
146 19
            for ($i = 0; $i < $nbrgrids; ++$i) {
147 19
                $y = $aTicksPos[$i];
148 19
                $this->img->StyleLine($xl, $y, $xr, $y, $style, true);
149
            }
150 10
        } elseif ($this->scale->type == 'x') {
151 10
            $yu    = $this->img->top_margin;
152 10
            $yl    = $this->img->height - $this->img->bottom_margin;
153 10
            $limit = $this->img->width - $this->img->right_margin;
154
155 10
            if ($this->fill) {
156
                // Draw filled areas
157
                $x2 = $aTicksPos[0];
158
                $i  = 1;
159
                while ($i < $nbrgrids) {
160
                    $x1 = $x2;
161
                    $x2 = min($aTicksPos[$i++], $limit);
162
                    $this->img->SetColor($this->fillcolor[$i & 1]);
163
                    $this->img->FilledRectangle($x1, $yu, $x2, $yl);
164
                }
165
            }
166
167 10
            $this->img->SetColor($aColor);
168 10
            $this->img->SetLineWeight($aWeight);
169
170
            // We must also test for limit since we might have
171
            // an offset and the number of ticks is calculated with
172
            // assumption offset==0 so we might end up drawing one
173
            // to many gridlines
174 10
            $i = 0;
175 10
            $x = $aTicksPos[$i];
0 ignored issues
show
Unused Code introduced by
The assignment to $x is dead and can be removed.
Loading history...
176 10
            while ($i < safe_count($aTicksPos) && ($x = $aTicksPos[$i]) <= $limit) {
177 10
                if ($aType == 'solid') {
178 10
                    $this->img->Line($x, $yl, $x, $yu);
179 2
                } elseif ($aType == 'dotted') {
180
                    $this->img->DashedLineForGrid($x, $yl, $x, $yu, 1, 6);
181 2
                } elseif ($aType == 'dashed') {
182 2
                    $this->img->DashedLineForGrid($x, $yl, $x, $yu, 2, 4);
183
                } elseif ($aType == 'longdashed') {
184
                    $this->img->DashedLineForGrid($x, $yl, $x, $yu, 8, 6);
185
                }
186
187 10
                ++$i;
188
            }
189
        } else {
190
            Util\JpGraphError::RaiseL(25054, $this->scale->type); //('Internal error: Unknown grid axis ['.$this->scale->type.']');
191
        }
192
193 19
        return true;
194
    }
195
} // @class
196