Issues (459)

src/graph/PieGraph.php (2 issues)

Severity
1
<?php
2
3
/**
4
 * JPGraph v4.0.3
5
 */
6
7
namespace Amenadiel\JpGraph\Graph;
8
9 1
require_once __DIR__ . '/../config.inc.php';
10
11
use Amenadiel\JpGraph\Image;
12
use Amenadiel\JpGraph\Plot;
13
use Amenadiel\JpGraph\Text;
14
15
/**
16
 * @class PieGraph
17
 * // Description:
18
 */
19
class PieGraph extends Graph
20
{
21
    private $posx;
22
    private $posy;
23
    private $radius;
0 ignored issues
show
The private property $radius is not used, and could be removed.
Loading history...
24
    private $legends = [];
0 ignored issues
show
The private property $legends is not used, and could be removed.
Loading history...
25
    public $plots    = [];
26
    public $pieaa    = false;
27
28
    /**
29
     * CONSTRUCTOR.
30
     *
31
     * @param mixed $width
32
     * @param mixed $height
33
     * @param mixed $cachedName
34
     * @param mixed $timeout
35
     * @param mixed $inline
36
     */
37 3
    public function __construct($width = 300, $height = 200, $cachedName = '', $timeout = 0, $inline = 1)
38
    {
39 3
        parent::__construct($width, $height, $cachedName, $timeout, $inline);
40 3
        $this->posx = $width / 2;
41 3
        $this->posy = $height / 2;
42 3
        $this->SetColor([255, 255, 255]);
43
44 3
        if ($this->graph_theme) {
45 3
            $this->graph_theme->ApplyGraph($this);
46
        }
47 3
    }
48
49
    /**
50
     * PUBLIC METHODS.
51
     *
52
     * @param mixed $aObj
53
     */
54 3
    public function Add($aObj)
55
    {
56 3
        if (is_array($aObj) && safe_count($aObj) > 0) {
57
            $cl = $aObj[0];
58
        } else {
59 3
            $cl = $aObj;
60
        }
61
62 3
        if ($cl instanceof Text\Text) {
63
            $this->AddText($aObj);
64 3
        } elseif (($cl instanceof Plot\IconPlot)) {
65
            $this->AddIcon($aObj);
66
        } else {
67 3
            if (is_array($aObj)) {
68
                $n = safe_count($aObj);
69
                for ($i = 0; $i < $n; ++$i) {
70
                    //if ($aObj[$i]->theme) {
71
                    //    $this->ClearTheme();
72
                    //}
73
                    $this->plots[] = $aObj[$i];
74
                }
75
            } else {
76
                //if ($aObj->theme) {
77
                //    $this->ClearTheme();
78
                //}
79 3
                $this->plots[] = $aObj;
80
            }
81
        }
82
83 3
        if ($this->graph_theme) {
84 3
            $this->graph_theme->SetupPlot($aObj);
85 3
            if ($aObj->is_using_plot_theme) {
86 3
                $aObj->UsePlotThemeColors();
87
            }
88
        }
89 3
    }
90
91 3
    public function SetAntiAliasing($aFlg = true)
92
    {
93 3
        $this->pieaa = $aFlg;
94 3
    }
95
96 3
    public function SetColor($c)
97
    {
98 3
        $this->SetMarginColor($c);
99 3
    }
100
101
    public function DisplayCSIMAreas()
102
    {
103
        $csim = '';
104
        foreach ($this->plots as $p) {
105
            $csim .= $p->GetCSIMareas();
106
        }
107
108
        $csim .= $this->legend->GetCSIMareas();
109
        if (preg_match_all('/area shape="(\\w+)" coords="([0-9\\, ]+)"/', $csim, $coords)) {
110
            $this->img->SetColor($this->csimcolor);
111
            $n = safe_count($coords[0]);
112
            for ($i = 0; $i < $n; ++$i) {
113
                if ($coords[1][$i] == 'poly') {
114
                    preg_match_all('/\s*([0-9]+)\s*,\s*([0-9]+)\s*,*/', $coords[2][$i], $pts);
115
                    $this->img->SetStartPoint($pts[1][count($pts[0]) - 1], $pts[2][count($pts[0]) - 1]);
116
                    $m = safe_count($pts[0]);
117
                    for ($j = 0; $j < $m; ++$j) {
118
                        $this->img->LineTo($pts[1][$j], $pts[2][$j]);
119
                    }
120
                } elseif ($coords[1][$i] == 'rect') {
121
                    $pts = preg_split('/,/', $coords[2][$i]);
122
                    $this->img->SetStartPoint($pts[0], $pts[1]);
123
                    $this->img->LineTo($pts[2], $pts[1]);
124
                    $this->img->LineTo($pts[2], $pts[3]);
125
                    $this->img->LineTo($pts[0], $pts[3]);
126
                    $this->img->LineTo($pts[0], $pts[1]);
127
                }
128
            }
129
        }
130
    }
131
132
    // Method description
133 3
    public function Stroke($aStrokeFileName = '')
134
    {
135
        // If the filename is the predefined value = '_csim_special_'
136
        // we assume that the call to stroke only needs to do enough
137
        // to correctly generate the CSIM maps.
138
        // We use this variable to skip things we don't strictly need
139
        // to do to generate the image map to improve performance
140
        // a best we can. Therefor you will see a lot of tests !$_csim in the
141
        // code below.
142 3
        $_csim = ($aStrokeFileName === _CSIM_SPECIALFILE);
143
144
        // If we are called the second time (perhaps the user has called GetHTMLImageMap()
145
        // himself then the legends have alsready been populated once in order to get the
146
        // CSIM coordinats. Since we do not want the legends to be populated a second time
147
        // we clear the legends
148 3
        $this->legend->Clear();
149
150
        // We need to know if we have stroked the plot in the
151
        // GetCSIMareas. Otherwise the CSIM hasn't been generated
152
        // and in the case of GetCSIM called before stroke to generate
153
        // CSIM without storing an image to disk GetCSIM must call Stroke.
154 3
        $this->iHasStroked = true;
155
156 3
        $n = safe_count($this->plots);
157
158 3
        if ($this->pieaa) {
159 3
            if (!$_csim) {
160 3
                if ($this->background_image != '') {
161
                    $this->StrokeFrameBackground();
162
                } else {
163 3
                    $this->StrokeFrame();
164 3
                    $this->StrokeBackgroundGrad();
165
                }
166
            }
167
168 3
            $w      = $this->img->width;
169 3
            $h      = $this->img->height;
170 3
            $oldimg = $this->img->img;
171
172 3
            $this->img->CreateImgCanvas(2 * $w, 2 * $h);
173
174 3
            $this->img->SetColor($this->margin_color);
175 3
            $this->img->FilledRectangle(0, 0, 2 * $w - 1, 2 * $h - 1);
176
177
            // Make all icons *2 i size since we will be scaling down the
178
            // imahe to do the anti aliasing
179 3
            $ni = safe_count($this->iIcons);
180 3
            for ($i = 0; $i < $ni; ++$i) {
181
                $this->iIcons[$i]->iScale *= 2;
182
                if ($this->iIcons[$i]->iX > 1) {
183
                    $this->iIcons[$i]->iX *= 2;
184
                }
185
186
                if ($this->iIcons[$i]->iY > 1) {
187
                    $this->iIcons[$i]->iY *= 2;
188
                }
189
            }
190
191 3
            $this->StrokeIcons();
192
193 3
            for ($i = 0; $i < $n; ++$i) {
194 3
                if ($this->plots[$i]->posx > 1) {
195
                    $this->plots[$i]->posx *= 2;
196
                }
197
198 3
                if ($this->plots[$i]->posy > 1) {
199
                    $this->plots[$i]->posy *= 2;
200
                }
201
202 3
                $this->plots[$i]->Stroke($this->img, 1);
203
204 3
                if ($this->plots[$i]->posx > 1) {
205
                    $this->plots[$i]->posx /= 2;
206
                }
207
208 3
                if ($this->plots[$i]->posy > 1) {
209
                    $this->plots[$i]->posy /= 2;
210
                }
211
            }
212
213 3
            $indent = $this->doframe ? ($this->frame_weight + ($this->doshadow ? $this->shadow_width : 0)) : 0;
214 3
            $indent += $this->framebevel ? $this->framebeveldepth + 1 : 0;
215 3
            $this->img->CopyCanvasH(
216 3
                $oldimg,
217 3
                $this->img->img,
218 3
                $indent,
219 3
                $indent,
220 3
                $indent,
221 3
                $indent,
222 3
                $w - 2 * $indent,
223 3
                $h - 2 * $indent,
224 3
                2 * ($w - $indent),
225 3
                2 * ($h - $indent)
226
            );
227
228 3
            $this->img->img    = $oldimg;
229 3
            $this->img->width  = $w;
230 3
            $this->img->height = $h;
231
232 3
            for ($i = 0; $i < $n; ++$i) {
233 3
                $this->plots[$i]->Stroke($this->img, 2); // Stroke labels
234 3
                $this->plots[$i]->Legend($this);
235
            }
236
        } else {
237
            if (!$_csim) {
238
                if ($this->background_image != '') {
239
                    $this->StrokeFrameBackground();
240
                } else {
241
                    $this->StrokeFrame();
242
                    $this->StrokeBackgroundGrad();
243
                }
244
            }
245
246
            $this->StrokeIcons();
247
248
            for ($i = 0; $i < $n; ++$i) {
249
                $this->plots[$i]->Stroke($this->img);
250
                $this->plots[$i]->Legend($this);
251
            }
252
        }
253
254 3
        $this->legend->Stroke($this->img);
255 3
        $this->footer->Stroke($this->img);
256 3
        $this->StrokeTitles();
257
258 3
        if (!$_csim) {
259
            // Stroke texts
260 3
            if ($this->texts != null) {
261
                $n = safe_count($this->texts);
262
                for ($i = 0; $i < $n; ++$i) {
263
                    $this->texts[$i]->Stroke($this->img);
264
                }
265
            }
266
267 3
            if (_JPG_DEBUG) {
268
                $this->DisplayCSIMAreas();
269
            }
270
271
            // Should we do any final image transformation
272 3
            if ($this->iImgTrans) {
273
                $tform          = new Image\ImgTrans($this->img->img);
274
                $this->img->img = $tform->Skew3D(
275
                    $this->iImgTransHorizon,
276
                    $this->iImgTransSkewDist,
277
                    $this->iImgTransDirection,
278
                    $this->iImgTransHighQ,
279
                    $this->iImgTransMinSize,
280
                    $this->iImgTransFillColor,
281
                    $this->iImgTransBorder
282
                );
283
            }
284
285
            // If the filename is given as the special "__handle"
286
            // then the image handler is returned and the image is NOT
287
            // streamed back
288 3
            if ($aStrokeFileName == _IMG_HANDLER) {
289
                return $this->img->img;
290
            }
291
            // Finally stream the generated picture
292 3
            $this->cache->PutAndStream(
293 3
                $this->img,
294 3
                $this->cache_name,
295 3
                $this->inline,
296 3
                $aStrokeFileName
297
            );
298
        }
299 3
    }
300
} // @class
301