-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmg5.php
4336 lines (3747 loc) · 123 KB
/
pmg5.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Library to create simple graph charts
*
* PHP version 5
*
* LICENSE:
*
* Free to use distribute or modify in any way.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS''.
*
* @category Images
* @package graph
* @author Martijn Beulens <mbeulens(at)bawic.nl>
* @thanksto Tekin Ozbek (For pointing out configuration directive misspellings)
* @thanksto Jack Finch (For right-aligment and round value range functionality)
* @license Free to use, distribute or modify in any way.
* @version 5.0.4
*
* @updates
* 5.0.4: A display bug has been resolved which occured when all data
* values where below zero.
*
*/
//##########################################################################
//# phpMyGraph
//##########################################################################
class phpMyGraph {
//-------------------------------------------------------------------------+
/**
* parseVerticalColumnGraph
*
* For backwards compatibility or easy usage
*
* @param array $data
* @param optional array $cfg
* @return void
*/
public static function parseVerticalColumnGraph($data, $cfg = array()) {
$graph = self::factory('verticalColumnGraph', $data, $cfg);
$graph->parse($data, $cfg);
}
//-------------------------------------------------------------------------+
/**
* parseVerticalLineGraph
*
* For backwards compatibility or easy usage
*
* @param array $data
* @param optional array $cfg
* @return void
*/
public static function parseVerticalLineGraph($data, $cfg = array()) {
$graph = self::factory('verticalLineGraph', $data, $cfg);
$graph->parse($data, $cfg);
}
//-------------------------------------------------------------------------+
/**
* parseVerticalPolygonGraph
*
* For backwards compatibility or easy usage
*
* @param array $data
* @param optional array $cfg
* @return void
*/
public static function parseVerticalPolygonGraph($data, $cfg = array()) {
$graph = self::factory('verticalPolygonGraph', $data, $cfg);
$graph->parse($data, $cfg);
}
//-------------------------------------------------------------------------+
/**
* parseHorizontalColumnGraph
*
* For backwards compatibility or easy usage
*
* @param array $data
* @param optional array $cfg
* @return void
*/
public static function parseHorizontalColumnGraph($data, $cfg = array()) {
$graph = self::factory('horizontalColumnGraph', $data, $cfg);
$graph->parse($data, $cfg);
}
//-------------------------------------------------------------------------+
/**
* parseHorizontalLineGraph
*
* For backwards compatibility or easy usage
*
* @param array $data
* @param optional array $cfg
* @return void
*/
public static function parseHorizontalLineGraph($data, $cfg = array()) {
$graph = self::factory('horizontalLineGraph', $data, $cfg);
$graph->parse($data, $cfg);
}
//-------------------------------------------------------------------------+
/**
* parseHorizontalPolygonGraph
*
* For backwards compatibility or easy usage
*
* @param array $data
* @param optional array $cfg
* @return void
*/
public static function parseHorizontalPolygonGraph($data, $cfg = array()) {
$graph = self::factory('horizontalPolygonGraph', $data, $cfg);
$graph->parse($data, $cfg);
}
//-------------------------------------------------------------------------+
/**
* parseHorizontalSimpleColumnGraphGraph
*
* For backwards compatibility or easy usage
*
* @param array $data
* @param optional array $cfg
* @return void
*/
public static function parseHorizontalSimpleColumnGraph($data, $cfg = array()) {
$graph = self::factory('horizontalSimpleColumnGraph', $data, $cfg);
$graph->parse($data, $cfg);
}
//-------------------------------------------------------------------------+
/**
* parseVerticalSimpleColumnGraphGraph
*
* For backwards compatibility or easy usage
*
* @param array $data
* @param optional array $cfg
* @return void
*/
public static function parseVerticalSimpleColumnGraph($data, $cfg = array()) {
$graph = self::factory('verticalSimpleColumnGraph', $data, $cfg);
$graph->parse($data, $cfg);
}
//-------------------------------------------------------------------------+
/**
* factory
*
* Creates a graph
*
* @param array $data
* @param optional array $cfg
* @return void
*/
public static function factory($type) {
if(!class_exists($type)) {
return false;
}
return new $type();
}
//-------------------------------------------------------------------------+
} //End class
//##########################################################################
//# iGraph
//##########################################################################
/**
* @name iGraph
* @type interface
* @package graph
* @version 5.0.1
* @comment: Graph interface
*/
interface iGraph {
public function parse($data, $cfg = array());
}
//##########################################################################
//# graph
//##########################################################################
/**
* @name graphBase
* @type class (abstract)
* @extends none
* @package graph
* @version 5.0.1
* @comment: Graph super class
*/
abstract class graphBase implements iGraph {
/**
* Properties
*/
/**
* @name: ip
* @access: protected
* @since: version 5.0.1
* @comment: Image pointer
*/
protected $ip;
/**
* @name: cfg
* @access: protected
* @since: version 5.0.1
* @comment: Config array
*/
protected $cfg;
//-------------------------------------------------------------------------+
/**
* Methods
*/
/**
* @name: __construct
* @param: none
* @return: void
* @access: public
* @exception: no
* @since: version 5.0.1
* @comment: Constructs the graph
*/
public function __construct() {
}
//-------------------------------------------------------------------------+
/**
* @name: parse
* @param: (array) $data, [(array) $cfg]
* @return: void
* @access: public
* @exception: no
* @since: version 5.0.1
* @comment: Parses the graph
*/
public function parse($data, $cfg=array()) {
//Try block
try {
//Parse config etc
$this->parseConfig($cfg);
//Implement your code here
throw new Exception('Method not implemented');
}
catch(Exception $ex) {
//Parse error message overriding original
$this->parseError($ex->__toString());
}
//Parse image
$this->parseImage();
}
//-------------------------------------------------------------------------+
/**
* @name: getConfigProperties
* @param: none
* @return: (array) configProperties
* @access: public
* @exception: no
* @since: version 5.0.1
* @comment: Returns array of config properties
*/
public static function getConfigProperties() {
$properties = array(
array('name' => 'type',
'description' => 'Type for image output (png, jpg, gif)',
'type' => 'arrayValue',
'default' => 'png',
'values' => array('png','jpg','gif'),
'casing' => 'lowercase'),
array('name' => 'width',
'description' => 'Width for image',
'type' => 'numeric',
'default' => 500),
array('name' => 'height',
'description' => 'Height for image',
'type' => 'numeric',
'default' => 200),
array('name' => 'background-color',
'description' => 'Background color',
'type' => 'color',
'default' => 'FFFFFF'),
array('name' => 'background-image',
'description' => 'Background image',
'type' => 'file',
'default' => ''),
array('name' => 'title',
'description' => 'Title for graph',
'type' => 'text',
'default' => ''),
array('name' => 'title-visible',
'description' => 'Set title visibility',
'type' => 'boolean',
'default' => true),
array('name' => 'title-font-size',
'description' => 'Title font size (1, 2, 3, 4, 5, 6)',
'type' => 'arrayValue',
'default' => 2,
'values' => array(1, 2, 3, 4, 5, 6)),
array('name' => 'title-color',
'description' => 'Title color (Use HEX)',
'type' => 'color',
'default' => '000000'),
array('name' => 'zero-line-visible',
'description' => 'Set zero line visibility',
'type' => 'boolean',
'default' => true),
array('name' => 'zero-line-color',
'description' => 'Zero line color',
'type' => 'color',
'default' => '000000'),
array('name' => 'zero-line-alpha',
'description' => 'Zero line alpha',
'type' => 'numeric',
'default' => 0),
array('name' => 'average-line-visible',
'description' => 'Set average line visibility',
'type' => 'boolean',
'default' => true),
array('name' => 'average-line-color',
'description' => 'Average line color (Use HEX)',
'type' => 'color',
'default' => '0000FF'),
array('name' => 'average-line-alpha',
'description' => 'Average line alpha',
'type' => 'numeric',
'default' => 0),
array('name' => 'key-color',
'description' => 'Key color (Use HEX)',
'type' => 'color',
'default' => '006699'),
array('name' => 'key-visible',
'description' => 'Set key visibility',
'type' => 'boolean',
'default' => true),
array('name' => 'key-font-size',
'description' => 'Set key font size (1, 2, 3, 4, 5, 6)',
'type' => 'arrayValue',
'default' => 2,
'values' => array(1, 2, 3, 4, 5, 6)),
array('name' => 'key-right-align',
'description' => 'Right Align Keys',
'type' => 'boolean',
'default' => false),
array('name' => 'label',
'description' => 'Label text',
'type' => 'text',
'default' => ''),
array('name' => 'label-color',
'description' => 'Label color (Use HEX)',
'type' => 'color',
'default' => '000000'),
array('name' => 'label-visible',
'description' => 'Set label visibility',
'type' => 'boolean',
'default' => true),
array('name' => 'label-font-size',
'description' => 'Label font size (1, 2, 3, 4, 5, 6)',
'type' => 'arrayValue',
'default' => 2,
'values' => array(1, 2, 3, 4, 5, 6)),
array('name' => 'label-right-align',
'description' => 'Right Align Labels',
'type' => 'boolean',
'default' => false),
array('name' => 'value-visible',
'description' => 'Set value visibility',
'type' => 'boolean',
'default' => true),
array('name' => 'value-font-size',
'description' => 'Value font size (1, 2, 3, 4, 5, 6)',
'type' => 'arrayValue',
'default' => 2,
'values' => array(1, 2, 3, 4, 5, 6)),
array('name' => 'value-color',
'description' => 'Value color (Use HEX)',
'type' => 'color',
'default' => '000000'),
array('name' => 'value-label-visible',
'description' => 'Set value label visibility',
'type' => 'boolean',
'default' => true),
array('name' => 'value-label-font-size',
'description' => 'Value label font size',
'type' => 'arrayValue',
'default' => 2,
'values' => array(1, 2, 3, 4, 5, 6)),
array('name' => 'value-label-color',
'description' => 'Value label color (Use HEX)',
'type' => 'color',
'default' => '006699'),
array('name' => 'box-border-color',
'description' => 'Box border color (Use HEX)',
'type' => 'color',
'default' => '006699'),
array('name' => 'box-border-alpha',
'description' => 'Box border alpha',
'type' => 'numeric',
'default' => 0),
array('name' => 'box-border-visible',
'description' => 'Set box border visibility',
'type' => 'boolean',
'default' => true),
array('name' => 'box-background-color',
'description' => 'Box background color (Use HEX)',
'type' => 'color',
'default' => 'F1F1F1'),
array('name' => 'box-background-visible',
'description' => 'Box background visibility',
'type' => 'boolean',
'default' => true),
array('name' => 'box-background-alpha',
'description' => 'Box background alpha',
'type' => 'numeric',
'default' => 0),
array('name' => 'column-divider-color',
'description' => 'Column divider color (Use HEX)',
'type' => 'color',
'default' => '000000'),
array('name' => 'column-divider-alpha',
'description' => 'Column divider alpha',
'type' => 'numeric',
'default' => 100),
array('name' => 'column-divider-visible',
'description' => 'Set column divider visibility',
'type' => 'boolean',
'default' => true),
array('name' => 'horizontal-divider-color',
'description' => 'Horizontal divider color (Use HEX)',
'type' => 'color',
'default' => '000000'),
array('name' => 'horizontal-divider-alpha',
'description' => 'Horizontal divider alpha',
'type' => 'numeric',
'default' => 100),
array('name' => 'horizontal-divider-visible',
'description' => 'Set horizontal divider visiblity',
'type' => 'boolean',
'default' => true),
array('name' => 'column-color-random',
'description' => 'Create random column color',
'type' => 'boolean',
'default' => false),
array('name' => 'column-color',
'description' => 'Column color (Use HEX)',
'type' => 'color',
'default' => '0099CC'),
array('name' => 'column-alpha',
'description' => 'Column alpha',
'type' => 'numeric',
'default' => 0),
array('name' => 'column-shadow-alpha',
'description' => 'Column shadow alpha',
'type' => 'numeric',
'default' => 0),
array('name' => 'column-shadow-color',
'description' => 'Column shadow color (Use HEX)',
'type' => 'color',
'default' => '006699'),
array('name' => 'column-compare-color',
'description' => 'Compare column color (Use HEX)',
'type' => 'color',
'default' => 'FF0000'),
array('name' => 'column-compare-shadow-color',
'description' => 'Compare column shadow color (Use HEX)',
'type' => 'color',
'default' => 'FF0000'),
array('name' => 'file-name',
'description' => 'Filename to parse to',
'type' => 'text',
'default' => NULL),
array('name' => 'jpg-quality',
'description' => 'JPG output quality',
'type' => 'numeric',
'default' => 60),
array('name' => 'round-value-range',
'description' => 'Round Range Values',
'type' => 'boolean',
'default' => false)
);
return $properties;
}
//-------------------------------------------------------------------------+
/**
* @name: getExampleUsage
* @param: none
* @return: (array) configProperties
* @access: public
* @exception: no
* @since: version 5.0.1
* @comment: Returns an usage example
*/
public function getExampleUsage() {
//Var
$phpCode = '<?php';
//Set config
$phpCode .= "\t\n";
$phpCode .= "\t//Include phpMyGraph5.0.php\n";
$phpCode .= "\tinclude_once('phpMyGraph5.0.php');\n";
$phpCode .= "\t\n";
$phpCode .= "\t//Set config directives\n";
$phpCode .= "\t\$cfg['title'] = 'Example graph';\n";
$phpCode .= "\t\$cfg['width'] = 500;\n";
$phpCode .= "\t\$cfg['height'] = 250;\n";
$phpCode .= "\t\n";
$phpCode .= "\t//Set data\n";
$phpCode .= "\t\$data = array(\n";
$phpCode .= "\t 'Jan' => 12,\n";
$phpCode .= "\t 'Feb' => 25,\n";
$phpCode .= "\t 'Mar' => 0,\n";
$phpCode .= "\t 'Apr' => 7,\n";
$phpCode .= "\t 'May' => 80,\n";
$phpCode .= "\t 'Jun' => 67,\n";
$phpCode .= "\t 'Jul' => 45,\n";
$phpCode .= "\t 'Aug' => 66,\n";
$phpCode .= "\t 'Sep' => 23,\n";
$phpCode .= "\t 'Oct' => 23,\n";
$phpCode .= "\t 'Nov' => 78,\n";
$phpCode .= "\t 'Dec' => 23\n";
$phpCode .= "\t);\n";
$phpCode .= "\t\n";
/*
$phpCode .= "//NEW WAY\n";
$phpCode .= " //Create instance\n";
$phpCode .= " \$graph = new ".get_class($this)."();\n";
$phpCode .= "\n";
$phpCode .= " //Parse\n";
$phpCode .= " \$graph->parse(\$data, \$cfg);\n";
$phpCode .= "\n";
$phpCode .= "//FACTORY WAY\n";
$phpCode .= " //Create instance via factory\n";
$phpCode .= " \$graph = phpMyGraph::factory('horizontalLineGraph');\n";
$phpCode .= "\n";
$phpCode .= " //Parse\n";
$phpCode .= " \$graph->parse(\$data, \$cfg);\n";
$phpCode .= "\n";
$phpCode .= "//BACKWARDS COMPATIBILITY WAY\n";
*/
$phpCode .= " //Create phpMyGraph instance\n";
$phpCode .= " \$graph = new phpMyGraph();\n";
$phpCode .= "\n";
$phpCode .= " //Parse\n";
$phpCode .= " \$graph->parse".ucfirst(get_class($this))."(\$data, \$cfg);\n";
$phpCode .= '?>';
return highlight_string($phpCode, true);
}
//-------------------------------------------------------------------------+
/**
* @name: parseConfig
* @param: (array) $cfg
* @return: (array) $cfg
* @access: protected
* @exception: no
* @since: version 5.0.1
* @comment: Parses config properties
*/
protected function parseConfig($cfg) {
//Var
$properties = $this->getConfigProperties();
//Test
if(!is_array($cfg)) {
$cfg = array();
}
//Loop properties
foreach($properties as $propertyData) {
//Test for key
if(!array_key_exists($propertyData['name'], $cfg)) {
$cfg[$propertyData['name']] = $propertyData['default'];
}
//Test casing
if(array_key_exists('casing', $propertyData)) {
switch(strtolower($propertyData['casing'])) {
case 'lower':
case 'lowercase':
$cfg[$propertyData['name']] =
strtolower($cfg[$propertyData['name']]);
break;
case 'upper':
case 'uppercase':
$cfg[$propertyData['name']] =
strtoupper($cfg[$propertyData['name']]);
break;
default:
break;
}
}
//Test type
switch($propertyData['type']) {
case 'boolean':
if($cfg[$propertyData['name']] === true ||
$cfg[$propertyData['name']] === '1' ||
$cfg[$propertyData['name']] === 1
) {
$cfg[$propertyData['name']] = true;
}
else {
$cfg[$propertyData['name']] = false;
}
break;
case 'color':
$cfg[$propertyData['name']] =
$this->hex2Rgb($cfg[$propertyData['name']]);
break;
case 'numeric':
if(!is_numeric($cfg[$propertyData['name']])) {
$cfg[$propertyData['name']] =
$propertyData['default'];
}
break;
case 'file':
if(!empty($cfg[$propertyData['name']])) {
if(!file_exists($cfg[$propertyData['name']])) {
throw new Exception(sprintf(
'Could not find file "%s" for use as background.',
$cfg[$propertyData['name']]));
}
}
break;
case 'arrayValue':
if(!in_array($cfg[$propertyData['name']],
$propertyData['values'])) {
$cfg[$propertyData['name']] =
$propertyData['default'];
}
break;
default:
break;
}
}
//Map
$this->cfg = $cfg;
//Result
return $this->cfg;
}
//-------------------------------------------------------------------------+
/**
* @name: createImage
* @param: (int) $width, (int) $height, [(bool) $transparent=false]
* @return: (resource) $ip
* @access: protected
* @exception: no
* @since: version 5.0.1
* @comment: Creates an image
*/
protected function createImage($width, $height, $transparent=false) {
//Var
$ip = imagecreatetruecolor($width, $height);
//Set transparent
if($transparent) {
imagesavealpha($ip, true);
$trans_colour = imagecolorallocatealpha($ip, 0, 0, 0, 127);
imagefill($ip, 0, 0, $trans_colour);
}
//Result
return $ip;
}
//-------------------------------------------------------------------------+
/**
* @name: parseImage
* @param: none
* @return: void
* @access: protected
* @exception: no
* @since: version 5.0.1
* @comment: Parses the image
*/
protected function parseImage() {
//Test IP
if(!$this->ip) {
$this->parseError('$this->ip is not a valid Image resource.');
}
if(!empty($this->cfg['file-name'])) {
switch(strtolower($this->cfg['type'])) {
case 'jpg':
imagejpeg($this->ip, $this->cfg['file-name'], $this->cfg['jpg-quality']);
break;
case 'gif':
imagegif($this->ip, $this->cfg['file-name']);
break;
default:
imagepng($this->ip, $this->cfg['file-name']);
break;
}
}
else {
switch(strtolower($this->cfg['type'])) {
case 'jpg':
imagejpeg($this->ip, NULL, $this->cfg['jpg-quality']);
break;
case 'gif':
imagegif($this->ip);
break;
default:
imagepng($this->ip);
break;
}
}
}
//-------------------------------------------------------------------------+
/**
* @name: hex2Rgb
* @param: (string) $hexColor
* @return: (array) $rgb
* @access: protected
* @exception: no
* @since: version 5.0.1
* @comment: Converts HEX color to RGB
*/
protected function hex2Rgb($hexColor) {
//Test empty
if(empty($hexColor)) {
return NULL;
}
//Var
$rgb = array();
//Strip #
$hexColor = str_replace("#", '', $hexColor);
//Convert to r g b
$rgb['r'] = hexdec(substr($hexColor, 0, 2));
$rgb['g'] = hexdec(substr($hexColor, 2, 2));
$rgb['b'] = hexdec(substr($hexColor, 4, 2));
//Return
return $rgb;
}
//-------------------------------------------------------------------------+
/**
* @name: parseDataStructure
* @param: (array) $data
* @return: (array) $structure
* @access: protected
* @exception: yes
* @since: version 5.0.1
* @comment: Parses a data array in to graph structure (Rounding calculations by Jack Finch)
*/
protected function parseDataStructure($data) {
//Test
if(!is_array($data)) {
throw new Exception('Provided data is not an array.');
}
//Var
$structure = array(
'cols' => 0,
'max' => 0,
'min' => 0,
'avg' => 0,
'fakeMax' => 0,
'fakeMin' => 0,
'difference' => 0,
'plusDifference' => 0,
'minDifference' => 0,
'plusQuadrantPercentage' => 0,
'minQuadrantPercentage' => 0,
'maxKeyLength' => 0,
'maxValueLength' => 0,
'data' => $data
);
//Loop
foreach($data as $key => $value) {
//Test
if(!is_numeric($value)) {
throw new Exception(
'The value of the key "%s" is not numeric.');
}
//Test max key length
$keyLength = strlen($key);
if($structure['maxKeyLength'] < $keyLength) {
$structure['maxKeyLength'] = $keyLength;
}
//Test max value length
$valueLength = strlen($value);
//if($structure['maxValueLength'] < $valueLength) {
// $structure['maxValueLength'] = $valueLength;
//}
}
//Set
$structure['max'] = max($data);
$structure['min'] = min($data);
$structure['fakeMax'] = ceil($structure['max'] + (($structure['max'] / 100) * 10)); //Add 2 percent onto max for margin
$structure['fakeMin'] = ($structure['min'] > 0) ? 0 : ($structure['min'] + (($structure['min'] / 100) * 10));
$structure['cols'] = count($data);
$structure['avg'] = array_sum($data) / $structure['cols'];
$structure['difference'] = $structure['fakeMax'] - $structure['fakeMin'];
$structure['plusDifference'] = $structure['fakeMax'];
$structure['minDifference'] = 0 - $structure['fakeMin'];
//Round values by Jack Finch
if( $this->cfg['round-value-range'] ){
$structure['fakeMax'] = round($structure['fakeMax']);
$structure['fakeMin'] = round($structure['fakeMin']);
}
//Test lengths
if($structure['maxValueLength'] < strlen($structure['fakeMax'])) {
$structure['maxValueLength'] = strlen($structure['fakeMax']);
}
if($structure['maxValueLength'] < strlen($structure['fakeMin'])) {
$structure['maxValueLength'] = strlen($structure['fakeMin']);
}
if($structure['maxValueLength'] < strlen(ceil($structure['avg']))) {
$structure['maxValueLength'] = strlen(ceil($structure['avg']));
}
//Calc quadrant percentages
if($structure['fakeMin'] < 0) {
$structure['plusQuadrantPercentage'] = ($structure['plusDifference'] / ($structure['difference'] / 100));
$structure['minQuadrantPercentage'] = ($structure['minDifference'] / ($structure['difference'] / 100));
}
else {
$structure['plusQuadrantPercentage'] = 100;
$structure['minQuadrantPercentage'] = 0;
}
//MAP TO SECTIONS
$structure['positiveSectionPercentage'] = $structure['plusQuadrantPercentage'];
$structure['negativeSectionPercentage'] = $structure['minQuadrantPercentage'];
#echo "<pre>";
#print_r($structure);
#exit;
//Result
return $structure;
}
//-------------------------------------------------------------------------+
/**
* @name: isDataStructure
* @param: (array) $structure
* @return: (bool)
* @access: protected
* @exception: no
* @since: version 5.0.1
* @comment: Test if provided struc is datastructure
*/
protected static function isDataStructure($structure) {
//Var
$values = array('cols','max','min','avg','maxKeyLength', 'maxValueLength');
//Test
if(!is_array($structure)) {
return false;
}
//Validate
foreach($values as $key) {
if(!array_key_exists($key, $structure)) {
return false;
}
}
return true;
}
//-------------------------------------------------------------------------+
/**
* @name: compareDataStructures
* @param: (multiple (array) $dataStructure)
* @return: (array) $compareStructure
* @access: protected
* @exception: yes
* @since: version 5.0.1
* @comment: Compare datastructures
*/
protected function compareDataStructures() {
//Test arg
if(func_num_args() < 2) {
throw new Exception('compareDataStructures needs atleast 2 data structures');
}
//Var
$argumentList = func_get_args();
$structure = array(
'cols' => NULL,
'max' => 0,
'min' => 0,
'avg' => NULL,
'fakeMax' => 0,
'fakeMin' => 0,
'difference' => 0,
'plusDifference' => 0,
'minDifference' => 0,
'plusQuadrantPercentage' => 0,
'minQuadrantPercentage' => 0,
'maxKeyLength' => 0,
'maxValueLength' => 0,
'structures' => array()
);
$cols = NULL;
$avgArray = array();
//Loop
foreach($argumentList as $idx => $dataStructure) {
//Test cols
if(is_null($structure['cols'])) {
//Set the first as control
$structure['cols'] = $dataStructure['cols'];
}
else {
//Test against control
if($structure['cols'] != $dataStructure['cols']) {
throw new Exception(sprintf('Not all datastructures have the same ammount of columns.', $idx));
}
}
//Test for valid structure
if(!$this->isDataStructure($dataStructure)) {
throw new Exception(sprintf('Structure "%s" is not a valid datastructure', $idx));
}
//Compare max
if($structure['max'] < $dataStructure['max']) {
$structure['max'] = $dataStructure['max'];
}
//Compare min
if($dataStructure['min'] < $structure['min']) {
$structure['min'] = $dataStructure['min'];
}
//Compare key length
if($structure['maxKeyLength'] < $dataStructure['maxKeyLength']) {
$structure['maxKeyLength'] = $dataStructure['maxKeyLength'];
}
//Compare value length
if($structure['maxValueLength'] < $dataStructure['maxValueLength']) {
$structure['maxValueLength'] = $dataStructure['maxValueLength'];
}
//Get avg
$avgArray[] = $dataStructure['avg'];
}