-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonth.php
80 lines (68 loc) · 1.79 KB
/
Month.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
<?php
declare(strict_types=1);
namespace Yggverse\Graph\Calendar;
class Month
{
private $_time;
private $_node = [];
public function __construct(int $time = null, int $calendar = CAL_GREGORIAN)
{
// Set timestamp
$this->_time = $time ? $time : time();
// Generate calendar days
for ($day = 1; $day <= cal_days_in_month($calendar, (int) date('n', $this->_time), (int) date('Y', $this->_time)); $day++)
{
$this->_node[$day][0] = [];
}
}
public function addNode(int $day, int $value, string $label = null, string $class = null, int $layer = 0)
{
$this->_node[$day][$layer][] = [
'value' => $value,
'label' => $label,
'class' => $class,
'width' => 0,
'height' => 0,
'offset' => 0,
];
}
public function getNodes() : object
{
// Calculate max value
$max = 0;
foreach ($this->_node as $i => $day)
{
foreach ($day as $l => $layer)
{
foreach ($layer as $data)
{
if ($data['value'] > $max)
{
$max = $data['value'];
}
}
}
}
// Calculate dimensions
foreach ($this->_node as $i => $day)
{
foreach ($day as $l => $layer)
{
// Count data values in layer
$count = 0;
foreach ($layer as $data) $count++;
// Calculate column width
$width = $count ? 100 / $count : 0;
// Calculate column width, height, offset
foreach ($layer as $j => $data)
{
$this->_node[$i][$l][$j]['height'] = $max ? ceil($data['value'] / $max * 100) : 0;
$this->_node[$i][$l][$j]['width'] = $width;
$this->_node[$i][$l][$j]['offset'] = $width * $j;
}
}
}
// Return object
return json_decode(json_encode($this->_node));
}
}