diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4329e7b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.vscode/ +/vendor/ + +composer.lock diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..7e6a97a --- /dev/null +++ b/composer.json @@ -0,0 +1,15 @@ +{ + "name": "yggverse/graph", + "description": "PHP library to build JS-less graphs", + "type": "library", + "require": { + "php": ">=8.1" + }, + "license": "MIT", + "autoload": { + "psr-4": { + "Yggverse\\Graph\\": "src/" + } + }, + "minimum-stability": "alpha" +} diff --git a/src/Calendar/Month.php b/src/Calendar/Month.php new file mode 100644 index 0000000..3e51c65 --- /dev/null +++ b/src/Calendar/Month.php @@ -0,0 +1,79 @@ +_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 month totals + $total = []; + + foreach ($this->_node as $i => $day) + { + foreach ($day as $l => $layer) + { + $total[$i][$l] = 0; + + foreach ($layer as $data) + { + $total[$i][$l] += $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]['width'] = $width; + $this->_node[$i][$l][$j]['height'] = $total[$i][$l] ? ceil($data['value'] / $total[$i][$l] * 100) : 0; + $this->_node[$i][$l][$j]['offset'] = $width * $j; + } + } + } + + // Return object + return json_decode(json_encode($this->_node)); + } +} \ No newline at end of file