2013-09-09 13:55:29 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Chart\Graph;
|
|
|
|
|
2014-07-09 18:04:03 +02:00
|
|
|
use DOMElement;
|
|
|
|
use Icinga\Chart\Primitive\Animation;
|
|
|
|
use Icinga\Chart\Primitive\Drawable;
|
|
|
|
use Icinga\Chart\Primitive\Rect;
|
|
|
|
use Icinga\Chart\Primitive\Styleable;
|
|
|
|
use Icinga\Chart\Render\RenderContext;
|
2013-09-09 13:55:29 +02:00
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* Bar graph implementation
|
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
class BarGraph extends Styleable implements Drawable
|
|
|
|
{
|
2014-02-18 18:35:34 +01:00
|
|
|
/**
|
|
|
|
* The width of the bars.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $barWidth = 4;
|
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* The dataset to use for this bar graph
|
2013-09-25 16:32:28 +02:00
|
|
|
*
|
2013-09-21 17:35:18 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
private $dataSet;
|
2013-09-21 17:35:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new BarGraph with the given dataset
|
|
|
|
*
|
|
|
|
* @param array $dataSet An array of datapoints
|
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
public function __construct(array $dataSet)
|
|
|
|
{
|
|
|
|
$this->dataSet = $dataSet;
|
|
|
|
}
|
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* Apply configuration styles from the $cfg
|
|
|
|
*
|
|
|
|
* @param array $cfg The configuration as given in the drawBars call
|
|
|
|
*/
|
|
|
|
public function setStyleFromConfig(array $cfg)
|
|
|
|
{
|
|
|
|
foreach ($cfg as $elem => $value) {
|
|
|
|
if ($elem === 'color') {
|
|
|
|
$this->setFill($value);
|
|
|
|
} elseif ($elem === 'width') {
|
|
|
|
$this->setStrokeWidth($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render this BarChart
|
|
|
|
*
|
|
|
|
* @param RenderContext $ctx The rendering context to use for drawing
|
|
|
|
*
|
|
|
|
* @return DOMElement $dom Element
|
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
public function toSvg(RenderContext $ctx)
|
|
|
|
{
|
|
|
|
$doc = $ctx->getDocument();
|
|
|
|
$group = $doc->createElement('g');
|
2013-09-23 12:09:40 +02:00
|
|
|
$idx = 0;
|
2013-09-21 17:35:18 +02:00
|
|
|
foreach ($this->dataSet as $point) {
|
2014-07-22 11:32:52 +02:00
|
|
|
$rect = new Rect($point[0] - 2, $point[1], 4, 100 - $point[1]);
|
2013-09-09 13:55:29 +02:00
|
|
|
$rect->setFill($this->fill);
|
|
|
|
$rect->setStrokeWidth($this->strokeWidth);
|
2013-09-21 17:35:18 +02:00
|
|
|
$rect->setStrokeColor('black');
|
2013-09-23 12:09:40 +02:00
|
|
|
$rect->setAttribute('data-icinga-graph-index', $idx++);
|
|
|
|
$rect->setAttribute('data-icinga-graph-type', 'bar');
|
2013-09-09 13:55:29 +02:00
|
|
|
$rect->setAdditionalStyle('clip-path: url(#clip);');
|
2013-10-23 13:05:06 +02:00
|
|
|
/*$rect->setAnimation(
|
2013-09-21 17:35:18 +02:00
|
|
|
new Animation(
|
|
|
|
'y',
|
|
|
|
$ctx->yToAbsolute(100),
|
|
|
|
$ctx->yToAbsolute($point[1]),
|
|
|
|
rand(1, 1.5)/2
|
|
|
|
)
|
2013-10-23 13:05:06 +02:00
|
|
|
);*/
|
2013-09-09 13:55:29 +02:00
|
|
|
$group->appendChild($rect->toSvg($ctx));
|
|
|
|
}
|
|
|
|
return $group;
|
|
|
|
}
|
2013-09-21 17:35:18 +02:00
|
|
|
}
|