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
This commit is contained in:
Matthias Jentsch 2014-07-21 17:17:09 +02:00
parent cebd71b2ff
commit 0a500efd8a
2 changed files with 45 additions and 5 deletions

View File

@ -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();
}
}

View File

@ -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'),