From debc30578985970ff952c7d53cf13cc5b80fd2aa Mon Sep 17 00:00:00 2001 From: Matthias Jentsch Date: Tue, 2 Dec 2014 17:24:34 +0100 Subject: [PATCH] Add logarithmic AxisUnit resolves #7845 --- library/Icinga/Chart/Unit/LogarithmicUnit.php | 264 ++++++++++++++++++ .../controllers/ChartController.php | 99 ++++++- .../views/scripts/chart/test.phtml | 11 +- 3 files changed, 366 insertions(+), 8 deletions(-) create mode 100644 library/Icinga/Chart/Unit/LogarithmicUnit.php diff --git a/library/Icinga/Chart/Unit/LogarithmicUnit.php b/library/Icinga/Chart/Unit/LogarithmicUnit.php new file mode 100644 index 000000000..44580b15e --- /dev/null +++ b/library/Icinga/Chart/Unit/LogarithmicUnit.php @@ -0,0 +1,264 @@ + + * this article 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 self 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 '10E' . $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 + */ + protected function getTicks() + { + return $this->maxExp - $this->minExp; + } +} diff --git a/modules/monitoring/application/controllers/ChartController.php b/modules/monitoring/application/controllers/ChartController.php index 2024f83f4..640ee2520 100644 --- a/modules/monitoring/application/controllers/ChartController.php +++ b/modules/monitoring/application/controllers/ChartController.php @@ -6,6 +6,8 @@ use Icinga\Module\Monitoring\Controller; use Icinga\Chart\GridChart; use Icinga\Chart\PieChart; use Icinga\Chart\Unit\StaticAxis; +use Icinga\Chart\Unit\LogarithmicUnit; +use Icinga\Chart\Unit\LinearUnit; /** * Class Monitoring_CommandController @@ -20,6 +22,95 @@ class Monitoring_ChartController extends Controller $this->view->compact = $this->_request->getParam('view') === 'compact'; } + private function drawLogChart1() + { + $chart = new GridChart(); + $chart->alignTopLeft(); + $chart->setAxisLabel('X axis label', 'Y axis label') + ->setYAxis(new LogarithmicUnit()); + + for ($i = -15; $i < 15; $i++) { + $data1[] = array($i, -$i * rand(1, 10) * pow(2, rand(1, 2))); + } + for ($i = -15; $i < 15; $i++) { + $data2[] = array($i, 1000 + $i * rand(1, 35) * pow(2, rand(1, 2))); + } + for ($i = -15; $i < 15; $i++) { + $data3[] = array($i, $i * rand(1, 100) * pow(2, rand(1, 10)) - 1000); + } + + $chart->drawLines( + array( + 'label' => 'Random 1', + 'color' => 'red', + 'data' => $data1, + 'showPoints' => true + ) + ); + $chart->drawLines( + array( + 'label' => 'Random 2', + 'color' => 'blue', + 'data' => $data2, + 'showPoints' => true + ) + ); + $chart->drawLines( + array( + 'label' => 'Random 3', + 'color' => 'green', + 'data' => $data3, + 'showPoints' => true + ) + ); + return $chart; + } + + private function drawLogChart2() + { + $chart = new GridChart(); + $chart->alignTopLeft(); + $chart->setAxisLabel('X axis label', 'Y axis label') + ->setYAxis(new LogarithmicUnit()); + + for ($i = -10; $i < 10; $i++) { + $sign = $i > 0 ? 1 : + ($i < 0 ? -1 : 0); + $data[] = array($i, $sign * pow(10, abs($i))); + } + $chart->drawLines( + array( + 'label' => 'f(x): sign(x) * 10^|x|', + 'color' => 'red', + 'data' => $data, + 'showPoints' => true + ) + ); + return $chart; + } + private function drawLogChart3() + { + $chart = new GridChart(); + $chart->alignTopLeft(); + $chart->setAxisLabel('X axis label', 'Y axis label') + ->setYAxis(new LogarithmicUnit()); + + for ($i = -2; $i < 3; $i++) { + $sign = $i > 0 ? 1 : + ($i < 0 ? -1 : 0); + $data[] = array($i, $sign * pow(10, abs($i))); + } + $chart->drawLines( + array( + 'label' => 'f(x): sign(x) * 10^|x|', + 'color' => 'red', + 'data' => $data, + 'showPoints' => true + ) + ); + return $chart; + } + public function testAction() { $this->chart = new GridChart(); @@ -52,7 +143,7 @@ class Monitoring_ChartController extends Controller */ $this->chart->drawBars( array( - 'label' => 'Some other line', + 'label' => 'A big amount of data', 'color' => 'green', 'data' => $data3, 'showPoints' => true @@ -68,7 +159,11 @@ class Monitoring_ChartController extends Controller ) ); */ - $this->view->svg = $this->chart; + $this->view->svgs = array(); + $this->view->svgs[] = $this->drawLogChart1(); + $this->view->svgs[] = $this->drawLogChart2(); + $this->view->svgs[] = $this->drawLogChart3(); + $this->view->svgs[] = $this->chart; } public function hostgroupAction() diff --git a/modules/monitoring/application/views/scripts/chart/test.phtml b/modules/monitoring/application/views/scripts/chart/test.phtml index 1c2046bdf..214f44d33 100644 --- a/modules/monitoring/application/views/scripts/chart/test.phtml +++ b/modules/monitoring/application/views/scripts/chart/test.phtml @@ -1,6 +1,5 @@ -mah -
-render(); -?> -
\ No newline at end of file + +
+ render() ?> +
+ \ No newline at end of file