-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimplePHPChart.php
410 lines (335 loc) · 7.17 KB
/
SimplePHPChart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
namespace codyben;
/**
* Class SimplePHPChart
* @package codyben
* @version 1.0
*/
class SimplePHPChart {
/**
* @var float
*/
private $shiftX;
/**
* @var float
*/
private $shiftY;
/**
* @var array
*/
private $xValues;
/**
* @var array
*/
private $yValues;
/**
* @var int
*/
private $sizeX;
/**
* @var int
*/
private $sizeY;
private $colorAxisY;
private $colorAxisX;
private $colorTickY;
private $colorTickX;
private $colorBackground;
private $colorPoints;
private $im;
/**
* @var bool
*/
private $isRangeX;
/**
* @var int
*/
private $rangeXLow = 0;
/**
* @var int
*/
private $rangeYLow = 0;
/**
* @var int
*/
private $rangeXHigh = 0;
/**
* @var int
*/
private $rangeYHigh = 0;
/**
* @var bool
*/
private $isRangeY;
/**
* @var bool
*/
public $gridLines;
/**
* @var float
*/
private $domain;
/**
* @var float
*/
private $range;
/**
* @var float
*/
private $scaleX;
/**
* @var float
*/
private $scaleY;
/**
* @var int
*/
private $originX;
/**
* @var int
*/
private $originY;
/**
* @var bool
*/
private $ticks;
/**
* Initialize the chart with its dimensions in pixels.
* @param int $x
* @param int $y
* @param bool|bool $ticks
*/
function __construct(int $x, int $y, bool $ticks = true)
{
$this->ticks = $ticks;
$this->sizeX = $x;
$this->sizeY = $y;
$this->imageName = "chart.png";
$this->im = imagecreatetruecolor($x, $y);
//Assign default colors
$this->colorTickY = imagecolorallocate($this->im, 255, 255, 255); //white
$this->colorTickX = imagecolorallocate($this->im, 255, 255, 255); //white
$this->colorAxisY = imagecolorallocate($this->im, 255,215,0); //gold
$this->colorAxisX = imagecolorallocate($this->im, 255, 215, 0); //gold
$this->colorBackground = imagecolorallocate($this->im, 0, 0, 0); //black
$this->colorPoints = imagecolorallocate($this->im, 0,128,0); //green
imagefill($this->im, 0, 0, $this->colorBackground);
}
/**
* Destroys the image when object falls out of scope.
* @return type
*/
function __destruct()
{
imagedestroy($this->im);
}
/**
* Draws the x & y axis. Draws from 0px to end.
* @return void
*/
private function drawAxis() : void
{
imageline($this->im, 0 , $this->shiftY , $this->sizeX , $this->shiftY, $this->colorAxisX);
imageline($this->im, $this->shiftX , 0 , $this->shiftX, $this->sizeY , $this->colorAxisX);
}
/**
* Scales all x values in terms of chart size.
* @return void
*/
private function scaleX() : void
{
$this->domain = abs($this->rangeXLow) + abs($this->rangeXHigh);
$this->scaleX = ((float)$this->sizeX / $this->domain);
$this->originX = $this->sizeX >> 1;
}
/**
* Scales all y values in terms of chart size.
* @return void
*/
private function scaleY() : void
{
$this->range = abs($this->rangeYLow) + abs($this->rangeYHigh);
$this->scaleY = ((float)$this->sizeY / $this->range);
$this->originY = $this->sizeY >> 1;
}
/**
* Set the extent of the domain.
* @param int $low
* @param int $high
* @return void
*/
public function setDomain(int $low, int $high) : void
{
$this->isRangeX = true;
$this->rangeXHigh = $high;
$this->rangeXLow = $low;
}
/**
* Set the extent of the range.
* @param int $low
* @param int $high
* @return void
*/
public function setRange(int $low, int $high) : void
{
$this->isRangeY = true;
$this->rangeYHigh = $high;
$this->rangeYLow = $low;
}
/**
* Set the color of the x axis.
* @param int $r
* @param int $g
* @param int $b
* @return void
*/
public function setColorAxisX(int $r, int $g, int $b) : void
{
$this->colorAxisX =imagecolorallocate($this->m, $r, $g, $b);
}
/**
* Set the color of the y axis.
* @param int $r
* @param int $g
* @param int $b
* @return void
*/
public function setColorAxisY(int $r, int $g, int $b) : void
{
$this->colorAxisY = imagecolorallocate($this->im, $r, $g, $b);
}
/**
* Set the color of the y axis tick marks
* @param int $r
* @param int $g
* @param int $b
* @return void
*/
public function setColorTickY(int $r, int $g, int $b) : void
{
$this->colorTickY = imagecolorallocate($this->im, $r, $g, $b);
}
/**
* Set the color of the x axis tick marks
* @param int $r
* @param int $g
* @param int $b
* @return void
*/
public function setColorTickX(int $r, int $g, int $b) : void
{
$this->colorTickX = imagecolorallocate($this->im, $r, $g, $b);
}
/**
* Set the chart background color.
* @param int $r
* @param int $g
* @param int $b
* @return void
*/
public function setColorBackground(int $r, int $g, int $b) : void
{
$this->colorBackground = imagecolorallocate($this->im, $r, $g, $b);
imagefill($this->im, 0, 0, $this->colorBackground);
}
/**
* Set the color of plotted data points.
* @param int $r
* @param int $g
* @param int $b
* @return void
*/
public function setColorPoints(int $r, int $g, int $b) : void
{
$colorPoints = imagecolorallocate($im, $r, $g, $b);
}
/**
* Create an (x,y) pair to be plotted.
* @param int $x
* @param int $y
* @return void
*/
public function setPoint(int $x, int $y ) : void
{
if($x > $this->rangeXHigh)
$this->rangeXHigh = $x;
elseif ($x < $this->rangeXLow)
$this->rangeXLow = $x;
if($y > $this->rangeYHigh)
$this->rangeYHigh = $y;
elseif ($y < $this->rangeYLow)
$this->rangeYLow = $y;
$this->xValues[] = $x;
$this->yValues[] = $y;
}
/**
* Get number of (x,y) pairs input. Returns -1 on failure.
* @return int
*/
public function getSize() : int
{
$s = count($this->xValues);
if($s !== count($this->yValues)) //these *should* be the same, but it doesn't hurt to check
return -1;
else
return $s;
}
/**
* Calculate the scaling needed to chart proportionally.
* @return type
*/
private function calcShifts() : void
{
$this->shiftY = $this->scaleY*abs($this->rangeYHigh);
$this->shiftX = $this->scaleX*abs($this->rangeXLow);
}
/**
* Draw tick marks using provided / default colors.
* @return void
*/
private function makeTickMarks() : void
{
$tickX = 0;
$tickY = 0;
if($this->ticks) {
$tickSizeX = $this->scaleY/6;
$tickSizeY = $this->scaleX/6;
while($tickX <= $this->sizeX) {
imageline($this->im, $tickX, 0-$tickSizeX+$this->shiftY, $tickX, 0+$tickSizeX+$this->shiftY, $this->colorTickX);
$tickX += $this->scaleX;
}
while($tickY <= $this->sizeY) {
imageline($this->im, 0-$tickSizeY+$this->shiftX, $tickY, 0+$tickSizeY+$this->shiftX, $tickY, $this->colorTickY);
$tickY += $this->scaleY;
}
}
}
/**
* Plot all (x,y) pairs.
* @return void
*/
private function plotData() : void
{
$n = $this->getSize();
if($n === -1) {
exit();
}
for ($i=0; $i < $n; $i++) {
imagefilledellipse($this->im, $this->xValues[$i]*$this->scaleX+$this->shiftX, $this->yValues[$i]*$this->scaleY*(-1)+$this->shiftY, 10, 10, $this->colorPoints);
}
}
/**
* Create the chart image resoucrce.
* @return resource
*/
public function makeChart()
{
$this->scaleX();
$this->scaleY();
$this->calcShifts();
$this->drawAxis();
$this->makeTickMarks();
$this->plotData();
return $this->im;
}
}