forked from jerrywham/phpGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpGraph.php
1678 lines (1533 loc) · 62.2 KB
/
phpGraph.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
# ------------------ BEGIN LICENSE BLOCK ------------------
#
##################################################################
# #
# ▓▓▓▓▓ ▓▓ ▓▓ ▓▓▓▓▓ ▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓ ▓▓▓▓▓ ▓▓ ▓▓ #
# ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ #
# ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ #
# ▓▓▓▓▓ ▓▓▓▓▓▓ ▓▓▓▓▓ ▓▓ ▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓▓ #
# ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ #
# ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ #
# ▓▓ ▓▓ ▓▓ ▓▓ ▓▓▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ #
# #
##################################################################
# @update 2017-12-27
# @copyright 2013-2017 Cyril MAGUIRE
# @licence http://www.cecill.info/licences/Licence_CeCILL_V2.1-fr.txt CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL version 2.1
# @link http://jerrywham.github.io/phpGraph/
# @version 1.4
#
# ------------------- END LICENSE BLOCK -------------------
class phpGraph {
# Basic css style
protected $css = '
/**/.draw {
width:70%;/*Adjust this value to resize svg automatically*/
margin:auto;
}
/**/svg {/*width and height of svg is 100% of dimension of its parent (draw class here)*/
display: block;
margin:auto;
margin-bottom: 50px;
}
/**/.graph-title {
stroke-width:4;
stroke:transparent;
fill:#000033;
font-size: 1.2em;
}
/**/.graph-grid {
stroke-width:1;
stroke:#c4c4c4;
}
/**/.graph-stroke {
stroke-width:2;
stroke:#424242;
}
/**/.graph-legends {}
/**/.graph-active .graph-legend {
font-size:0.6em;
}
/**/.graph-legend-stroke {}
/**/.graph-line {
stroke-linejoin:round;
stroke-width:2;
}
/**/.graph-fill {
stroke-width:0;
}
/**/.graph-bar {}
/**/.graph-point {
stroke-width:1;
fill:#fff;
fill-opacity:1;
stroke-opacity:1;
}
/**/.graph-point-active:hover {
stroke-width:5;
transition-duration:.9s;
cursor: pointer;
}
/**/title.graph-tooltip {
background-color:#d6d6d6;
}
/**/.graph-pie {
cursor: pointer;
stroke-width:1;
stroke:#fff;
}
/**/text {
fill:#000;
font-size:0.7em;
}
';
protected $options = array(
'width' => null,// (int) width of grid
'height' => null,// (int) height of grid
'paddingTop' => 10,// (int)
'type' => 'line',// (string) line, bar, pie, ring, stock or h-stock (todo curve)
'steps' => null,// (int) 2 graduations on y-axis are separated by $steps units. "steps" is automatically calculated but we can set the value with integer. No effect on stock and h-stock charts
'filled' => true,// (bool) to fill lines/histograms/disks
'tooltips' => false,// (bool) to show tooltips
'circles' => true,// (bool) to show circles on graph (lines or histograms). No effect on stock and h-stock charts
'stroke' => '#3cc5f1',// (string) color of lines by default. Use an array to personalize each line
'background' => "#ffffff",// (string) color of grid background. Don't use short notation (#fff) because of $this->__genColor();
'opacity' => '0.5',// (float) between 0 and 1. No effect on stock and h-stock charts
'gradient' => null,// (array) 2 colors from left to right
'titleHeight' => 0,// (int) Height of main title
'tooltipLegend' => null,// (string or array) Text display in tooltip with y value. Each text can be personalized using an array. No effect on stock and h-stock charts
'legends' => null,// (string or array or bool) General legend for each line/histogram/disk displaying under diagram
'title' => null,// (string) Main title. Title wil be displaying in a tooltip too.
'radius' => 100,// (int) Radius of pie
'diskLegends' => false,// (bool) to display legends around a pie
'diskLegendsType' => 'label',// (string) data, pourcent or label to display around a pie as legend
'diskLegendsLineColor' => 'darkgrey',// (string) color of lines which join pie to legends
'responsive' => true,// (bool) to avoid svg to be responsive (dimensions fixed)
'paddingLegendX' => 10,//We add 10 units in viewbox to display x legend correctly
'paddingLegendY' => 0,//Padding to display y legend correctly, if needed, for pie and ring
'transform' => null,//Transformation of the text of the legend
'marginTop' => 0,//Margin of the legend when use transform
);
# authorized types
protected $types = array('line','line','bar','pie','ring','stock','h-stock');
protected $periodOfCache = 1;//A REGLER ET A VERIFIER
//protected $colors = array();
/**
* Constructor
*
* @param $width integer Width of grid
* @param $height integer Height of grid
* @param $options array Options
* @return stdio
*
* @author Cyril MAGUIRE
**/
public function __construct($width=600,$height=300,$options=array()) {
if (!empty($options)) {
$this->options = $options;
}
if (!empty($width)) {
$this->options['width'] = $width;
}
if (!empty($height)) {
$this->options['height'] = $height;
}
if (is_string($this->options['stroke']) && substr($this->options['stroke'], 0,1) == '#') {
$this->options['stroke'] = array(0=>substr($this->options['stroke'],0,7));
}
if (is_string($this->options['type']) && in_array($this->options['type'], $this->types)) {
$this->options['type'] = array(0=>$this->options['type']);
}
}
/**
* To add your own style
* @param $css string your css
* @return string css
*
* @author Cyril MAGUIRE
*/
public function setCss($css) {
if (is_string($css) && !empty($css)) {
$this->css .= $css;
}
}
/**
* Main function
* @param $data array Uni or bidimensionnal array
* @param $options array Array of options
* @param $putInCache string path of directory where cache will be recorded
* @param $id string index of svg tag
* @return string SVG
*
* @author Cyril MAGUIRE
*/
public function draw($data,$options=array(),$putInCache=false,$id=false,$txt=false) {
if ($id) {
$this->css = str_replace('/**/', '#'.$id.' ', $this->css);
}
$downloadLink = '<p class="downloadSvg"><a href="'.$id.'">'.$txt.'</a></p>';
# Cache
$nameOfFiles = glob($putInCache.'*.svg');
if ($putInCache != false && isset($nameOfFiles[0])) {
$stat = stat($nameOfFiles[0]);
if ($stat['mtime'] > (time() - $this->periodOfCache) ) {
return file_get_contents($nameOfFiles[0]).($id ? $downloadLink : '');
}
}
$return = '';
$options = array_merge($this->options,$options);
extract($options);
if ($title) {
$options['titleHeight'] = $titleHeight = 40;
}
if ($opacity < 0 || $opacity > 1) {
$options['opacity'] = 0.5;
}
if (!is_string($diskLegendsLineColor)) {
$diskLegendsLineColor = 'darkgrey';
}
$HEIGHT = $height+$titleHeight+$paddingTop;
$heightLegends = 0;
if (isset($legends) && !empty($legends)) {
$heightLegends = count($legends)*30+2*$paddingTop;
}
$pie = '';
if ($type != 'pie' && $type != 'ring') {
# looking for min and max
extract($this->__minMax($data,$type));
$options['type'] = $type;
extract($this->__xAxisConfig($type,$width,$max,$Xmax,$Xmin,$lenght,$options));
$options['steps'] = $steps;
$unitY = ($height/abs(($max+$steps)-$min));
$gridV = $gridH = '';
$x = $y = '';
$headerDimensions = $this->__headerDimensions($widthViewBox,$HEIGHT,$heightLegends,$titleHeight,$paddingTop,$paddingLegendX,$lenght,$stepX);
# Size of canevas will be bigger than grid size to display legends
$return = $this->__header($headerDimensions,$responsive,$id);
if ($type == 'stock' || (is_array($type) && in_array('stock',$type)) ) {
$plotLimit = $this->__stockDef();
}
if ($type == 'h-stock' || (is_array($type) && in_array('h-stock',$type)) ) {
$plotLimit = $this->__hstockDef();
}
# we draw the grid
$return .= $this->__svgGrid($gradient,$width,$height,($paddingTop+$titleHeight));
if ($title) {
$return .= $this->__titleDef($title,$width,$titleHeight);
}
# Legends x axis and vertical grid
extract($this->__XAxisDef($type,$Xmin,$Xmax,$XM,$stepX,$unitX,$HEIGHT,$paddingTop,$titleHeight,$labels,$lenght, $transform));
# Legendes y axis and horizontal grid
extract($this->__YAxisDef($type,$width,$min,$max,$steps,$HEIGHT,$titleHeight,$paddingTop,$paddingLegendX,$unitY,$labels));
//Grid
$return .= "\t".'<g class="graph-grid">'."\n";
$return .= $gridH."\n";
$return .= $gridV;
$return .= "\t".'</g>'."\n";
$return .= $x;
$return .= $y;
if (!$multi) {
if (is_array($type) && count($type) == 1) {
$type = $type[0];
$options['type'] = $type;
}
$options['stroke'] = is_array($stroke) ? $stroke[0] : $stroke;
switch ($type) {
case 'line':
$return .= $this->__drawLine($data,$height,$HEIGHT,$stepX,$unitY,$lenght,$min,$max,$options);
break;
case 'bar':
$return .= $this->__drawBar($data,$height,$HEIGHT,$stepX,$unitY,$lenght,$min,$max,$options);
break;
default:
$return .= $this->__drawLine($data,$height,$HEIGHT,$stepX,$unitY,$lenght,$min,$max,$options);
break;
}
} else {
$i = 1;
foreach ($data as $line => $datas) {
if (!isset($type[$line]) && !is_string($type) && is_numeric($line)) {
$type[$line] = 'line';
}
if (!isset($type[$line]) && !is_string($type) && !is_numeric($line)) {
$type[$line] = 'stock';
}
if (is_string($options['type'])) {
$type = array();
$type[$line] = $options['type'];
}
if (!isset($tooltipLegend[$line])) {
$options['tooltipLegend'] = '';
} else {
$options['tooltipLegend'] = $tooltipLegend[$line];
}
if (!isset($stroke[$line])) {
$stroke[$line] = $this->__genColor();
}
$options['stroke'] = $STROKE = $stroke[$line];
$options['fill'] = $stroke[$line];
switch ($type[$line]) {
case 'line':
$return .= $this->__drawLine($datas,$height,$HEIGHT,$stepX,$unitY,$lenght,$min,$max,$options);
break;
case 'bar':
$return .= $this->__drawBar($datas,$height,$HEIGHT,$stepX,$unitY,$lenght,$min,$max,$options);
break;
case 'stock':
$id = rand();
$return .= str_replace(array('id="plotLimit"','stroke=""'), array('id="plotLimit'.$id.'"','stroke="'.$stroke[$line].'"'), $plotLimit);
$return .= $this->__drawStock($data,$height,$HEIGHT,$stepX,$unitY,$lenght,$min,$max,$options,$i,$labels,$id);
$i++;
break;
case 'h-stock':
$id = rand();
$return .= str_replace(array('id="plotLimit"','stroke=""'), array('id="plotLimit'.$id.'"','stroke="'.$stroke[$line].'"'), $plotLimit);
$return .= $this->__drawHstock($data,$HEIGHT,$stepX,$unitX,$unitY,$lenght,$Xmin,$Xmax,$options,$i,$labels,$id);
$i++;
break;
case 'ring':
$options['subtype'] = 'ring';
case 'pie':
$options['multi'] = $multi;
if (is_array($stroke)) {
$options['stroke'] = $stroke[$line];
$options['fill'] = $stroke;
}
if (is_array($legends)) {
$options['legends'] = $legends[$line];
}
$pie .= $this->__drawDisk($datas,$options,$id);
$pie .= "\n".'</svg>'."\n";
break;
default:
$return .= $this->__drawLine($datas,$height,$HEIGHT,$stepX,$unitY,$lenght,$min,$max,$options);
break;
}
}
}
# legends
$return .= $this->__legendsDef($legends,$type,$stroke,$HEIGHT,$paddingTop,$marginTop);
$return .= "\n".'</svg>'."\n";
$return .= $pie;
} else {
$options['tooltipLegend'] = array();
if ($tooltipLegend && !is_array($tooltipLegend)) {
foreach ($data as $key => $value) {
$options['tooltipLegend'][] = $tooltipLegend;
}
}
if ($tooltipLegend && is_array($tooltipLegend)) {
$options['tooltipLegend'] = $tooltipLegend;
}
$options['stroke'] = array();
if (isset($stroke) && !is_array($stroke)) {
foreach ($data as $key => $value) {
$options['stroke'][] = $stroke;
}
}
if (isset($stroke) && is_array($stroke)) {
$options['stroke'] = $stroke;
}
foreach ($data as $line => $datas) {
if (is_array($datas)) {
if (is_array($stroke)) {
$options['stroke'] = $stroke[$line];
$options['fill'] = $stroke;
}
if (is_array($legends)) {
$options['legends'] = $legends[$line];
}
$return .= $this->__drawDisk($datas,$options,$id);
$return .= "\n".'</svg>'."\n";
$multi = true;
} else {
$multi = false;
}
}
if (!$multi) {
if (is_array($stroke)) {
$options['stroke'] = $stroke;
$options['fill'] = $stroke;
}
if (is_array($legends)) {
$options['legends'] = $legends;
}
$return .= $this->__drawDisk($data,$options,$id);
$return .= "\n".'</svg>'."\n";
}
}
$this->colors = array();
if ($putInCache && $stat['mtime'] > (time() - $this->periodOfCache) ) {
$this->putInCache(trim($return),md5(implode('',$nameOfFile)),$putInCache);
//file_put_contents($putInCache, trim($return));
}
return $return.($id ? $downloadLink : '');
}
/**
* To draw lines
* @param $data array Unidimensionnal array
* @param $height integer Height of grid
* @param $HEIGHT integer Height of grid + title + padding top
* @param $stepX integer Unit of x-axis
* @param $unitY integer Unit of y-axis
* @param $lenght integer Size of data array
* @param $min integer Minimum value of data
* @param $max integer Maximum value of data
* @param $options array Options
* @return string Path of lines (with options)
*
* @author Cyril MAGUIRE
*/
protected function __drawLine($data,$height,$HEIGHT,$stepX,$unitY,$lenght,$min,$max,$options) {
$return = '';
extract($options);
$this->colors[] = $options['stroke'];
//Ligne
$i = 0;
$c = '';
$t = '';
$path = "\t\t".'<path d="';
foreach ($data as $label => $value) {
if ($min <=0) {
$V = ($value-$min);
} else {
$V = $value;
}
$coordonneesCircle1 = 'cx="'.($i * $stepX + 50).'" cy="'.($HEIGHT - $unitY*$V).'"';
//$min == $value
$coordonneesCircle2 = 'cx="'.($i * $stepX + 50).'" cy="'.($HEIGHT - $unitY*$V - $value*$unitY).'"';
$coordonnees1 = ($i * $stepX + 50).' '.($HEIGHT - $unitY*$V);
//$min == $value
$coordonnees2 = ($i * $stepX + 50).' '.($HEIGHT - $unitY*$V - $value*$unitY);
# Tooltips
if($tooltips == true) {
$c .= "\n\t\t".'<g class="graph-active">';
}
# Line
$c1 = $this->__c($coordonneesCircle1,$stroke);
$c2 = $this->__c($coordonneesCircle2,$stroke);
if ($value != $max) {
if ($value == $min) {
if ($i == 0) {
if ($min<=0) {
$path .= 'M '.$coordonnees1.' L';
//Tooltips and circles
$c .= $c1;
} else {
$path .= 'M '.$coordonnees2.' L';
//Tooltips and circles
$c .= $c2;
}
} else {
$path .= "\n\t\t\t\t".$coordonnees1;
//Tooltips and circles
$c .= $c1;
}
} else {
if ($i == 0) {
$path .= 'M '.$coordonnees1.' L';
//Tooltips and circles
$c .= $c1;
} else {
$path .= "\n\t\t\t\t".$coordonnees1;
//Tooltips and circles
$c .= $c1;
}
}
} else {
//Line
if ($i == 0) {
$path .= 'M '.($i * $stepX + 50).' '.($titleHeight + 2 * $paddingTop).' L';
//Tooltips and circles
$c .= "\n\t\t\t".'<circle cx="'.($i * $stepX + 50).'" cy="'.($titleHeight + 2 * $paddingTop).'" r="3" stroke="'.$stroke.'" class="graph-point-active"/>';
} else {
$path .= "\n\t\t\t\t".$coordonnees1;
//Tooltips and circles
$c .= "\n\t\t\t".'<circle '.$coordonneesCircle1.' r="3" stroke="'.$stroke.'" class="graph-point-active"/>';
}
}
$i++;
//End tooltips
if($tooltips == true) {
$c .= "\n\t\t\t".'<title class="graph-tooltip">'.(is_array($tooltipLegend) ? $tooltipLegend[$i] : $tooltipLegend).$value.'</title>'."\n\t\t".'</g>';
}
}
if ($opacity > 0.8 && $filled === true) {
$tmp = $stroke;
$stroke = '#a1a1a1';
}
//End of line
$pathLine = '" class="graph-line" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>'."\n";
//Filling
if ($filled === true) {
if ($min<=0) {
$path .= "\n\t\t\t\t".(($i - 1) * $stepX + 50).' '.($HEIGHT + ($unitY)*($min-$value) + ($unitY * $value)).' 50 '.($HEIGHT + ($unitY)*($min-$value) + ($unitY * $value))."\n\t\t\t\t";
} else {
$path .= "\n\t\t\t\t".(($i - 1) * $stepX + 50).' '.$HEIGHT.' 50 '.$HEIGHT."\n\t\t\t\t";
}
if ($opacity > 0.8) {
$stroke = $tmp;
}
$return .= $path.'" class="graph-fill" fill="'.$stroke.'" fill-opacity="'.$opacity.'"/>'."\n";
}
//Display line
$return .= $path.$pathLine;
if($circles == true) {
$return .= "\t".'<g class="graph-point">';
$return .= $c;
$return .= "\n\t".'</g>'."\n";
}
return $return;
}
/**
* To draw histograms
* @param $data array Unidimensionnal array
* @param $height integer Height of grid
* @param $HEIGHT integer Height of grid + title + padding top
* @param $stepX integer Unit of x-axis
* @param $unitY integer Unit of y-axis
* @param $lenght integer Size of data array
* @param $min integer Minimum value of data
* @param $max integer Maximum value of data
* @param $options array Options
* @return string Path of lines (with options)
*
* @author Cyril MAGUIRE
*/
protected function __drawBar($data,$height,$HEIGHT,$stepX,$unitY,$lenght,$min,$max,$options) {
$return = '';
extract($options);
$this->colors[] = $options['stroke'];
//Bar
$bar = '';
$i = 0;
$c = '';
$t = '';
foreach ($data as $label => $value) {
if ($min <=0) {
$V = ($value-$min);
} else {
$V = $value;
}
//Tooltips and circles
if($tooltips == true) {
$c .= "\n\t\t".'<g class="graph-active">';
}
$stepY = $value*$unitY;
//$min>=0
$coordonnees1 = 'x="'.($i * $stepX + 50).'" y="'.($HEIGHT - $unitY*$V).'"';
//On recule d'un demi pas pour que la valeur de x soit au milieu de la barre de diagramme
$coordonnees2 = 'x="'.($i * $stepX + 50 - $stepX/2).'" y="'.($HEIGHT - $stepY).'"';
//$min<0
$coordonnees3 = 'x="'.($i * $stepX + 50 - $stepX/2).'" y="'.($HEIGHT - $unitY*$V).'"';
//$min<0 et $value<0
$coordonnees4 = 'x="'.($i * $stepX + 50 - $stepX/2).'" y="'.($HEIGHT - $unitY*$V + $stepY).'"';
$coordonnees5 = 'x="'.($i * $stepX + 50).'" y="'.($HEIGHT - $unitY*$V + $stepY).'"';
//$min>=0 et $value == $max
$coordonnees6 = 'x="'.($i * $stepX + 50).'" y="'.($paddingTop + $titleHeight).'"';
//$value == 0
$coordonnees7 = 'x="50" y="'.($HEIGHT + $unitY*$min).'"';
if ($value == 0) {
$stepY = 1;
}
//Diagramme
//On est sur la première valeur, on divise la largeur de la barre en deux
if ($i == 0) {
if ($value == $max) {
$bar .= "\n\t".'<rect '.$coordonnees6.' width="'.($stepX/2).'" height="'.$height.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
$c .= "\n\t\t\t".'<circle c'.str_replace('y="', 'cy="', $coordonnees6).' r="3" stroke="'.$stroke.'" class="graph-point-active"/>';
} else {
if ($min>=0) {
$bar .= "\n\t".'<rect '.$coordonnees1.' width="'.($stepX/2).'" height="'.$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
$c .= "\n\t\t\t".'<circle c'.str_replace('y="', 'cy="', $coordonnees1).' r="3" stroke="'.$stroke.'" class="graph-point-active"/>';
} else {
if ($value == $min) {
$bar .= "\n\t".'<rect '.$coordonnees5.' width="'.($stepX/2).'" height="'.-$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
} else {
if ($value == 0) {
$bar .= "\n\t".'<rect '.$coordonnees7.' width="'.($stepX/2).'" height="1" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
} else {
$bar .= "\n\t".'<rect '.$coordonnees1.' width="'.($stepX/2).'" height="'.$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
}
}
$c .= "\n\t\t\t".'<circle c'.str_replace('y="', 'cy="', $coordonnees1).' r="3" stroke="'.$stroke.'" class="graph-point-active"/>';
}
}
} else {
if ($value == $max) {
if ($min>=0) {
//Si on n'est pas sur la dernière valeur
if ($i != $lenght-1) {
$bar .= "\n\t".'<rect '.$coordonnees2.' width="'.$stepX.'" height="'.$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
} else {
$bar .= "\n\t".'<rect '.$coordonnees2.' width="'.($stepX/2).'" height="'.$height.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
}
$c .= "\n\t\t\t".'<circle c'.str_replace('y="', 'cy="', $coordonnees1).' r="3" stroke="'.$stroke.'" class="graph-point-active"/>';
} else {
if ($value >= 0) {
//Si on n'est pas sur la dernière valeur
if ($i != $lenght-1) {
$bar .= "\n\t".'<rect '.$coordonnees3.' width="'.$stepX.'" height="'.$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
} else {
$bar .= "\n\t".'<rect '.$coordonnees3.' width="'.($stepX/2).'" height="'.$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
}
} else {
//Si on n'est pas sur la dernière valeur
if ($i != $lenght-1) {
$bar .= "\n\t".'<rect '.$coordonnees4.' width="'.$stepX.'" height="'.-$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
} else {
$bar .= "\n\t".'<rect '.$coordonnees4.' width="'.($stepX/2).'" height="'.-$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
}
}
$c .= "\n\t\t\t".'<circle c'.str_replace('y="', 'cy="', $coordonnees1).' r="3" stroke="'.$stroke.'" class="graph-point-active"/>';
}
}else {
if ($min>=0) {
//Si on n'est pas sur la dernière valeur
if ($i != $lenght-1) {
$bar .= "\n\t".'<rect '.$coordonnees2.' width="'.$stepX.'" height="'.$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
} else {
$bar .= "\n\t".'<rect '.$coordonnees2.' width="'.($stepX/2).'" height="'.$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
}
$c .= "\n\t\t\t".'<circle c'.str_replace('y="', 'cy="', $coordonnees1).' r="3" stroke="'.$stroke.'" class="graph-point-active"/>';
} else {
if ($value >= 0) {
//Si on n'est pas sur la dernière valeur
if ($i != $lenght-1) {
$bar .= "\n\t".'<rect '.$coordonnees3.' width="'.$stepX.'" height="'.($stepY).'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
} else {
$bar .= "\n\t".'<rect '.$coordonnees3.' width="'.($stepX/2).'" height="'.$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
}
} else {
//Si on n'est pas sur la dernière valeur
if ($i != $lenght-1) {
$bar .= "\n\t".'<rect '.$coordonnees4.' width="'.$stepX.'" height="'.-$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
} else {
$bar .= "\n\t".'<rect '.$coordonnees4.' width="'.($stepX/2).'" height="'.-$stepY.'" class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>';
}
}
$c .= "\n\t\t\t".'<circle c'.str_replace('y="', 'cy="', $coordonnees1).' r="3" stroke="'.$stroke.'" class="graph-point-active"/>';
}
}
}
$i++;
//End of tooltips
if($tooltips == true) {
$c .= '<title class="graph-tooltip">'.(is_array($tooltipLegend) ? $tooltipLegend[$i] : $tooltipLegend).$value.'</title>'."\n\t\t".'</g>';
}
}
//Filling
if ($filled === true) {
if ($opacity == 1) {
$opacity = '1" stroke="#424242';
}
$barFilled = str_replace(' class="graph-bar" stroke="'.$stroke.'" fill="#fff" fill-opacity="0"/>', ' class="graph-bar" fill="'.$stroke.'" fill-opacity="'.$opacity.'"/>',$bar);
$return .= $barFilled;
}
$return .= $bar;
if($circles == true) {
$return .= "\n\t".'<g class="graph-point">';
$return .= $c;
$return .= "\n\t".'</g>'."\n";
}
return $return;
}
/**
* To draw pie diagrams
* @param $data array Unidimensionnal array
* @param $options array Options
* @return string Path of lines (with options)
*
* @author Cyril MAGUIRE
*/
protected function __drawDisk($data,$options=array(),$id=false) {
$options = array_merge($this->options,$options);
extract($options);
if (is_string($legends)) {
$mainLegend = $legends;
} else {
$mainLegend = null;
}
if (is_string($stroke)) {
$mainStroke = $stroke;
} else {
$mainStroke = $this->__genColor();
}
$lenght = count($data);
$max = max($data);
$total = 0;
foreach ($data as $label => $value) {
if ($value < 0) {$value = 0;}
$total += $value;
}
$deg = array();
$i = 0;
foreach ($data as $label => $value) {
if ($value < 0) {$value = 0;}
if ($total == 0) {
$deg[] = array(
'pourcent' => 0,
'val' => $value,
'label' => $label,
'tooltipLegend' => (is_array($tooltipLegend) && isset($tooltipLegend[$i])) ? $tooltipLegend[$i] : (isset($tooltipLegend) && is_string($tooltipLegend) ? $tooltipLegend : ''),
'stroke' => (is_array($stroke) && isset($stroke[$i]))? $stroke[$i] : $this->__genColor(),
);
} else {
$deg[] = array(
'pourcent' => round(((($value * 100)/$total)/100),2),
'val' => $value,
'label' => $label,
'tooltipLegend' => (is_array($tooltipLegend) && isset($tooltipLegend[$i])) ? $tooltipLegend[$i] : (isset($tooltipLegend) && is_string($tooltipLegend) ? $tooltipLegend : ''),
'stroke' => (is_array($stroke) && isset($stroke[$i]) ) ? $stroke[$i] : $this->__genColor(),
);
}
$i++;
}
if (isset($legends)) {
if (!is_array($legends) && !empty($legends) && !is_bool($legends)) {
$Legends = array();
for ($l=0;$l<$lenght;$l++) {
$Legends[$l] = array(
'label' => $deg[$l]['label'].' : '.$deg[$l]['val'],
'stroke' => $deg[$l]['stroke']
);
}
$legends = $Legends;
unset($Legends);
} elseif (empty($legends)) {
$notDisplayLegends = true;
} elseif (is_bool($legends)) {
$legends = array();
}
foreach ($deg as $k => $v) {
if (!isset($legends[$k]) || !is_array($legends[$k])) {
$legends[$k] = array(
'label' => $v['label'].' : '.$v['val'],
'stroke' => $v['stroke']
);
}
}
}
$deg = array_reverse($deg);
$heightLegends = 0;
if (isset($legends) && !empty($legends)) {
$heightLegends = count($legends)*30+2*$paddingTop+80;
} else {
$heightLegends = 2*$paddingTop+100;
}
$this->colors[] = $options['stroke'];
$originX = (2*$radius+400)/2;
$originY = 10+$titleHeight+2*$paddingTop;
//Size of canevas will be bigger than grid size to display legends
$return = "\n".'<svg width="100%" height="100%" viewBox="0 0 '.(2*$radius+400).' '.(2*$radius+100+$titleHeight+$paddingTop+$heightLegends).'" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg" version="1.1"'.($id ? ' id="'.$id.'"':'').'>'."\n";
$return .= "\n\t".'<defs>
<style type="text/css">//<![CDATA[
'.$this->css.'
]]></style>'."\n";
// $return .= "\n\t\t".'<marker id="Triangle"';
// $return .= "\n\t\t\t".'viewBox="0 0 10 10" refX="0" refY="5"';
// $return .= "\n\t\t\t".'markerUnits="strokeWidth"';
// $return .= "\n\t\t\t".'markerWidth="10" markerHeight="3"';
// $return .= "\n\t\t\t".'fill="#a1a1a1" fill-opacity="0.7"';
// $return .= "\n\t\t\t".'orient="auto">';
// $return .= "\n\t\t\t".'<path d="M 0 0 L 10 5 L 0 10 z" />';
// $return .= "\n\t\t".'</marker>';
if (is_array($gradient)) {
$id = 'BackgroundGradient'.rand();
$return .= "\n\t\t".'<linearGradient id="'.$id.'">';
$return .= "\n\t\t\t".'<stop offset="5%" stop-color="'.$gradient[0].'" />';
$return .= "\n\t\t\t".'<stop offset="95%" stop-color="'.$gradient[1].'" />';
$return .= "\n\t\t".'</linearGradient>';
$return .= "\n\t".'</defs>'."\n";
$background = 'url(#'.$id.')';
$return .= "\t".'<rect x="0" y="0" width="'.(2*$radius+400).'" height="'.(2*$radius+100+$titleHeight+$paddingTop+$heightLegends).'" class="graph-stroke" fill="'.$background.'" fill-opacity="1"/>'."\n";
} else {
$return .= "\n\t".'</defs>'."\n";
}
if (isset($title)) {
$return .= "\t".'<text x="'.($originX).'" y="'.$titleHeight.'" text-anchor="middle" class="graph-title">'.$title.'</text>'."\n";
}
$ox = $prevOriginX = $originX;
$oy = $prevOriginY = $originY;
$total = 1;
$i = 0;
while ($i <= $lenght-1) {
if ($deg[0]['val'] != 0) {
$t = (2-$deg[0]['pourcent'])/2;
$cosOrigine = cos((-90 + 360 * $t) * M_PI / 180)*$radius;
$sinOrigine = sin((-90 + 360 * $t) * M_PI / 180)*$radius;
$cosLegOrigine = cos((-90 + 360 * $t) * M_PI / 180)*(2*$radius);
$sinLegOrigine = sin((-90 + 360 * $t) * M_PI / 180)*(2*$radius);
} else {
$cosOrigine = 0;
$sinOrigine = 0;
$cosLegOrigine = 0;
$sinLegOrigine = 0;
}
if ($deg[$i]['val'] != 0) {
//Tooltips
if($tooltips == true) {
$return .= "\n\t\t".'<g class="graph-active">';
}
$color = $deg[0]['stroke'];
$return .= "\n\t\t\t".'<circle cx="'.$originX.'" cy="'.($originY+2*$radius).'" r="'.$radius.'" fill="'.$color.'" class="graph-pie"/>'."\n\t\t\t";
if ($diskLegends == true && $deg[0]['val'] != 0 ){
if ($deg[0]['pourcent'] >= 0 && $deg[0]['pourcent'] <= 0.5 || $deg[0]['pourcent'] == 1) {
$gapx = $gapy = 0;
$pathGap = 2;
}
if($deg[0]['pourcent'] > 0.5 && $deg[0]['pourcent'] < 1) {
$gapx = $gapy = 1;
$pathGap = 1;
}
$LABEL = ($diskLegendsType == 'label' ? $deg[$i]['label'] : ($diskLegendsType == 'pourcent' ? ($deg[$i]['pourcent']*100).'%' : $deg[$i]['val']));
if ($gapx == -1) {
$gapx = strlen($LABEL)*$gapx*12;
$gapy = 5;
}
$return .= "\n\t\t\t".'<path d=" M '.($originX+$cosOrigine).' '.($originY+2*$radius+$sinOrigine).' L '.($originX+$cosLegOrigine).' '.($originY + 2*$radius + $sinLegOrigine).' L '.($originX+$cosLegOrigine-30).' '.($originY + 2*$radius + $sinLegOrigine).'" class="graph-line" stroke="'.$diskLegendsLineColor.'" stroke-opacity="0.5" stroke-dasharray="2,2,2" fill="none"/>';
$return .= "\n\t\t\t".'<text x="'.($originX+$cosLegOrigine+$gapx-30*$pathGap).'" y="'.($originY+2*$radius+$sinLegOrigine+$gapy-5).'" class="graph-legend" stroke="darkgrey" stroke-opacity="0.5">'.$LABEL.'</text>'."\n\t\t\t";
}
//End tooltips
if($tooltips == true) {
$return .= '<title class="graph-tooltip">'.$deg[$i]['tooltipLegend']. $deg[$i]['label'].' : '.$deg[$i]['val'].'</title>';
$return .= "\n\t\t".'</g>';
}
//$i = $deg[$i]['label'];
break;
}
$i++;
}
// $tmp = array();
// if (is_array($legends)) {
// foreach($legends as &$ma) {
// $tmp[] = &$ma['label'];
// }
// array_multisort($tmp, $legends);
// }
foreach ($deg as $key => $value) {
$total -= $value['pourcent'];
$total2 = $total;
$cos = cos((-90 + 360 * $total) * M_PI / 180)*$radius;
$sin = sin((-90 + 360 * $total) * M_PI / 180)*$radius;
$cosLeg = cos((-90 + 360 * $total) * M_PI / 180)*(2*$radius);
$sinLeg = sin((-90 + 360 * $total) * M_PI / 180)*(2*$radius);
if (isset($deg[$key+1])) {
$total2 -= $deg[$key+1]['pourcent'];
$t = (($total - $total2)/2) + $total2;
$cos2 = cos((-90 + 360 * $t) * M_PI / 180)*$radius;
$sin2 = sin((-90 + 360 * $t) * M_PI / 180)*$radius;
$cosLeg2 = cos((-90 + 360 * $t) * M_PI / 180)*(2*$radius);
$sinLeg2 = sin((-90 + 360 * $t) * M_PI / 180)*(2*$radius);
} else {
$cos2 = 0;
$sin2 = 0;
$cosLeg2 = 0;
$sinLeg2 = 0;
}
//Tooltips
if($tooltips == true && $key < ($lenght-1)) {
$return .= "\n\t\t".'<g class="graph-active">';
}
if ($total >= 0 && $total <= 0.5 || $total == 1) {
$arc = 0;
$gapx = $gapy = 0;
$signe = 1;
$pathGap = 1;
}
if($total > 0.5 && $total < 1) {
$arc = 1;
$signe = -1;
$pathGap = 2.5;
}
$index = ($key == $lenght-1 ? 0 : $key+1);
if ($key != $lenght-1 && $deg[$index]['val'] != 0) {
$return .= "\n\t\t\t".'<path d="M '.$originX.' '.($originY + $radius).' A '.$radius.' '.$radius.' 0 '.$arc.' 1 '.($originX + $cos).' '.($originY + 2*$radius + $sin).' L '.$originX.' '.($originY+2*$radius).' z" fill="'.$deg[$index]['stroke'].'" class="graph-pie"/>'."\n\t\t\t";
}
if ($key < ($lenght-1) && $deg[$key+1]['val'] != 0 && $diskLegends == true ) {
$LABEL = ($diskLegendsType == 'label' ? $deg[$key+1]['label'] : ($diskLegendsType == 'pourcent' ? ($deg[$key+1]['pourcent']*100).'%' : $deg[$key+1]['val']));
if ($arc == 1) {
$gapx = strlen($LABEL)*$gapx*12;
$gapy = 5;
}
$return .= "\n\t\t\t".'<path d=" M '.($originX+$cos2).' '.($originY+2*$radius+$sin2).' L '.($originX + $cosLeg2).' '.($originY + 2*$radius + $sinLeg2).' L '.($originX+$cosLeg2+$signe*30).' '.($originY + 2*$radius + $sinLeg2).'" fill="none" class="graph-line" stroke="'.$diskLegendsLineColor.'" stroke-opacity="0.5" stroke-dasharray="2,2,2"/>';
$return .= "\n\t\t\t".'<text x="'.($originX + $cosLeg2 + $gapx + $signe*30*$pathGap).'" y="'.($originY + 2*$radius + $sinLeg2 + $gapy).'" class="graph-legend" stroke="darkgrey" stroke-opacity="0.5"> '.$LABEL.'</text>'."\n\t\t\t";
}
//End tooltips
if($tooltips == true && $key < ($lenght-1)) {
$return .= '<title class="graph-tooltip">'.$deg[$key+1]['tooltipLegend'].$deg[$key+1]['label'].' : '.$deg[$key+1]['val'].'</title>'."\n\t\t".'</g>';
}
}
if ($mainLegend) {
$return .= '<rect x="50" y="'.(4*$radius+$titleHeight+$paddingTop+(2*$paddingTop)+30).'" width="10" height="10" fill="'.$mainStroke.'" class="graph-legend-stroke"/>
<text x="70" y="'.(4*$radius+$titleHeight+$paddingTop+(2*$paddingTop)+40).'" class="graph-legend">'.$mainLegend.'</text>';
$paddingLegendY += 70;
}
if (isset($legends) && !empty($legends) && !isset($notDisplayLegends)) {
$leg = "\t".'<g class="graph-legends">';
foreach ($legends as $key => $value) {
$colorToFillWith = ($key == 0 ? $value['stroke'] : $deg[$lenght-$key-1]['stroke']);
$leg .= "\n\t\t".'<rect x="70" y="'.(4*$radius+$titleHeight+$paddingTop+$paddingLegendY+$key*(2*$paddingTop)).'" width="10" height="10" fill="'.$colorToFillWith.'" class="graph-legend-stroke"/>';
$leg .= "\n\t\t".'<text x="90" y="'.(4*$radius+$titleHeight+$paddingTop+$paddingLegendY+10+$key*(2*$paddingTop)).'" text-anchor="start" class="graph-legend">'.$value['label'].'</text>';
}
$leg .= "\n\t".'</g>';
$return .= $leg;
}
if ($type == 'ring' || isset($subtype)) {
$return .= '<circle cx="'.$originX.'" cy="'.($originY+2*$radius).'" r="'.($radius/2).'" fill="'.$background.'" class="graph-pie"/>';
}
return $return;
}
/**
* To draw vertical stock chart
* @param $data array Array with structure equal to array('index'=> array('open'=>val,'close'=>val,'min'=>val,'max'=>val))
* @param $height integer Height of grid
* @param $HEIGHT integer Height of grid + title + padding top
* @param $stepX integer Distance between two graduations on x-axis
* @param $unitY integer Unit of y-axis
* @param $lenght integer Number of graduations on x-axis
* @param $min integer Minimum value of data
* @param $max integer Maximum value of data
* @param $options array Options
* @param $i integer index of current data
* @param $labels array labels of x-axis
* @param $id integer index of plotLimit
* @return string Path of lines (with options)
*