Skip to content

Commit f18c7ad

Browse files
author
ghost
committed
initial commit
1 parent 18930f7 commit f18c7ad

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.vscode/
2+
/vendor/
3+
4+
composer.lock

composer.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "yggverse/graph",
3+
"description": "PHP library to build JS-less graphs",
4+
"type": "library",
5+
"require": {
6+
"php": ">=8.1"
7+
},
8+
"license": "MIT",
9+
"autoload": {
10+
"psr-4": {
11+
"Yggverse\\Graph\\": "src/"
12+
}
13+
},
14+
"minimum-stability": "alpha"
15+
}

src/Calendar/Month.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)