-
Notifications
You must be signed in to change notification settings - Fork 8
/
graph-php.class.php
2642 lines (2103 loc) · 87.4 KB
/
graph-php.class.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
/** graph-php.class.php
*
* Class for generate graphs
*
* @author Rafael Martin Soto
* @author {@link https://www.inatica.com/ Inatica}
* @blog {@link https://rafamartin10.blogspot.com/ Blog Rafael Martin Soto}
* @since October 2021
* @version 1.0.0
* @license GNU General Public License v3.0
*
* */
require_once __DIR__ . '/img2img/img2img.class.php';
require_once __DIR__ . '/ext-op-ml-php/ext-op-ml-php.class.php';
class graph
{
/**
* Definition of Defaut Format values
*
* @var array
* @access private
*
**/
private $default_cfg = [
'width' => 6.4, // 6.4 inches
'height' => 4.8, // 4.8 inches
'dpi' => 100, // 100 dpis
'padding' => .6, // 0.6 inches
'fontdir' => __DIR__.'/fonts',
'fontfamilypath' => 'dejavu-fonts-ttf-2.37/ttf',
'font' => 'DejaVuSans.ttf',
'fontsize' => 10.5,
'xtickfontsize' => 10.5,
'ytickfontsize' => 10.5,
'axes' => [ 'prop_cycle' => []
],
'lines' =>
['width' => 3],
'values' => [],
'x_values' => [],
'y_values' => [],
'ylabel' => '',
'xlabel' => '',
'title' => '',
'cycler' => [
'color' => [
'default' => ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
],
'linestyle' => [ '-', '--', ':', '-.' ]
],
'backgroundstyle' => 'solid',
'backgroundcolor' => '#ffffff',
'bordertype' => 'square',
'paddingleft' => .79,
'paddingright' => .63,
'paddingtop' => .58,
'paddingbottom' => .515,
'paddinginsideleft' => .2,
'paddinginsideright' => .2,
'paddinginsidetop' => .15,
'paddinginsidebottom' => .15,
'ymarginleftlabel' => 10,
'xmarginlabelsticks' => 16,
'ymarginlabelsticks' => 10,
'xshowlabelticks' => true,
'yshowlabelticks' => true,
'margintitle' => 50,
'x_drawguidelines' => false,
'y_drawguidelines' => false,
'centerlabels' => false,
'bars' => ['percmarginbetwbars' => 20 ],
'legend' => false,
'legendmarginleft' => 7,
'legendmargintop' => 6,
'legendpaddingleft' => 5,
'legendpaddingtop' => 1,
'legendpaddingright' => 5,
'legendpaddingbottom' => 3,
'legendwidthlines' => 28,
'legendlabelheight' => 21,
'width_marker_x' => 9,
'height_marker_x' => 9,
'width_marker_o' => 10,
'height_marker_o' => 10,
'xticks' => ['rotation' => 0]
]; // /$default_cfg
/**
* Define colors relations
*/
private $colors_rel = ['b' => '#0000ff', 'g' => '#008000', 'r' => '#ff0000', 'c' => '#00cccc', 'm' => '#cc0000', 'y' => '#cc0000', 'k' => '#000000', 'w' => '#ffffff',
'#1f77b4' => '#1f77b4', '#ff7f0e' => '#ff7f0e', '#2ca02c' => '#2ca02c', '#d62728' => '#d62728', '#9467bd' => '#9467bd', '#8c564b' => '#8c564b',
'#e377c2' => '#e377c2', '#7f7f7f' => '#7f7f7f', '#bcbd22' => '#bcbd22', '#17becf' => '#17becf'];
/**
* Define markers
*/
private $markers = ['.png', 'o', 'x', '^', 's', 'd'];
/**
* Define line & bar styles
*/
private $linestyles = ['bar', '--', '-.', ':', '-'];
/**
* Define Background Image GD
*/
public $bckgr_img_gd = null;
/**
* Define math object for ext_op_ml class
*/
public $math = null;
/**
* Config
*
* @var array
* @access protected
*
**/
protected $cfg = [];
/**
* GD
*
* @var image
* @access private
*
**/
protected $gd = null;
public function __construct( $cfg = null) {
$this->set_config( ( is_null($cfg) ) ? $this->default_cfg : $cfg );
$this->init_defaults( );
$this->math = new ext_op_ml( );
} // / __construct
/**
* Set CONFIG
* @param array $cfg
*/
public function set_config( $cfg ){
$this->cfg = $cfg;
} // /set_config()
/**
* Set 'backgroundimage' CONFIG
* @param string $filename
*/
public function imread( $filename ){
$this->cfg['backgroundimage'] = $filename;
$this->bckgr_img_gd = new img2img( $this->cfg['backgroundimage'] );
} // /imread()
/**
* Set 'draw_guidelines' CONFIG
* @param boolean $show
*/
public function set_drawguidelines( $show = true ){
$this->set_x_drawguidelines( $show );
$this->set_y_drawguidelines( $show );
} // /set_drawguidelines()
/**
* Set 'x_draw_guidelines' CONFIG
* @param boolean $show
*/
public function set_x_drawguidelines( $show = true ){
$this->cfg['x_drawguidelines'] = $show;
} // /set_x_drawguidelines()
/**
* Set 'width' CONFIG
* @param float $width
*/
public function width( $width = 6.4 ){
$this->cfg['width'] = $width;
// Change paddings automatically
// 6.4 -> .79
// $width -> X
$this->cfg['paddingleft'] = $width * .79 / 6.4;
$this->cfg['paddingright'] = $width * .63 / 6.4;
// Change font size automatically
$ponderation = 6.4 - (6.4 - $width)/2;
$this->cfg['xtickfontsize'] = $ponderation * 10.5 / 6.4;
// Change margin labels ticks automatically
$this->cfg['xmarginlabelsticks'] = $this->cfg['height'] * 16 / 4.8;
$this->cfg['ymarginlabelsticks'] = $this->cfg['width'] * 10 / 6.4;
} // /width()
/**
* Set 'legendwidthlines' CONFIG
* @param float $legendwidthlines
*/
public function legendwidthlines( $legendwidthlines = 28 ){
$this->cfg['legendwidthlines'] = $legendwidthlines;
} // /legendwidthlines()
/**
* Set 'xticks' CONFIG
* @param array $xticks
*/
public function xticks( $xticks ){
if( isset($xticks['rotation']) ){
$this->cfg['xticks']['rotation'] = $xticks['rotation'];
}
} // /xticks()
/**
* Set 'legendlabelheight' CONFIG
* @param float $legendlabelheight
*/
public function legendlabelheight( $legendlabelheight = 21 ){
$this->cfg['legendlabelheight'] = $legendlabelheight;
} // /legendlabelheight()
/**
* Set 'height' CONFIG
* @param float $height
*/
public function height( $height = 4.8 ){
$this->cfg['height'] = $height;
// Change paddings automatically
// 4.8 -> .58
// $height -> X
$this->cfg['paddingtop'] = $height * .58 / 4.8;
$this->cfg['paddingbottom'] = $height * .515 / 4.8;
// Change font size automatically
// For the size of horizontal text, we need to check width
$ponderation = 6.4 - (6.4- $this->cfg['width'])/2;
$this->cfg['ytickfontsize'] = $ponderation * 10.5 / 6.4;
// Change margin labels ticks automatically
$this->cfg['ymarginlabelsticks'] = $this->cfg['width'] * 10 / 6.4;
$this->cfg['xmarginlabelsticks'] = $this->cfg['height'] * 16 / 4.8;
// Change title margin automatically
$this->cfg['margintitle'] = $this->cfg['height'] * 50 / 4.8;
} // /height()
/**
* ASSIGN values
*
* @param array $values
*/
private function set_values( $values ){
$this->cfg['values'] = $values;
} // /set_values()
/**
* Set 'y_draw_guidelines' CONFIG
* @param boolean $show
*/
public function set_y_drawguidelines( $show = true ){
$this->cfg['y_drawguidelines'] = $show;
} // /set_y_drawguidelines()
/**
* Set 'legend' CONFIG
* @param boolean $legend
*/
public function legend( $legend = true ){
$this->cfg['legend'] = $legend;
} // /legend()
/**
* Set X LABEL
* @param string $xlabel
*/
public function xlabel( $xlabel = '' ){
$this->cfg['xlabel'] = $xlabel;
} // /xlabel()
/**
* Synonym of xlabel()
* @param string $xlabel
*/
public function set_xlabel( $xlabel = '' ){
$this->xlabel( $xlabel );
} // /set_xlabel()
/**
* Synonym of ylabel()
* @param string $ylabel
*/
public function set_ylabel( $ylabel = '' ){
$this->ylabel( $ylabel );
} // /set_ylabel()
/**
* Set Y LABEL
* @param string $ylabel
*/
public function ylabel( $ylabel = '' ){
$this->cfg['ylabel'] = $ylabel;
} // /ylabel()
/**
* Set TITLE
* @param string $title
*/
public function title( $title = '' ){
$this->cfg['title'] = $title;
} // /title()
/**
* Synonym of title()
* @param string $title
*/
public function set_title( $title = '' ){
$this->title( $title );
} // /set_title()
/**
* INIT DEFAULT CONFIGURATIONS
*/
private function init_defaults( ){
$this->cfg['axes']['prop_cycle'] = $this->cycler( 'color', 'default' ); // Assign a cycler to axes
} // /init_defaults()
/**
* GET cycler
*
* @param string $key
* @return array $cycler
*/
public function cycler( $key, $value = '' ){
switch(strtolower(trim($key))){
case 'color': return $this->cycler_color( $value );
break;
}
} // /cycler()
/**
* GET cycler Color
*
* @param string $colors
* @return array $cycler
*/
public function cycler_color( $colors = '' ){
if( is_array( $colors ) ){
$cycler = $this->get_colors_array_assoc( $colors );
} else {
// Is string. Need to split and return each rel color
switch(strtolower(trim($colors))){
case 'default':
case 'category10':
case 'vega':
case 'd3': $cycler = $this->cycler_color( $this->default_cfg['cycler']['color']['default'] ); // Return default config cycler colors
break;
case '': $cycler = $this->cfg['axes']['prop_cycle']; // Return assigned cycler colors
break;
default: $colors_splitted = split( $colors );
$cycler = $this->cycler_color( $colors_splitted ); // Return splitted cycler colors
unset($colors_splitted);
break;
} // /Switch $colors
} // /if is array
return $cycler;
} // /cycler_color()
/**
* GET colors array from abreviations
*
* @param string $colors_abrev
* @return array $colors
*/
private function get_colors_array_assoc($colors_abrev){
$colors = [];
foreach( $colors_abrev as $color){
$colors[] = $this->colors_rel[ $color ];
}
unset( $color );
return $colors;
} // /get_colors_array_assoc()
/**
* axes takes a list of [xmin, xmax, ymin, ymax] and specifies the viewport of the axes.
*
* 1st Method: Set Max & Min with params [ 'xmin' => $xmin, 'xmax' => $xmax, 'ymin' => $ymin, 'ymax' => $ymax ]
* 2nd Method: Set Max & Min with array [$xmin, $xmax, $ymin, $ymax]
* @param array $arr_values_min_max
*/
public function axes( $arr_values_min_max ){
if( $this->math->is_assoc($arr_values_min_max) ){
// Set Max & Min with associative array [ 'xmin' => $xmin, 'xmax' => $xmax, 'ymin' => $ymin, 'ymax' => $ymax ]
if( isset($arr_values_min_max['xmin']) && !is_null($arr_values_min_max['xmin']) ){
$this->cfg['global_force_min_x'] = $arr_values_min_max['xmin'];
}
if( isset($arr_values_min_max['xmax']) && !is_null($arr_values_min_max['xmax']) ){
$this->cfg['global_force_max_x'] = $arr_values_min_max['xmax'];
}
if( isset($arr_values_min_max['ymin']) && !is_null($arr_values_min_max['ymin']) ){
$this->cfg['global_force_min_y'] = $arr_values_min_max['ymin'];
}
if( isset($arr_values_min_max['ymax']) && !is_null($arr_values_min_max['ymax']) ){
$this->cfg['global_force_max_y'] = $arr_values_min_max['ymax'];
}
} else {
// Set Max & Min with array [$xmin, $xmax, $ymin, $ymax]
if( isset($arr_values_min_max[0]) && !is_null($arr_values_min_max[0]) ){
$this->cfg['global_force_min_x'] = $arr_values_min_max[0];
}
if( isset($arr_values_min_max[1]) && !is_null($arr_values_min_max[1]) ){
$this->cfg['global_force_max_x'] = $arr_values_min_max[1];
}
if( isset($arr_values_min_max[02]) && !is_null($arr_values_min_max[2]) ){
$this->cfg['global_force_min_y'] = $arr_values_min_max[2];
}
if( isset($arr_values_min_max[3]) && !is_null($arr_values_min_max[3]) ){
$this->cfg['global_force_max_y'] = $arr_values_min_max[3];
}
}
}// /axes()
/**
* PLOT sub method to check if begin with 0
*
* @param array $cfg // ['y_begin_with_zero' => true] to draw y values begin with value 0
* @return boolean $y_begin_with_zero
*/
public function plot_y_begin_with_zero( $cfg = null ){
$y_begin_with_zero = false;
if( !is_null($cfg) ){
if( isset($cfg['y_begin_with_zero']) && $cfg['y_begin_with_zero'] ){
$y_begin_with_zero = true;
}
}
return $y_begin_with_zero;
} // /plot_y_begin_with_zero()
/**
* PLOT sub method to get plot type
*
* @param array $cfg // ['type' => '-' || '--' 'bar'
* @return string $type
*/
public function plot_type( $cfg = null ){
$type = '-';
if( !is_null($cfg) ){
if( isset($cfg['type']) ){
if( strpos($cfg['type'], 'bar') !== false ){
$type = 'bar';
} else
if( strpos($cfg['type'], 'scatter') !== false ){
$type = 'scatter';
} else if( strpos($cfg['type'], '--') !== false ){
$type = '--';
} else if( strpos($cfg['type'], '-') !== false ){
$type = '-';
}
}
}
return $type;
} // /plot_type()
/**
* PLOT sub method to get plot color
*
* @param array $cfg // ['color' => ...
* @return string $color
*/
public function plot_next_color( $cfg = null ){
$arr_color_cycler = $this->cycler( 'color' );
$countval = count($this->cfg['values']);
$color_id = $countval % count($this->cycler( 'color', 'default' ));
$color = $arr_color_cycler[ $color_id ];
unset( $arr_color_cycler );
unset( $countval );
unset( $color_id );
return $color;
} // /plot_next_color()
/**
* RECALCULATES MAX & MIN of all values
*/
private function recalc_max_min( ){
$values =$this->cfg['values'][0];
$global_max_x = max( $values['values_x'] );
$global_min_x = min( $values['values_x'] );
$global_max_y = max( $values['values_y'] );
$global_min_y = min( $values['values_y'] );
foreach( $this->cfg['values'] as $key => $value ){
if( $key == 0) continue;
$values =$this->cfg['values'][$key];
if( $global_max_x < max( $values['values_x'] ) ){
$global_max_x = max( $values['values_x'] );
}
if( $global_min_x > min( $values['values_x'] ) ){
$global_min_x = min( $values['values_x'] );
}
if( $global_max_y < max( $values['values_y'] ) ){
$global_max_y = max( $values['values_y'] );
}
if( $global_min_y > min( $values['values_y'] ) ){
$global_min_y = min( $values['values_y'] ) ;
}
} // /foreach $this->cfg['values']
$global_diff_x = $global_max_x - $global_min_x;
$global_diff_y = $global_max_y - $global_min_y;
$this->cfg['global_max_x'] = $global_max_x;
$this->cfg['global_min_x'] = $global_min_x;
$this->cfg['global_max_y'] = $global_max_y;
$this->cfg['global_min_y'] = $global_min_y;
$this->cfg['global_diff_x'] = $global_diff_x;
$this->cfg['global_diff_y'] = $global_diff_y;
$this->cfg['global_count_x'] = count( $values['values_x'] );
$this->cfg['global_count_y'] = count( $values['values_y'] );
unset( $values );
unset( $global_diff_x );
unset( $global_diff_y );
unset( $global_max_x );
unset( $global_min_x );
unset( $global_max_y );
unset( $global_min_y );
unset( $key );
unset( $value );
unset( $values );
}// /recalc_max_min()
/**
* PLOT. Add serie of values to graph
*
* @param array $arr_values
* @param array $arr_values_y_param
* @param array $cfg // ['type' => '-' || '--' || 'o' | 'x' || '^' || 's']
*/
public function plot( $arr_values, $arr_values_y_param = null, $cfg = null ){
if( is_array($arr_values[0]) ){
// Call plot() recursively
foreach( $arr_values as $value){
$param1 = $value[0];
$param2 = ( isset( $value[1] )?$value[1]:null );
$param3 = ( isset( $value[2] )?$value[2]:null );
$this->plot( $param1, $param2, $param3 );
}
unset( $param1 );
unset( $param2 );
unset( $param3 );
unset( $value );
return;
}
$y_begin_with_zero = $this->plot_y_begin_with_zero( $cfg );
$type = $this->plot_type( $cfg );
$color = $this->plot_next_color( $cfg );
$values_are_keys = is_null($arr_values_y_param);
$label = '';
if( !is_null( $cfg ) && isset($cfg['label']) ){
$label = $cfg['label'];
}
$marker = $markerfilename = '';
if( !is_null( $cfg ) && isset($cfg['marker']) ){
$marker = strtolower( trim( $cfg['marker'] ) );
if( strpos( $marker, '.png' ) !== false ){
$markerfilename = $marker;
$marker = '.png';
}
}
if( !is_null( $cfg ) && !is_array( $cfg ) && is_string( $cfg ) ){
// Config param is string. Extract config parts
// search for markers as 'o', 'x', '^', 's', 'd'
$cfg = strtolower( trim( $cfg ) );
foreach( $this->markers as $mark ){
if( strpos( $cfg, $mark ) !== false ){
$marker = $mark;
}
}
unset( $mark );
// Search for colors
foreach( $this->colors_rel as $key => $col_rel ){
if( strpos( $cfg, $key ) !== false ){
$color = $col_rel;
}
}
unset( $col_rel );
unset( $key );
// search for styles as '--', '-', 'bar'
// Needed in config str. If not given, will not drawed line
$type = '';
foreach( $this->linestyles as $style ){
if( strpos( $cfg, $style ) !== false ){
$type = $style;
break;
}
}
unset( $style );
}
if( $values_are_keys ) {
$x_values_return = [];
for($i=0;$i<count($arr_values);$i++){
$x_values_return[] = $i;
}
$arr_values_x = $x_values_return;
unset( $x_values_return );
} else {
array_multisort( $arr_values, $arr_values_y_param);
$arr_values_x = $arr_values;
}
if( $values_are_keys ){
$arr_values_y = $arr_values;
} else {
$arr_values_y = $arr_values_y_param;
}
$max_x = max( $arr_values_x );
$max_y = max( $arr_values_y );
$min_x = min( $arr_values_x );
$min_y = min( $arr_values_y );
$count_x = count( $arr_values_x );
$count_y = count( $arr_values_y );
$diff_x = ( $max_x - $min_x );
$diff_y = ( $max_y - $min_y );
$this->cfg['values'][] = [ 'type' => $type, 'color'=>$color, 'values_x' => $arr_values_x, 'values_y' => $arr_values_y, 'max_x' => $max_x, 'min_x' => $min_x, 'max_y' => $max_y, 'min_y' => $min_y,
'count_x' => $count_x, 'count_y' => $count_y, 'diff_x' => $diff_x, 'diff_y' => $diff_y, 'values_are_keys' => $values_are_keys, 'label' => $label,
'marker' => $marker, 'markerfilename' => $markerfilename ];
unset( $y_begin_with_zero );
unset( $type );
unset( $color );
unset( $values_are_keys );
unset( $label );
unset( $marker );
unset( $max_x );
unset( $max_y );
unset( $min_x );
unset( $min_y );
unset( $count_x );
unset( $count_y );
unset( $diff_x );
unset( $diff_y );
unset( $arr_values_x );
unset( $arr_values_y );
unset( $i );
} // /plot()
/**
* BAR GRAPH PLOT. generate graph
*
* @param array $arr_values
* @param array $arr_values_y_param
* @param array $cfg // ['type' => '-' || '--' || 'o' || '^' || 's' || 'd']
*/
public function bar( $arr_values, $arr_values_y_param = null, $cfg = null ){
if( is_null($cfg) ){
$cfg = [];
}
$cfg['type'] = 'bar';
$this->plot( $arr_values, $arr_values_y_param, $cfg );
} // /bar()
/**
* SCATTER GRAPH PLOT. generate graph
*
* $arr_values have all series of values as:
* [
* [ [x0, x1, x2, x3], [y0, y1, y2, y3], $cfg ], // first serie. $cfg can be null
* [ [x0, x1, x2, x3], [y0, y1, y2, y3], $cfg ] // second serie. $cfg can be null
* ..... // n series. $cfg can be null
* ]
*
* @param array $arr_values
* @param array $cfg // ['type' => '-' || '--' || 'o' || '^' || 's' || 'd']
*/
public function scatter( $arr_values ){
foreach( $arr_values as $arr_values_serie){
if( !isset( $arr_values_serie[2] ) || (isset( $arr_values_serie[2] ) && is_null($arr_values_serie[2])) ){
$cfg = [];
} else {
$cfg = $arr_values_serie[2];
}
$cfg['type'] = 'scatter';
$cfg['marker'] = 'o';
$this->plot( $arr_values_serie[0], $arr_values_serie[1], $cfg );
}
} // /scatter()
/**
* HISTOGRAM GRAPH PLOT. generate graph
*
* $cfg['num_blocks] = blocks to divide hist. Default = 10
*
* @param array $arr_values
*/
public function hist( $arr_values, $cfg = null ){
if( is_null($cfg) ){
$cfg = [];
}
$num_blocks = (( isset($cfg['num_blocks']) )?$cfg['num_blocks']:10);
$min = min($arr_values);
$max = max($arr_values);
$diff = $max - $min;
$arr_hist = array_fill(0, $num_blocks, 0); // Fill count values to 0
$div = ( ( $num_blocks - 1 ) / $diff );
foreach( $arr_values as $value){
// $diff -> ( $num_blocks - 1 )
// ($value - $min) -> X
$block_index = (int)round( ($value - $min) * $div ); //$block_index = ( ($value - $min) * ( $num_blocks - 1 ) / $diff );
++$arr_hist[$block_index];
}
$cfg['type'] = 'bar';
$this->plot( $this->math->linspace($min, $max, $num_blocks), $arr_hist, $cfg );
$this->axes([null, null, 0, max($arr_hist) ]);
unset( $arr_hist );
unset( $min );
unset( $max );
unset( $diff );
unset( $value );
unset( $num_blocks );
unset( $div );
unset( $block_index );
} // /hist()
/**
* Show Legend
*/
public function generate_gd_legend( ){
$this->set_labels_if_empty();
$left = $this->cfg['pix_paddingleft'] + 1 + $this->cfg['legendmarginleft'];
$top = $this->cfg['pix_paddingtop'] + 1 + $this->cfg['legendmargintop'];
$width = $this->cfg['legendpaddingleft'] + $this->cfg['legendpaddingright'];
$width += $this->cfg['legendwidthlines'] + 12; // 12 = margin between lines and text
// Calculates max length of labels
$font_path = $this->cfg['fontdir']. '/' . $this->cfg['fontfamilypath']. '/' . $this->cfg['font'];
$font_size = $this->cfg['fontsize'];
$arrlabels = [];
foreach( $this->cfg['values'] as $val ){
$arrlabels[] = $val['label'];
}
$max_length = $this->math->arr_max_len_ttf( $arrlabels, $font_path, $font_size );
$width += $max_length;
$height = $this->cfg['legendpaddingtop'] + $this->cfg['legendpaddingbottom'];
$height += $this->cfg['legendlabelheight'] * count( $this->cfg['values'] );
// White Transp
$white_transp = imagecolorallocatealpha($this->gd, 255, 255, 255, 25);
imagefilledrectangle( $this->gd, $left, $top, $left + $width, $top + $height, $white_transp) ;
$color = imagecolorallocate( $this->gd, 214, 214, 214); // gray
$this->generate_gd_legend_round_border( $left, $top, $width, $height, $color );
$color = imagecolorallocate( $this->gd, 247, 247, 247); // light gray
$this->generate_gd_legend_round_border( $left-1, $top-1, $width+2, $height+2, $color );
$this->generate_gd_legend_round_border( $left+1, $top+1, $width-2, $height-2, $color );
foreach( $this->cfg['values'] as $key => $values){
$this->generate_gd_legend_line( $left, $top, $key, $this->cfg['legendlabelheight'] );
}
foreach( $this->cfg['values'] as $key => $values){
$this->gd_draw_text_legend( $left, $top, $key, $this->cfg['legendlabelheight'] );
}
unset( $left );
unset( $top );
unset( $width );
unset( $height );
unset( $max_length );
unset( $key );
unset( $values );
unset( $color );
unset( $white_transp );
unset( $length_value );
unset( $font_path );
unset( $font_size );
unset( $val );
unset( $arrlabels );
}// /generate_gd_legend()
/**
* Set labels to init texts if they are empty
*/
private function set_labels_if_empty( ){
foreach( $this->cfg['values'] as $key => $values){
if( !isset( $values['label'] ) || isset( $values['label'] ) && $values['label'] == '' ){
$this->cfg['values'][$key]['label'] = 'legend ['.$key.']';
}
}
} // /set_labels_if_empty()
/**
* Draw the legend
*
* @param integer $left
* @param integer $top
* @param integer $key
* @param integer $labelheight
*/
private function gd_draw_text_legend( $left, $top, $key, $labelheight ){
$angle = 0;
$font_path = $this->cfg['fontdir']. '/' . $this->cfg['fontfamilypath']. '/' . $this->cfg['font'];
$text_color = imagecolorallocate( $this->gd, 0, 0, 0); // Black
$left += $this->cfg['legendpaddingleft'] + $this->cfg['legendwidthlines'] + 12;
$top += $this->cfg['legendpaddingtop'] + $labelheight * $key + ($labelheight/1.4);
imagettftext($this->gd, $this->cfg['fontsize'], $angle, $left, $top, $text_color, $font_path, $this->cfg['values'][$key]['label'] );
unset( $angle );
unset( $font_path );
unset( $text_color );
unset( $left );
unset( $top );
} // /gd_draw_text_legend()
/**
* Show rectanle with round corners
*
* Draw a rectangle with rounded corners.
* from https://gist.github.com/mistic100/9301c0eebaef047bfdc8
*
* @param integer $left
* @param integer $top
* @param integer $width
* @param integer $height
* @param object $color
*/
public function generate_gd_legend_round_border( $left, $top, $width, $height, $color ){
$r = 1;
$x1 = $left;
$x2 = ( $left + $width );
$y1 = $top;
$y2 = ( $top + $height );
$img = &$this->gd;
$r = min($r, floor(min(($x2-$x1)/2, ($y2-$y1)/2)));
// top border
imageline($img, $x1+$r, $y1, $x2-$r, $y1, $color);
// right border
imageline($img, $x2, $y1+$r, $x2, $y2-$r, $color);
// bottom border
imageline($img, $x1+$r, $y2, $x2-$r, $y2, $color);
// left border