summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Chart/Unit
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:39:39 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:39:39 +0000
commit8ca6cc32b2c789a3149861159ad258f2cb9491e3 (patch)
tree2492de6f1528dd44eaa169a5c1555026d9cb75ec /library/Icinga/Chart/Unit
parentInitial commit. (diff)
downloadicingaweb2-8ca6cc32b2c789a3149861159ad258f2cb9491e3.tar.xz
icingaweb2-8ca6cc32b2c789a3149861159ad258f2cb9491e3.zip
Adding upstream version 2.11.4.upstream/2.11.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--library/Icinga/Chart/Unit/AxisUnit.php56
-rw-r--r--library/Icinga/Chart/Unit/CalendarUnit.php167
-rw-r--r--library/Icinga/Chart/Unit/LinearUnit.php227
-rw-r--r--library/Icinga/Chart/Unit/LogarithmicUnit.php263
-rw-r--r--library/Icinga/Chart/Unit/StaticAxis.php129
5 files changed, 842 insertions, 0 deletions
diff --git a/library/Icinga/Chart/Unit/AxisUnit.php b/library/Icinga/Chart/Unit/AxisUnit.php
new file mode 100644
index 0000000..251787f
--- /dev/null
+++ b/library/Icinga/Chart/Unit/AxisUnit.php
@@ -0,0 +1,56 @@
+<?php
+/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Chart\Unit;
+
+use Iterator;
+
+/**
+ * Base class for Axis Units
+ *
+ * An AxisUnit takes a set of values and places them on a given range
+ *
+ * Concrete subclasses must implement the iterator interface, with
+ * getCurrent returning the axis relative position and getValue the label
+ * that will be displayed
+ */
+interface AxisUnit extends Iterator
+{
+ /**
+ * Add a dataset to this AxisUnit, required for dynamic min and max vlaues
+ *
+ * @param array $dataset The dataset that will be shown in the Axis
+ * @param int $id The idx in the dataset (0 for x, 1 for y)
+ */
+ public function addValues(array $dataset, $id = 0);
+
+ /**
+ * Transform the given absolute value in an axis relative value
+ *
+ * @param int $value The absolute, dataset dependent value
+ *
+ * @return int An axis relative value
+ */
+ public function transform($value);
+
+ /**
+ * Set the axis minimum value to a fixed value
+ *
+ * @param int $min The new minimum value
+ */
+ public function setMin($min);
+
+ /**
+ * Set the axis maximum value to a fixed value
+ *
+ * @param int $max The new maximum value
+ */
+ public function setMax($max);
+
+ /**
+ * Get the amount of ticks of this axis
+ *
+ * @return int
+ */
+ public function getTicks();
+}
diff --git a/library/Icinga/Chart/Unit/CalendarUnit.php b/library/Icinga/Chart/Unit/CalendarUnit.php
new file mode 100644
index 0000000..7ce6a99
--- /dev/null
+++ b/library/Icinga/Chart/Unit/CalendarUnit.php
@@ -0,0 +1,167 @@
+<?php
+/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
+
+
+namespace Icinga\Chart\Unit;
+
+use DateTime;
+
+/**
+ * Calendar Axis Unit that transforms timestamps into user-readable values
+ *
+ */
+class CalendarUnit extends LinearUnit
+{
+ /**
+ * Constant for a minute
+ */
+ const MINUTE = 60;
+
+ /**
+ * Constant for an hour
+ */
+ const HOUR = 3600;
+
+ /**
+ * Constant for a day
+ */
+ const DAY = 864000;
+
+ /**
+ * Constant for ~a month
+ * 30 Days, this is sufficient for our needs
+ */
+ const MONTH = 2592000; // x
+
+ /**
+ * An array containing all labels that will be displayed
+ *
+ * @var array
+ */
+ private $labels = array();
+
+ /**
+ * The date format to use
+ *
+ * @var string
+ */
+ private $dateFormat = 'd-m';
+
+ /**
+ * The time format to use
+ *
+ * @var string
+ */
+ private $timeFormat = 'g:i:s';
+
+ /**
+ * Create the labels for the given dataset
+ */
+ private function createLabels()
+ {
+ $this->labels = array();
+ $duration = $this->getMax() - $this->getMin();
+
+ if ($duration <= self::HOUR) {
+ $unit = self::MINUTE;
+ } elseif ($duration <= self::DAY) {
+ $unit = self::HOUR;
+ } elseif ($duration <= self::MONTH) {
+ $unit = self::DAY;
+ } else {
+ $unit = self::MONTH;
+ }
+ $this->calculateLabels($unit);
+ }
+
+ /**
+ * Calculate the labels for this dataset
+ *
+ * @param integer $unit The unit to use as the basis for calculation
+ */
+ private function calculateLabels($unit)
+ {
+ $fac = new DateTime();
+
+ $duration = $this->getMax() - $this->getMin();
+
+ // Calculate number of ticks, but not more than 30
+ $tickCount = ($duration/$unit * 10);
+ if ($tickCount > 30) {
+ $tickCount = 30;
+ }
+
+ $step = $duration / $tickCount;
+ $format = $this->timeFormat;
+ if ($unit === self::DAY) {
+ $format = $this->dateFormat;
+ } elseif ($unit === self::MONTH) {
+ $format = $this->dateFormat;
+ }
+
+ for ($i = 0; $i <= $duration; $i += $step) {
+ $this->labels[] = $fac->setTimestamp($this->getMin() + $i)->format($format);
+ }
+ }
+
+ /**
+ * Add a dataset to this CalendarUnit and update labels
+ *
+ * @param array $dataset The dataset to update
+ * @param int $idx The index to use for determining the data
+ *
+ * @return $this Fluid interface
+ */
+ public function addValues(array $dataset, $idx = 0)
+ {
+ parent::addValues($dataset, $idx);
+ $this->createLabels();
+ return $this;
+ }
+
+ /**
+ * Return the current axis relative position
+ *
+ * @return int The position of the next tick (between 0 and 100)
+ */
+ public function current()
+ {
+ return 100 * (key($this->labels) / count($this->labels));
+ }
+
+ /**
+ * Move to next tick
+ */
+ public function next()
+ {
+ next($this->labels);
+ }
+
+ /**
+ * Return the current tick caption
+ *
+ * @return string
+ */
+ public function key()
+ {
+ return current($this->labels);
+ }
+
+ /**
+ * Return true when the iterator is in a valid range
+ *
+ * @return bool
+ */
+ public function valid()
+ {
+ return current($this->labels) !== false;
+ }
+
+ /**
+ * Rewind the internal array
+ */
+ public function rewind()
+ {
+ reset($this->labels);
+ }
+}
diff --git a/library/Icinga/Chart/Unit/LinearUnit.php b/library/Icinga/Chart/Unit/LinearUnit.php
new file mode 100644
index 0000000..ea4792b
--- /dev/null
+++ b/library/Icinga/Chart/Unit/LinearUnit.php
@@ -0,0 +1,227 @@
+<?php
+/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Chart\Unit;
+
+/**
+ * Linear tick distribution over the axis
+ */
+class LinearUnit implements AxisUnit
+{
+ /**
+ * The minimum value to display
+ *
+ * @var int
+ */
+ protected $min;
+
+ /**
+ * The maximum value to display
+ *
+ * @var int
+ */
+ protected $max;
+
+ /**
+ * True when the minimum value is static and isn't affected by the dataset
+ *
+ * @var bool
+ */
+ protected $staticMin = false;
+
+ /**
+ * True when the maximum value is static and isn't affected by the dataset
+ *
+ * @var bool
+ */
+ protected $staticMax = false;
+
+ /**
+ * The number of ticks to use
+ *
+ * @var int
+ */
+ protected $nrOfTicks = 10;
+
+ /**
+ * The currently displayed tick
+ *
+ * @var int
+ */
+ protected $currentTick = 0;
+
+ /**
+ * The currently displayed value
+ * @var int
+ */
+ protected $currentValue = 0;
+
+ /**
+ * Create and initialize this AxisUnit
+ *
+ * @param int $nrOfTicks The number of ticks to use
+ */
+ public function __construct($nrOfTicks = 10)
+ {
+ $this->min = PHP_INT_MAX;
+ $this->max = ~PHP_INT_MAX;
+ $this->nrOfTicks = $nrOfTicks;
+ }
+
+ /**
+ * Add a dataset and calculate the minimum and maximum value for this AxisUnit
+ *
+ * @param array $dataset The dataset to add
+ * @param int $idx The idx (0 for x, 1 for y)
+ *
+ * @return $this Fluent interface
+ */
+ public function addValues(array $dataset, $idx = 0)
+ {
+ $datapoints = array();
+
+ foreach ($dataset['data'] as $points) {
+ $datapoints[] = $points[$idx];
+ }
+ if (empty($datapoints)) {
+ return $this;
+ }
+ sort($datapoints);
+ if (!$this->staticMax) {
+ $this->max = max($this->max, $datapoints[count($datapoints) - 1]);
+ }
+ if (!$this->staticMin) {
+ $this->min = min($this->min, $datapoints[0]);
+ }
+ $this->currentTick = 0;
+ $this->currentValue = $this->min;
+ if ($this->max === $this->min) {
+ $this->max = $this->min + 10;
+ }
+ $this->nrOfTicks = $this->max - $this->min;
+ return $this;
+ }
+
+ /**
+ * Transform the absolute value to an axis relative value
+ *
+ * @param int $value The absolute coordinate from the dataset
+ * @return float|int The axis relative coordinate (between 0 and 100)
+ */
+ public function transform($value)
+ {
+ if ($value < $this->min) {
+ return 0;
+ } elseif ($value > $this->max) {
+ return 100;
+ } else {
+ return 100 * ($value - $this->min) / $this->nrOfTicks;
+ }
+ }
+
+ /**
+ * Return the position of the current tick
+ *
+ * @return int
+ */
+ #[\ReturnTypeWillChange]
+ public function current()
+ {
+ return $this->currentTick;
+ }
+
+ /**
+ * Calculate the next tick and tick value
+ */
+ public function next(): void
+ {
+ $this->currentTick += (100 / $this->nrOfTicks);
+ $this->currentValue += (($this->max - $this->min) / $this->nrOfTicks);
+ }
+
+ /**
+ * Return the label for the current tick
+ *
+ * @return string The label for the current tick
+ */
+ #[\ReturnTypeWillChange]
+ public function key()
+ {
+ return (string) intval($this->currentValue);
+ }
+
+ /**
+ * True when we're at a valid tick (iterator interface)
+ *
+ * @return bool
+ */
+ public function valid(): bool
+ {
+ return $this->currentTick >= 0 && $this->currentTick <= 100;
+ }
+
+ /**
+ * Reset the current tick and label value
+ */
+ public function rewind(): void
+ {
+ $this->currentTick = 0;
+ $this->currentValue = $this->min;
+ }
+
+ /**
+ * Set the axis maximum value to a fixed value
+ *
+ * @param int $max The new maximum value
+ */
+ public function setMax($max)
+ {
+ if ($max !== null) {
+ $this->max = $max;
+ $this->staticMax = true;
+ }
+ }
+
+ /**
+ * Set the axis minimum value to a fixed value
+ *
+ * @param int $min The new minimum value
+ */
+ public function setMin($min)
+ {
+ if ($min !== null) {
+ $this->min = $min;
+ $this->staticMin = true;
+ }
+ }
+
+ /**
+ * Return the current minimum value of the axis
+ *
+ * @return int The minimum set for this axis
+ */
+ public function getMin()
+ {
+ return $this->min;
+ }
+
+ /**
+ * Return the current maximum value of the axis
+ *
+ * @return int The maximum set for this axis
+ */
+ public function getMax()
+ {
+ return $this->max;
+ }
+
+ /**
+ * Get the amount of ticks necessary to display this AxisUnit
+ *
+ * @return int
+ */
+ public function getTicks()
+ {
+ return $this->nrOfTicks;
+ }
+}
diff --git a/library/Icinga/Chart/Unit/LogarithmicUnit.php b/library/Icinga/Chart/Unit/LogarithmicUnit.php
new file mode 100644
index 0000000..90cad57
--- /dev/null
+++ b/library/Icinga/Chart/Unit/LogarithmicUnit.php
@@ -0,0 +1,263 @@
+<?php
+/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Chart\Unit;
+
+/**
+ * Logarithmic tick distribution over the axis
+ *
+ * This class does not use the actual logarithm, but a slightly altered version called the
+ * Log-Modulo transformation. This is necessary, since a regular logarithmic scale is not able to display negative
+ * values and zero-points. See <a href="http://blogs.sas.com/content/iml/2014/07/14/log-transformation-of-pos-neg>
+ * this article </a> for a more detailed description.
+ */
+class LogarithmicUnit implements AxisUnit
+{
+ /**
+ * @var int
+ */
+ protected $base;
+
+ /**
+ * @var
+ */
+ protected $currentTick;
+
+ /**
+ * @var
+ */
+ protected $minExp;
+
+ /**
+ * @var
+ */
+ protected $maxExp;
+
+ /**
+ * True when the minimum value is static and isn't affected by the data set
+ *
+ * @var bool
+ */
+ protected $staticMin = false;
+
+ /**
+ * True when the maximum value is static and isn't affected by the data set
+ *
+ * @var bool
+ */
+ protected $staticMax = false;
+
+ /**
+ * Create and initialize this AxisUnit
+ *
+ * @param int $nrOfTicks The number of ticks to use
+ */
+ public function __construct($base = 10)
+ {
+ $this->base = $base;
+ $this->minExp = PHP_INT_MAX;
+ $this->maxExp = ~PHP_INT_MAX;
+ }
+
+ /**
+ * Add a dataset and calculate the minimum and maximum value for this AxisUnit
+ *
+ * @param array $dataset The dataset to add
+ * @param int $idx The idx (0 for x, 1 for y)
+ *
+ * @return $this Fluent interface
+ */
+ public function addValues(array $dataset, $idx = 0)
+ {
+ $datapoints = array();
+
+ foreach ($dataset['data'] as $points) {
+ $datapoints[] = $points[$idx];
+ }
+ if (empty($datapoints)) {
+ return $this;
+ }
+ sort($datapoints);
+ if (!$this->staticMax) {
+ $this->maxExp = max($this->maxExp, $this->logCeil($datapoints[count($datapoints) - 1]));
+ }
+ if (!$this->staticMin) {
+ $this->minExp = min($this->minExp, $this->logFloor($datapoints[0]));
+ }
+ $this->currentTick = 0;
+
+ return $this;
+ }
+
+ /**
+ * Transform the absolute value to an axis relative value
+ *
+ * @param int $value The absolute coordinate from the data set
+ * @return float|int The axis relative coordinate (between 0 and 100)
+ */
+ public function transform($value)
+ {
+ if ($value < $this->pow($this->minExp)) {
+ return 0;
+ } elseif ($value > $this->pow($this->maxExp)) {
+ return 100;
+ } else {
+ return 100 * ($this->log($value) - $this->minExp) / $this->getTicks();
+ }
+ }
+
+ /**
+ * Return the position of the current tick
+ *
+ * @return int
+ */
+ public function current()
+ {
+ return $this->currentTick * (100 / $this->getTicks());
+ }
+
+ /**
+ * Calculate the next tick and tick value
+ */
+ public function next()
+ {
+ ++ $this->currentTick;
+ }
+
+ /**
+ * Return the label for the current tick
+ *
+ * @return string The label for the current tick
+ */
+ public function key()
+ {
+ $currentBase = $this->currentTick + $this->minExp;
+ if (abs($currentBase) > 4) {
+ return $this->base . 'E' . $currentBase;
+ }
+ return (string) intval($this->pow($currentBase));
+ }
+
+ /**
+ * True when we're at a valid tick (iterator interface)
+ *
+ * @return bool
+ */
+ public function valid()
+ {
+ return $this->currentTick >= 0 && $this->currentTick < $this->getTicks();
+ }
+
+ /**
+ * Reset the current tick and label value
+ */
+ public function rewind()
+ {
+ $this->currentTick = 0;
+ }
+
+ /**
+ * Perform a log-modulo transformation
+ *
+ * @param $value The value to transform
+ *
+ * @return double The transformed value
+ */
+ protected function log($value)
+ {
+ $sign = $value > 0 ? 1 : -1;
+ return $sign * log1p($sign * $value) / log($this->base);
+ }
+
+ /**
+ * Calculate the biggest exponent necessary to display the given data point
+ *
+ * @param $value
+ *
+ * @return float
+ */
+ protected function logCeil($value)
+ {
+ return ceil($this->log($value)) + 1;
+ }
+
+ /**
+ * Calculate the smallest exponent necessary to display the given data point
+ *
+ * @param $value
+ *
+ * @return float
+ */
+ protected function logFloor($value)
+ {
+ return floor($this->log($value));
+ }
+
+ /**
+ * Inverse function to the log-modulo transformation
+ *
+ * @param $value
+ *
+ * @return double
+ */
+ protected function pow($value)
+ {
+ if ($value == 0) {
+ return 0;
+ }
+ $sign = $value > 0 ? 1 : -1;
+ return $sign * (pow($this->base, $sign * $value));
+ }
+
+ /**
+ * Set the axis minimum value to a fixed value
+ *
+ * @param int $min The new minimum value
+ */
+ public function setMin($min)
+ {
+ $this->minExp = $this->logFloor($min);
+ $this->staticMin = true;
+ }
+
+ /**
+ * Set the axis maximum value to a fixed value
+ *
+ * @param int $max The new maximum value
+ */
+ public function setMax($max)
+ {
+ $this->maxExp = $this->logCeil($max);
+ $this->staticMax = true;
+ }
+
+ /**
+ * Return the current minimum value of the axis
+ *
+ * @return int The minimum set for this axis
+ */
+ public function getMin()
+ {
+ return $this->pow($this->minExp);
+ }
+
+ /**
+ * Return the current maximum value of the axis
+ *
+ * @return int The maximum set for this axis
+ */
+ public function getMax()
+ {
+ return $this->pow($this->maxExp);
+ }
+
+ /**
+ * Get the amount of ticks necessary to display this AxisUnit
+ *
+ * @return int
+ */
+ public function getTicks()
+ {
+ return $this->maxExp - $this->minExp;
+ }
+}
diff --git a/library/Icinga/Chart/Unit/StaticAxis.php b/library/Icinga/Chart/Unit/StaticAxis.php
new file mode 100644
index 0000000..d563091
--- /dev/null
+++ b/library/Icinga/Chart/Unit/StaticAxis.php
@@ -0,0 +1,129 @@
+<?php
+/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
+
+
+namespace Icinga\Chart\Unit;
+
+class StaticAxis implements AxisUnit
+{
+ private $items = array();
+
+ /**
+ * Add a dataset to this AxisUnit, required for dynamic min and max values
+ *
+ * @param array $dataset The dataset that will be shown in the Axis
+ * @param int $idx The idx in the dataset (0 for x, 1 for y)
+ *
+ * @return $this Fluent interface
+ */
+ public function addValues(array $dataset, $idx = 0)
+ {
+ $datapoints = array();
+ foreach ($dataset['data'] as $points) {
+ $this->items[] = $points[$idx];
+ }
+ $this->items = array_unique($this->items);
+
+ return $this;
+ }
+
+ /**
+ * Transform the given absolute value in an axis relative value
+ *
+ * @param int $value The absolute, dataset dependent value
+ *
+ * @return int An axis relative value
+ */
+ public function transform($value)
+ {
+ $flipped = array_flip($this->items);
+ if (!isset($flipped[$value])) {
+ return 0;
+ }
+ $pos = $flipped[$value];
+ return 1 + (99 / count($this->items) * $pos);
+ }
+ /**
+ * Set the axis minimum value to a fixed value
+ *
+ * @param int $min The new minimum value
+ */
+ public function setMin($min)
+ {
+ }
+
+ /**
+ * Set the axis maximum value to a fixed value
+ *
+ * @param int $max The new maximum value
+ */
+ public function setMax($max)
+ {
+ }
+
+ /**
+ * (PHP 5 &gt;= 5.0.0)<br/>
+ * Return the current element
+ * @link http://php.net/manual/en/iterator.current.php
+ * @return mixed Can return any type.
+ */
+ public function current()
+ {
+ return 1 + (99 / count($this->items) * key($this->items));
+ }
+
+ /**
+ * (PHP 5 &gt;= 5.0.0)<br/>
+ * Move forward to next element
+ * @link http://php.net/manual/en/iterator.next.php
+ * @return void Any returned value is ignored.
+ */
+ public function next()
+ {
+ return next($this->items);
+ }
+
+ /**
+ * (PHP 5 &gt;= 5.0.0)<br/>
+ * Return the key of the current element
+ * @link http://php.net/manual/en/iterator.key.php
+ * @return mixed scalar on success, or null on failure.
+ */
+ public function key()
+ {
+ return current($this->items);
+ }
+
+ /**
+ * (PHP 5 &gt;= 5.0.0)<br/>
+ * Checks if current position is valid
+ * @link http://php.net/manual/en/iterator.valid.php
+ * @return boolean The return value will be casted to boolean and then evaluated.
+ * Returns true on success or false on failure.
+ */
+ public function valid()
+ {
+ return current($this->items) !== false;
+ }
+
+ /**
+ * (PHP 5 &gt;= 5.0.0)<br/>
+ * Rewind the Iterator to the first element
+ * @link http://php.net/manual/en/iterator.rewind.php
+ * @return void Any returned value is ignored.
+ */
+ public function rewind()
+ {
+ return reset($this->items);
+ }
+
+ /**
+ * Get the amount of ticks of this axis
+ *
+ * @return int
+ */
+ public function getTicks()
+ {
+ return count($this->items);
+ }
+}