From 0a500efd8a3375ac4c02a80a181cc6bc53a25e11 Mon Sep 17 00:00:00 2001 From: Matthias Jentsch Date: Mon, 21 Jul 2014 17:17:09 +0200 Subject: [PATCH] Determine the max value in LinearUnit dynamically The range between min and max should always be divisable by the amount of ticks, to ensure that the vertical lines are always at a full discrete value. fixes #6769 --- library/Icinga/Chart/Unit/LinearUnit.php | 43 +++++++++++++++++-- .../controllers/ChartController.php | 7 ++- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/library/Icinga/Chart/Unit/LinearUnit.php b/library/Icinga/Chart/Unit/LinearUnit.php index bf27486d2..d776fe304 100644 --- a/library/Icinga/Chart/Unit/LinearUnit.php +++ b/library/Icinga/Chart/Unit/LinearUnit.php @@ -90,17 +90,52 @@ class LinearUnit implements AxisUnit } sort($datapoints); if (!$this->staticMax) { - $this->max = max($this->max, $datapoints[count($datapoints)-1]); + $this->max = max($this->max, $datapoints[count($datapoints) - 1]); } if (!$this->staticMin) { $this->min = min($this->min, $datapoints[0]); } - + if (!$this->staticMin || !$this->staticMax) { + $this->updateMaxValue(); + } $this->currentTick = 0; $this->currentValue = $this->min; return $this; } + /** + * Refresh the range depending on the current values of min, max and nrOfTicks + */ + private function updateMaxValue() + { + $this->max = $this->calculateTickRange($this->max - $this->min, $this->nrOfTicks) * + $this->nrOfTicks + $this->min; + } + + /** + * Determine the minimum tick range that is necessary to display the given value range + * correctly + * + * @param int range The range to display + * @param int ticks The amount of ticks to use + * + * @return int The value for each tick + */ + private function calculateTickRange($range, $ticks) + { + $factor = 1; + $steps = array(1, 2, 5); + $step = 0; + while ($range / ($factor * $steps[$step]) > $ticks) { + $step++; + if ($step === count($steps)) { + $step = 0; + $factor *= 10; + } + } + return $steps[$step] * $factor; + } + /** * Transform the absolute value to an axis relative value * @@ -114,7 +149,7 @@ class LinearUnit implements AxisUnit } elseif ($value > $this->max) { return 100; } else { - return 100 * ($value - $this->min) / ($this->max - $this->min); + return 100 * ($value - $this->min) / $this->max - $this->min; } } @@ -176,6 +211,7 @@ class LinearUnit implements AxisUnit if ($max !== null) { $this->max = $max; $this->staticMax = true; + $this->updateMaxValue(); } } @@ -189,6 +225,7 @@ class LinearUnit implements AxisUnit if ($min !== null) { $this->min = $min; $this->staticMin = true; + $this->updateMaxValue(); } } diff --git a/modules/monitoring/application/controllers/ChartController.php b/modules/monitoring/application/controllers/ChartController.php index 3cfb4ef71..31bea79af 100644 --- a/modules/monitoring/application/controllers/ChartController.php +++ b/modules/monitoring/application/controllers/ChartController.php @@ -140,7 +140,8 @@ class Monitoring_ChartController extends Controller } $this->view->chart = new GridChart(); $this->view->chart->setAxisLabel('', t('Services')) - ->setXAxis(new \Icinga\Chart\Unit\StaticAxis()); + ->setXAxis(new \Icinga\Chart\Unit\StaticAxis()) + ->setAxisMin(null, 0); $this->view->chart->drawBars( array( @@ -190,7 +191,9 @@ class Monitoring_ChartController extends Controller ); } $this->view->chart = new GridChart(); - $this->view->chart->setAxisLabel('', t('Hosts'))->setXAxis(new StaticAxis()); + $this->view->chart->setAxisLabel('', t('Hosts')) + ->setXAxis(new StaticAxis()) + ->setAxisMin(null, 0); $this->view->chart->drawBars( array( 'label' => t('Up'),