Grid::SetColor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
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
    public function __construct($aAxis)
31
    {
32
        $this->scale = $aAxis->scale;
33
        $this->img   = $aAxis->img;
34
    }
35
36
    public function SetColor($aMajColor, $aMinColor = false)
37
    {
38
        $this->majorcolor = $aMajColor;
39
        if ($aMinColor === false) {
40
            $aMinColor = $aMajColor;
41
        }
42
        $this->minorcolor = $aMinColor;
43
    }
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
    public function SetLineStyle($aMajorType, $aMinorType = 'solid')
53
    {
54
        $this->majortype = $aMajorType;
55
        $this->minortype = $aMinorType;
56
    }
57
58
    public function SetStyle($aMajorType, $aMinorType = 'solid')
59
    {
60
        $this->SetLineStyle($aMajorType, $aMinorType);
61
    }
62
63
    // Decide if both major and minor grid should be displayed
64
    public function Show($aShowMajor = true, $aShowMinor = false)
65
    {
66
        $this->show      = $aShowMajor;
67
        $this->showMinor = $aShowMinor;
68
    }
69
70
    public function SetFill($aFlg = true, $aColor1 = 'lightgray', $aColor2 = 'lightblue')
71
    {
72
        $this->fill      = $aFlg;
73
        $this->fillcolor = [$aColor1, $aColor2];
74
    }
75
76
    // Display the grid
77
    public function Stroke()
78
    {
79
        if ($this->showMinor && !$this->scale->textscale) {
80
            $this->DoStroke($this->scale->ticks->ticks_pos, $this->minortype, $this->minorcolor, $this->minorweight);
81
            $this->DoStroke($this->scale->ticks->maj_ticks_pos, $this->majortype, $this->majorcolor, $this->majorweight);
82
        } else {
83
            $this->DoStroke($this->scale->ticks->maj_ticks_pos, $this->majortype, $this->majorcolor, $this->majorweight);
84
        }
85
    }
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
    public function DoStroke($aTicksPos, $aType, $aColor, $aWeight)
97
    {
98
        $nbrgrids = safe_count($aTicksPos);
99
        if (!$this->show || $nbrgrids === 0) {
100
            return;
101
        }
102
103
        if ($this->scale->type == 'y') {
104
            $xl = $this->img->left_margin;
105
            $xr = $this->img->width - $this->img->right_margin;
106
107
            if ($this->fill) {
108
                // Draw filled areas
109
                $y2 = $aTicksPos[0];
110
                $i  = 1;
111
                while ($i < $nbrgrids) {
112
                    $y1 = $y2;
113
                    $y2 = $aTicksPos[$i++];
114
                    $this->img->SetColor($this->fillcolor[$i & 1]);
115
                    $this->img->FilledRectangle($xl, $y1, $xr, $y2);
116
                }
117
            }
118
119
            $this->img->SetColor($aColor);
120
            $this->img->SetLineWeight($aWeight);
121
122
            // Draw grid lines
123
            switch ($aType) {
124
                case 'solid':
125
                    $style = LINESTYLE_SOLID;
126
127
                    break;
128
                case 'dotted':
129
                    $style = LINESTYLE_DOTTED;
130
131
                    break;
132
                case 'dashed':
133
                    $style = LINESTYLE_DASHED;
134
135
                    break;
136
                case 'longdashed':
137
                    $style = LINESTYLE_LONGDASH;
138
139
                    break;
140
                default:
141
                    $style = LINESTYLE_SOLID;
142
143
                    break;
144
            }
145
146
            for ($i = 0; $i < $nbrgrids; ++$i) {
147
                $y = $aTicksPos[$i];
148
                $this->img->StyleLine($xl, $y, $xr, $y, $style, true);
149
            }
150
        } elseif ($this->scale->type == 'x') {
151
            $yu    = $this->img->top_margin;
152
            $yl    = $this->img->height - $this->img->bottom_margin;
153
            $limit = $this->img->width - $this->img->right_margin;
154
155
            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
            $this->img->SetColor($aColor);
168
            $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
            $i = 0;
175
            $x = $aTicksPos[$i];
0 ignored issues
show
Unused Code introduced by
The assignment to $x is dead and can be removed.
Loading history...
176
            while ($i < safe_count($aTicksPos) && ($x = $aTicksPos[$i]) <= $limit) {
177
                if ($aType == 'solid') {
178
                    $this->img->Line($x, $yl, $x, $yu);
179
                } elseif ($aType == 'dotted') {
180
                    $this->img->DashedLineForGrid($x, $yl, $x, $yu, 1, 6);
181
                } elseif ($aType == 'dashed') {
182
                    $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
                ++$i;
188
            }
189
        } else {
190
            Util\JpGraphError::RaiseL(25054, $this->scale->type); //('Internal error: Unknown grid axis ['.$this->scale->type.']');
191
        }
192
193
        return true;
194
    }
195
} // @class
196