1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace YGGverse \Chart \Calendar ;
6
+
7
+ class Month
8
+ {
9
+ private $ _time ;
10
+ private $ _node = [];
11
+
12
+ public function __construct (int $ time = null , int $ calendar = CAL_GREGORIAN )
13
+ {
14
+ // Set timestamp
15
+ $ this ->_time = $ time ? $ time : time ();
16
+
17
+ // Generate calendar days
18
+ for ($ day = 1 ; $ day <= cal_days_in_month ($ calendar , (int ) date ('n ' , $ this ->_time ), (int ) date ('Y ' , $ this ->_time )); $ day ++)
19
+ {
20
+ $ this ->_node [$ day ][0 ] = [];
21
+ }
22
+ }
23
+
24
+ public function addNode (int $ day , int $ value , string $ label = null , string $ class = null , int $ layer = 0 )
25
+ {
26
+ $ this ->_node [$ day ][$ layer ][] = [
27
+ 'value ' => $ value ,
28
+ 'label ' => $ label ,
29
+ 'class ' => $ class ,
30
+ 'width ' => 0 ,
31
+ 'height ' => 0 ,
32
+ 'offset ' => 0 ,
33
+ ];
34
+ }
35
+
36
+ public function getNodes () : object
37
+ {
38
+ // Calculate month totals
39
+ $ total = [];
40
+
41
+ foreach ($ this ->_node as $ i => $ day )
42
+ {
43
+ foreach ($ day as $ l => $ layer )
44
+ {
45
+ $ total [$ i ][$ l ] = 0 ;
46
+
47
+ foreach ($ layer as $ data )
48
+ {
49
+ $ total [$ i ][$ l ] += $ data ['value ' ];
50
+ }
51
+ }
52
+ }
53
+
54
+ // Calculate dimensions
55
+ foreach ($ this ->_node as $ i => $ day )
56
+ {
57
+ foreach ($ day as $ l => $ layer )
58
+ {
59
+ // Count data values in layer
60
+ $ count = 0 ;
61
+ foreach ($ layer as $ data ) $ count ++;
62
+
63
+ // Calculate column width
64
+ $ width = $ count ? 100 / $ count : 0 ;
65
+
66
+ // Calculate column width, height, offset
67
+ foreach ($ layer as $ j => $ data )
68
+ {
69
+ $ this ->_node [$ i ][$ l ][$ j ]['width ' ] = $ width ;
70
+ $ this ->_node [$ i ][$ l ][$ j ]['height ' ] = $ total [$ i ][$ l ] ? ceil ($ data ['value ' ] / $ total [$ i ][$ l ] * 100 ) : 0 ;
71
+ $ this ->_node [$ i ][$ l ][$ j ]['offset ' ] = $ width * $ j ;
72
+ }
73
+ }
74
+ }
75
+
76
+ // Return object
77
+ return json_decode (json_encode ($ this ->_node ));
78
+ }
79
+ }
0 commit comments