Merge branch 'feature/improve-chart-implementation-7841'
This commit is contained in:
commit
8c8e3e4f3a
|
@ -78,24 +78,23 @@ class Axis implements Drawable
|
|||
private $yUnit = null;
|
||||
|
||||
/**
|
||||
* If the displayed labels should be aligned horizontally or diagonally
|
||||
* The minimum amount of units each step must take up
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $labelRotationStyle = self::LABEL_ROTATE_DIAGONAL;
|
||||
public $minUnitsPerStep = 80;
|
||||
|
||||
/**
|
||||
* Set the label rotation style for the horizontal axis
|
||||
* The minimum amount of units each tick must take up
|
||||
*
|
||||
* <ul>
|
||||
* <li><b>LABEL_ROTATE_HORIZONTAL</b>: Labels will be displayed horizontally </li>
|
||||
* <li><b>LABEL_ROTATE_DIAGONAL</b>: Labels will be rotated by 45° </li>
|
||||
* </ul>
|
||||
*
|
||||
* @param $style The rotation mode
|
||||
* @var int
|
||||
*/
|
||||
public function setHorizontalLabelRotationStyle($style)
|
||||
{
|
||||
$this->labelRotationStyle = $style;
|
||||
}
|
||||
public $minUnitsPerTick = 15;
|
||||
|
||||
/**
|
||||
* If the displayed labels should be aligned horizontally or diagonally
|
||||
*/
|
||||
protected $labelRotationStyle = self::LABEL_ROTATE_HORIZONTAL;
|
||||
|
||||
/**
|
||||
* Inform the axis about an added dataset
|
||||
|
@ -160,58 +159,74 @@ class Axis implements Drawable
|
|||
*/
|
||||
private function renderHorizontalAxis(RenderContext $ctx, DOMElement $group)
|
||||
{
|
||||
$steps = $this->ticksPerX($this->xUnit->getTicks(), $ctx->getNrOfUnitsX(), $this->minUnitsPerStep);
|
||||
$ticks = $this->ticksPerX($this->xUnit->getTicks(), $ctx->getNrOfUnitsX(), $this->minUnitsPerTick);
|
||||
|
||||
// Steps should always be ticks
|
||||
if ($ticks !== $steps) {
|
||||
$steps = $ticks * 5;
|
||||
}
|
||||
|
||||
// Check whether there is enough room for regular labels
|
||||
$labelRotationStyle = $this->labelRotationStyle;
|
||||
if ($this->labelsOversized($this->xUnit, 6)) {
|
||||
$labelRotationStyle = self::LABEL_ROTATE_DIAGONAL;
|
||||
}
|
||||
|
||||
/*
|
||||
$line = new Line(0, 100, 100, 100);
|
||||
$line->setStrokeWidth(2);
|
||||
$group->appendChild($line->toSvg($ctx));
|
||||
*/
|
||||
|
||||
// contains the approximate end position of the last label
|
||||
$lastLabelEnd = -1;
|
||||
$shift = 0;
|
||||
|
||||
$i = 0;
|
||||
foreach ($this->xUnit as $label => $pos) {
|
||||
if ($this->labelRotationStyle === self::LABEL_ROTATE_HORIZONTAL) {
|
||||
// If the last label would overlap this label we shift the y axis a bit
|
||||
if ($lastLabelEnd > $pos) {
|
||||
$shift = ($shift + 5) % 10;
|
||||
} else {
|
||||
$shift = 0;
|
||||
|
||||
if ($i % $ticks === 0) {
|
||||
/*
|
||||
$tick = new Line($pos, 100, $pos, 101);
|
||||
$group->appendChild($tick->toSvg($ctx));
|
||||
*/
|
||||
}
|
||||
|
||||
if ($i % $steps === 0) {
|
||||
if ($labelRotationStyle === self::LABEL_ROTATE_HORIZONTAL) {
|
||||
// If the last label would overlap this label we shift the y axis a bit
|
||||
if ($lastLabelEnd > $pos) {
|
||||
$shift = ($shift + 5) % 10;
|
||||
} else {
|
||||
$shift = 0;
|
||||
}
|
||||
}
|
||||
|
||||
$labelField = new Text($pos + 0.5, ($this->xLabel ? 107 : 105) + $shift, $label);
|
||||
if ($labelRotationStyle === self::LABEL_ROTATE_HORIZONTAL) {
|
||||
$labelField->setAlignment(Text::ALIGN_MIDDLE)
|
||||
->setFontSize('2.5em');
|
||||
} else {
|
||||
$labelField->setFontSize('2.5em');
|
||||
}
|
||||
|
||||
if ($labelRotationStyle === self::LABEL_ROTATE_DIAGONAL) {
|
||||
$labelField = new Rotator($labelField, 45);
|
||||
}
|
||||
$labelField = $labelField->toSvg($ctx);
|
||||
|
||||
$group->appendChild($labelField);
|
||||
|
||||
if ($this->drawYGrid) {
|
||||
$bgLine = new Line($pos, 0, $pos, 100);
|
||||
$bgLine->setStrokeWidth(0.5)
|
||||
->setStrokeColor('#BFBFBF');
|
||||
$group->appendChild($bgLine->toSvg($ctx));
|
||||
}
|
||||
$lastLabelEnd = $pos + strlen($label) * 1.2;
|
||||
}
|
||||
|
||||
$tick = new Line($pos, 100, $pos, 102);
|
||||
$group->appendChild($tick->toSvg($ctx));
|
||||
|
||||
$labelField = new Text($pos + 0.5, ($this->xLabel ? 107 : 105) + $shift, $label);
|
||||
if ($this->labelRotationStyle === self::LABEL_ROTATE_HORIZONTAL) {
|
||||
$labelField->setAlignment(Text::ALIGN_MIDDLE)
|
||||
->setFontSize('1.8em');
|
||||
} else {
|
||||
$labelField->setFontSize('2.5em');
|
||||
}
|
||||
|
||||
if ($this->labelRotationStyle === self::LABEL_ROTATE_DIAGONAL) {
|
||||
$labelField = new Rotator($labelField, 45);
|
||||
}
|
||||
$labelField = $labelField->toSvg($ctx);
|
||||
|
||||
$group->appendChild($labelField);
|
||||
|
||||
if ($this->drawYGrid) {
|
||||
$bgLine = new Line($pos, 0, $pos, 100);
|
||||
$bgLine->setStrokeWidth(0.5)
|
||||
->setStrokeColor('#232');
|
||||
$group->appendChild($bgLine->toSvg($ctx));
|
||||
}
|
||||
$lastLabelEnd = $pos + strlen($label) * 1.2;
|
||||
}
|
||||
|
||||
// render the label for this axis
|
||||
if ($this->xLabel) {
|
||||
$axisLabel = new Text(50, 104, $this->xLabel);
|
||||
$axisLabel->setFontSize('2em')
|
||||
->setFontWeight('bold')
|
||||
->setAlignment(Text::ALIGN_MIDDLE);
|
||||
$group->appendChild($axisLabel->toSvg($ctx));
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -223,34 +238,59 @@ class Axis implements Drawable
|
|||
*/
|
||||
private function renderVerticalAxis(RenderContext $ctx, DOMElement $group)
|
||||
{
|
||||
$steps = $this->ticksPerX($this->yUnit->getTicks(), $ctx->getNrOfUnitsY(), $this->minUnitsPerStep);
|
||||
$ticks = $this->ticksPerX($this->yUnit->getTicks(), $ctx->getNrOfUnitsY(), $this->minUnitsPerTick);
|
||||
|
||||
// Steps should always be ticks
|
||||
if ($ticks !== $steps) {
|
||||
$steps = $ticks * 5;
|
||||
}
|
||||
/*
|
||||
$line = new Line(0, 0, 0, 100);
|
||||
$line->setStrokeWidth(2);
|
||||
$group->appendChild($line->toSvg($ctx));
|
||||
*/
|
||||
|
||||
$i = 0;
|
||||
foreach ($this->yUnit as $label => $pos) {
|
||||
$pos = 100 - $pos;
|
||||
$tick = new Line(0, $pos, -1, $pos);
|
||||
$group->appendChild($tick->toSvg($ctx));
|
||||
|
||||
$labelField = new Text(-0.5, $pos+0.5, $label);
|
||||
$labelField->setFontSize('1.8em')
|
||||
->setAlignment(Text::ALIGN_END);
|
||||
|
||||
$group->appendChild($labelField->toSvg($ctx));
|
||||
if ($this->drawXGrid) {
|
||||
$bgLine = new Line(0, $pos, 100, $pos);
|
||||
$bgLine->setStrokeWidth(0.5)
|
||||
->setStrokeColor('#343');
|
||||
$group->appendChild($bgLine->toSvg($ctx));
|
||||
if ($i % $ticks === 0) {
|
||||
// draw a tick
|
||||
//$tick = new Line(0, $pos, -1, $pos);
|
||||
//$group->appendChild($tick->toSvg($ctx));
|
||||
}
|
||||
|
||||
if ($i % $steps === 0) {
|
||||
// draw a step
|
||||
$labelField = new Text(-0.5, $pos + 0.5, $label);
|
||||
$labelField->setFontSize('2.5em')
|
||||
->setAlignment(Text::ALIGN_END);
|
||||
|
||||
$group->appendChild($labelField->toSvg($ctx));
|
||||
if ($this->drawXGrid) {
|
||||
$bgLine = new Line(0, $pos, 100, $pos);
|
||||
$bgLine->setStrokeWidth(0.5)
|
||||
->setStrokeColor('#BFBFBF');
|
||||
$group->appendChild($bgLine->toSvg($ctx));
|
||||
}
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
|
||||
if ($this->yLabel) {
|
||||
$axisLabel = new Text(-8, 50, $this->yLabel);
|
||||
if ($this->yLabel || $this->xLabel) {
|
||||
if ($this->yLabel && $this->xLabel) {
|
||||
$txt = $this->yLabel . ' / ' . $this->xLabel;
|
||||
} else if ($this->xLabel) {
|
||||
$txt = $this->xLabel;
|
||||
} else {
|
||||
$txt = $this->yLabel;
|
||||
}
|
||||
|
||||
$axisLabel = new Text(50, -3, $txt);
|
||||
$axisLabel->setFontSize('2em')
|
||||
->setFontWeight('bold')
|
||||
->setAlignment(Text::ALIGN_MIDDLE);
|
||||
$axisLabel = new Rotator($axisLabel, 90);
|
||||
|
||||
$group->appendChild($axisLabel->toSvg($ctx));
|
||||
}
|
||||
|
@ -416,4 +456,32 @@ class Axis implements Drawable
|
|||
$this->renderVerticalAxis($ctx, $group);
|
||||
return $group;
|
||||
}
|
||||
|
||||
protected function ticksPerX($ticks, $units, $min)
|
||||
{
|
||||
$per = 1;
|
||||
while ($per * $units / $ticks < $min) {
|
||||
$per++;
|
||||
}
|
||||
return $per;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether at least one label of the given Axis
|
||||
* is bigger than the given maxLength
|
||||
*
|
||||
* @param AxisUnit $axis The axis that contains the labels that will be checked
|
||||
*
|
||||
* @return boolean Whether at least one label is bigger than maxLength
|
||||
*/
|
||||
private function labelsOversized(AxisUnit $axis, $maxLength = 5)
|
||||
{
|
||||
$oversized = false;
|
||||
foreach ($axis as $label => $pos) {
|
||||
if (strlen($label) > $maxLength) {
|
||||
$oversized = true;
|
||||
}
|
||||
}
|
||||
return $oversized;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ class BarGraph extends Styleable implements Drawable
|
|||
*
|
||||
* @var int
|
||||
*/
|
||||
private $barWidth = 4;
|
||||
private $barWidth = 3;
|
||||
|
||||
/**
|
||||
* The dataset to use for this bar graph
|
||||
|
@ -122,6 +122,14 @@ class BarGraph extends Styleable implements Drawable
|
|||
$doc = $ctx->getDocument();
|
||||
$group = $doc->createElement('g');
|
||||
$idx = 0;
|
||||
|
||||
if (count($this->dataSet) > 15) {
|
||||
$this->barWidth = 2;
|
||||
}
|
||||
if (count($this->dataSet) > 25) {
|
||||
$this->barWidth = 1;
|
||||
}
|
||||
|
||||
foreach ($this->dataSet as $x => $point) {
|
||||
// add white background bar, to prevent other bars from altering transparency effects
|
||||
$bar = $this->drawSingleBar($point, $idx++, 'white', $this->strokeWidth, $idx)->toSvg($ctx);
|
||||
|
|
|
@ -45,6 +45,13 @@ class LineGraph extends Styleable implements Drawable
|
|||
*/
|
||||
public $strokeWidth = 5;
|
||||
|
||||
/**
|
||||
* The size of the displayed dots
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $dotWith = 0;
|
||||
|
||||
/**
|
||||
* Create a new LineGraph displaying the given dataset
|
||||
*
|
||||
|
@ -138,8 +145,8 @@ class LineGraph extends Styleable implements Drawable
|
|||
$group = $path->toSvg($ctx);
|
||||
if ($this->showDataPoints === true) {
|
||||
foreach ($this->dataset as $point) {
|
||||
$dot = new Circle($point[0], $point[1], $this->strokeWidth*5);
|
||||
$dot->setFill('black');
|
||||
$dot = new Circle($point[0], $point[1], $this->dotWith);
|
||||
$dot->setFill($this->strokeColor);
|
||||
|
||||
$group->appendChild($dot->toSvg($ctx));
|
||||
}
|
||||
|
|
|
@ -66,13 +66,14 @@ class Legend implements Drawable
|
|||
$outer->getLayout()->setPadding(2, 2, 2, 2);
|
||||
$nrOfColumns = 4;
|
||||
|
||||
$leftstep = 100 / $nrOfColumns;
|
||||
$topstep = 10 / $nrOfColumns + 2;
|
||||
|
||||
$top = 0;
|
||||
$left = 0;
|
||||
$lastLabelEndPos = -1;
|
||||
foreach ($this->dataset as $color => $text) {
|
||||
$leftstep = 100 / $nrOfColumns + strlen($text);
|
||||
|
||||
// Make sure labels don't overlap each other
|
||||
while ($lastLabelEndPos >= $left) {
|
||||
$left += $leftstep;
|
||||
|
|
|
@ -61,7 +61,7 @@ class Circle extends Styleable implements Drawable
|
|||
$circle = $ctx->getDocument()->createElement('circle');
|
||||
$circle->setAttribute('cx', Format::formatSVGNumber($coords[0]));
|
||||
$circle->setAttribute('cy', Format::formatSVGNumber($coords[1]));
|
||||
$circle->setAttribute('r', 5);
|
||||
$circle->setAttribute('r', $this->radius);
|
||||
$circle->setAttribute('style', $this->getStyle());
|
||||
$this->applyAttributes($circle);
|
||||
return $circle;
|
||||
|
|
|
@ -9,13 +9,14 @@ 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
|
||||
*
|
||||
|
@ -46,4 +47,11 @@ interface AxisUnit extends Iterator
|
|||
* @param int $max The new maximum value
|
||||
*/
|
||||
public function setMax($max);
|
||||
|
||||
/**
|
||||
* Get the amount of ticks of this axis
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTicks();
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ namespace Icinga\Chart\Unit;
|
|||
*/
|
||||
class LinearUnit implements AxisUnit
|
||||
{
|
||||
|
||||
/**
|
||||
* The minimum value to display
|
||||
*
|
||||
|
@ -43,7 +42,7 @@ class LinearUnit implements AxisUnit
|
|||
*
|
||||
* @var int
|
||||
*/
|
||||
private $nrOfTicks = 10;
|
||||
protected $nrOfTicks = 10;
|
||||
|
||||
/**
|
||||
* The currently displayed tick
|
||||
|
@ -95,45 +94,13 @@ class LinearUnit implements AxisUnit
|
|||
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;
|
||||
}
|
||||
if ($this->max === $this->min) {
|
||||
$this->max = $this->min + 10;
|
||||
}
|
||||
return $steps[$step] * $factor;
|
||||
$this->nrOfTicks = $this->max - $this->min;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -149,7 +116,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->nrOfTicks;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -211,7 +178,6 @@ class LinearUnit implements AxisUnit
|
|||
if ($max !== null) {
|
||||
$this->max = $max;
|
||||
$this->staticMax = true;
|
||||
$this->updateMaxValue();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -225,7 +191,6 @@ class LinearUnit implements AxisUnit
|
|||
if ($min !== null) {
|
||||
$this->min = $min;
|
||||
$this->staticMin = true;
|
||||
$this->updateMaxValue();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -248,4 +213,14 @@ class LinearUnit implements AxisUnit
|
|||
{
|
||||
return $this->max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount of ticks necessary to display this AxisUnit
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTicks()
|
||||
{
|
||||
return $this->nrOfTicks;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,264 @@
|
|||
<?php
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
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 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 $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;
|
||||
}
|
||||
}
|
|
@ -118,4 +118,14 @@ class StaticAxis implements AxisUnit
|
|||
{
|
||||
return reset($this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount of ticks of this axis
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTicks()
|
||||
{
|
||||
return count($this->items);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -342,10 +342,10 @@ class Monitoring_AlertsummaryController extends Controller
|
|||
$gridChart = new GridChart();
|
||||
|
||||
$gridChart->alignTopLeft();
|
||||
$gridChart->setAxisLabel('', mt('monitoring', 'Notifications'))
|
||||
$gridChart->setAxisLabel($this->createPeriodDescription(), mt('monitoring', 'Notifications'))
|
||||
->setXAxis(new StaticAxis())
|
||||
->setAxisMin(null, 0)
|
||||
->setYAxis(new LinearUnit(10));
|
||||
->setYAxis(new LinearUnit(10))
|
||||
->setAxisMin(null, 0);
|
||||
|
||||
$interval = $this->getInterval();
|
||||
|
||||
|
@ -429,11 +429,10 @@ class Monitoring_AlertsummaryController extends Controller
|
|||
$item[1] = $item[1]/60/60;
|
||||
}
|
||||
|
||||
|
||||
$gridChart->drawBars(
|
||||
array(
|
||||
'label' => $this->translate('Notifications'),
|
||||
'color' => '#049baf',
|
||||
'color' => '#07C0D9',
|
||||
'data' => $notifications,
|
||||
'showPoints' => true
|
||||
)
|
||||
|
@ -470,15 +469,15 @@ class Monitoring_AlertsummaryController extends Controller
|
|||
$gridChart = new GridChart();
|
||||
|
||||
$gridChart->alignTopLeft();
|
||||
$gridChart->setAxisLabel('', mt('monitoring', 'Notifications'))
|
||||
$gridChart->setAxisLabel($this->createPeriodDescription(), mt('monitoring', 'Notifications'))
|
||||
->setXAxis(new StaticAxis())
|
||||
->setAxisMin(null, 0)
|
||||
->setYAxis(new LinearUnit(10));
|
||||
->setYAxis(new LinearUnit(10))
|
||||
->setAxisMin(null, 0);
|
||||
|
||||
$gridChart->drawBars(
|
||||
array(
|
||||
'label' => $this->translate('Notifications'),
|
||||
'color' => '#049baf',
|
||||
'color' => '#07C0D9',
|
||||
'data' => $this->notificationData,
|
||||
'showPoints' => true
|
||||
)
|
||||
|
@ -554,7 +553,7 @@ class Monitoring_AlertsummaryController extends Controller
|
|||
{
|
||||
$format = '';
|
||||
if ($interval === '1d') {
|
||||
$format = '%H:00:00';
|
||||
$format = '%H:00';
|
||||
} elseif ($interval === '1w') {
|
||||
$format = '%Y-%m-%d';
|
||||
} elseif ($interval === '1m') {
|
||||
|
@ -623,4 +622,28 @@ class Monitoring_AlertsummaryController extends Controller
|
|||
|
||||
return $interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a human-readable description of the current interval size
|
||||
*
|
||||
* @return string The description of the current interval size
|
||||
*/
|
||||
private function createPeriodDescription()
|
||||
{
|
||||
$int = $this->getInterval();
|
||||
switch ($int) {
|
||||
case '1d':
|
||||
return t('Hour');
|
||||
break;
|
||||
case '1w';
|
||||
return t('Day');
|
||||
break;
|
||||
case '1m':
|
||||
return t('Day');
|
||||
break;
|
||||
case '1y':
|
||||
return t('Month');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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' => '#F56',
|
||||
'data' => $data1,
|
||||
'showPoints' => true
|
||||
)
|
||||
);
|
||||
$chart->drawLines(
|
||||
array(
|
||||
'label' => 'Random 2',
|
||||
'color' => '#fa4',
|
||||
'data' => $data2,
|
||||
'showPoints' => true
|
||||
)
|
||||
);
|
||||
$chart->drawLines(
|
||||
array(
|
||||
'label' => 'Random 3',
|
||||
'color' => '#4b7',
|
||||
'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' => '#F56',
|
||||
'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' => '#F56',
|
||||
'data' => $data,
|
||||
'showPoints' => true
|
||||
)
|
||||
);
|
||||
return $chart;
|
||||
}
|
||||
|
||||
public function testAction()
|
||||
{
|
||||
$this->chart = new GridChart();
|
||||
|
@ -28,7 +119,7 @@ class Monitoring_ChartController extends Controller
|
|||
$data1 = array();
|
||||
$data2 = array();
|
||||
$data3 = array();
|
||||
for ($i = 0; $i < 25; $i++) {
|
||||
for ($i = 0; $i < 50; $i++) {
|
||||
$data3[] = array('Label ' . $i, rand(0, 30));
|
||||
}
|
||||
|
||||
|
@ -36,13 +127,13 @@ class Monitoring_ChartController extends Controller
|
|||
$this->chart->drawLines(
|
||||
array(
|
||||
'label' => 'Nr of outtakes',
|
||||
'color' => 'red',
|
||||
'color' => '#F56',
|
||||
'width' => '5',
|
||||
|
||||
'data' => $data
|
||||
), array(
|
||||
'label' => 'Some line',
|
||||
'color' => 'blue',
|
||||
'color' => '#fa4',
|
||||
'width' => '4',
|
||||
|
||||
'data' => $data3,
|
||||
|
@ -52,8 +143,8 @@ class Monitoring_ChartController extends Controller
|
|||
*/
|
||||
$this->chart->drawBars(
|
||||
array(
|
||||
'label' => 'Some other line',
|
||||
'color' => 'green',
|
||||
'label' => 'A big amount of data',
|
||||
'color' => '#4b7',
|
||||
'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()
|
||||
|
@ -185,18 +280,15 @@ class Monitoring_ChartController extends Controller
|
|||
$upBars = array();
|
||||
$downBars = array();
|
||||
$unreachableBars = array();
|
||||
foreach ($query as $hostgroup) {
|
||||
for ($i = 0; $i < 50; $i++) {
|
||||
$upBars[] = array(
|
||||
$hostgroup->hostgroup,
|
||||
$hostgroup->hosts_up
|
||||
(string)$i, rand(1, 200), rand(1, 200)
|
||||
);
|
||||
$downBars[] = array(
|
||||
$hostgroup->hostgroup,
|
||||
$hostgroup->hosts_down_unhandled
|
||||
(string)$i, rand(1, 200), rand(1, 200)
|
||||
);
|
||||
$unreachableBars[] = array(
|
||||
$hostgroup->hostgroup,
|
||||
$hostgroup->hosts_unreachable_unhandled
|
||||
(string)$i, rand(1, 200), rand(1, 200)
|
||||
);
|
||||
}
|
||||
$tooltip = mt('monitoring', '<b>{title}:</b><br> {value} of {sum} hosts are {label}');
|
||||
|
@ -272,7 +364,14 @@ class Monitoring_ChartController extends Controller
|
|||
(int) $query->hosts_unreachable_unhandled,
|
||||
(int) $query->hosts_pending
|
||||
),
|
||||
'colors' => array('#44bb77', '#ff4444', '#ff0000', '#E066FF', '#f099FF', '#fefefe'),
|
||||
'colors' => array(
|
||||
'#44bb77', // 'Ok'
|
||||
'#ff4444', // 'Warning'
|
||||
'#ff0000', // 'WarningHandled'
|
||||
'#E066FF',
|
||||
'#f099FF',
|
||||
'#fefefe'
|
||||
),
|
||||
'labels'=> array(
|
||||
(int) $query->hosts_up . mt('monitoring', ' Up Hosts'),
|
||||
(int) $query->hosts_down_handled . mt('monitoring', ' Down Hosts (Handled)'),
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
mah
|
||||
<div style="border:1px dashed black;width:900px;height:520px">
|
||||
<?=
|
||||
$svg->render();
|
||||
?>
|
||||
</div>
|
||||
<?php foreach ($svgs as $svg) { ?>
|
||||
<div style="border:1px dashed black;width:900px;height:520px">
|
||||
<?= $svg->render() ?>
|
||||
</div>
|
||||
<?php } ?>
|
Loading…
Reference in New Issue