2013-09-09 13:55:29 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-09-09 13:55:29 +02:00
|
|
|
|
|
|
|
namespace Icinga\Chart;
|
|
|
|
|
2014-12-15 13:55:20 +01:00
|
|
|
use Imagick;
|
2014-07-09 18:04:03 +02:00
|
|
|
use Icinga\Chart\Legend;
|
|
|
|
use Icinga\Chart\Palette;
|
|
|
|
use Icinga\Chart\Primitive\Drawable;
|
|
|
|
use Icinga\Chart\SVGRenderer;
|
2014-08-27 16:03:15 +02:00
|
|
|
use Icinga\Exception\IcingaException;
|
2013-09-09 13:55:29 +02:00
|
|
|
|
|
|
|
/**
|
2013-09-21 17:35:18 +02:00
|
|
|
* Base class for charts, extended by all other Chart classes.
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
|
|
|
abstract class Chart implements Drawable
|
|
|
|
{
|
2014-08-27 13:51:52 +02:00
|
|
|
protected $align = false;
|
|
|
|
|
2013-09-09 13:55:29 +02:00
|
|
|
/**
|
2013-09-21 17:35:18 +02:00
|
|
|
* SVG renderer that handles
|
2013-09-09 13:55:29 +02:00
|
|
|
*
|
|
|
|
* @var SVGRenderer
|
|
|
|
*/
|
|
|
|
protected $renderer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Legend to use for this chart
|
|
|
|
*
|
|
|
|
* @var Legend
|
|
|
|
*/
|
2013-09-21 17:35:18 +02:00
|
|
|
protected $legend;
|
2013-09-09 13:55:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The style-palette for this chart
|
|
|
|
*
|
|
|
|
* @var Palette
|
|
|
|
*/
|
2013-09-21 17:35:18 +02:00
|
|
|
protected $palette;
|
2013-09-09 13:55:29 +02:00
|
|
|
|
2015-02-17 16:34:36 +01:00
|
|
|
/**
|
|
|
|
* The title of this chart, used for providing accessibility features
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $title;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The description for this chart, mandatory for providing accessibility features
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $description;
|
|
|
|
|
2013-09-09 13:55:29 +02:00
|
|
|
/**
|
2013-09-21 17:35:18 +02:00
|
|
|
* Create a new chart object and create internal objects
|
2013-09-09 13:55:29 +02:00
|
|
|
*
|
2013-09-21 17:35:18 +02:00
|
|
|
* If you want to extend this class use the init() method as an extension point,
|
2015-02-17 16:34:36 +01:00
|
|
|
* as this will be called at the end of the construct call
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->legend = new Legend();
|
|
|
|
$this->palette = new Palette();
|
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-21 17:35:18 +02:00
|
|
|
* Extension point for subclasses, called on __construct
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
2013-09-21 17:35:18 +02:00
|
|
|
protected function init()
|
2013-09-09 13:55:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-21 17:35:18 +02:00
|
|
|
* Extension point for implementing rendering logic
|
|
|
|
*
|
|
|
|
* This method is called after data validation, but before toSvg is called
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
|
|
|
protected function build()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-21 17:35:18 +02:00
|
|
|
* Check if the current dataset has the proper structure for this chart.
|
2013-09-09 13:55:29 +02:00
|
|
|
*
|
|
|
|
* Needs to be overwritten by extending classes. The default implementation returns false.
|
|
|
|
*
|
2013-09-25 16:32:28 +02:00
|
|
|
* @return bool True when the dataset is valid, otherwise false
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
2013-09-21 17:35:18 +02:00
|
|
|
abstract public function isValidDataFormat();
|
|
|
|
|
2013-09-09 13:55:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable the legend for this chart
|
|
|
|
*/
|
|
|
|
public function disableLegend()
|
|
|
|
{
|
|
|
|
$this->legend = null;
|
|
|
|
}
|
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* Render this graph and return the created SVG
|
|
|
|
*
|
2014-08-27 16:03:15 +02:00
|
|
|
* @return string The SVG created by the SvgRenderer
|
2013-09-21 17:35:18 +02:00
|
|
|
*
|
2014-08-27 16:03:15 +02:00
|
|
|
* @throws IcingaException Thrown wen the dataset is not valid for this graph
|
2013-09-25 16:32:28 +02:00
|
|
|
* @see SVGRenderer::render
|
2013-09-21 17:35:18 +02:00
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
if (!$this->isValidDataFormat()) {
|
2014-08-27 16:03:15 +02:00
|
|
|
throw new IcingaException('Dataset for graph doesn\'t have the proper structure');
|
2013-09-09 13:55:29 +02:00
|
|
|
}
|
|
|
|
$this->build();
|
2014-08-27 13:51:52 +02:00
|
|
|
if ($this->align) {
|
|
|
|
$this->renderer->preserveAspectRatio();
|
|
|
|
$this->renderer->setXAspectRatioAlignment(SVGRenderer::X_ASPECT_RATIO_MIN);
|
|
|
|
$this->renderer->setYAspectRatioAlignment(SVGRenderer::Y_ASPECT_RATIO_MIN);
|
|
|
|
}
|
2015-02-17 16:34:36 +01:00
|
|
|
|
|
|
|
$this->renderer->setAriaDescription($this->description);
|
|
|
|
$this->renderer->setAriaTitle($this->title);
|
|
|
|
$this->renderer->getCanvas()->setAriaRole('presentation');
|
|
|
|
|
2013-09-09 13:55:29 +02:00
|
|
|
$this->renderer->getCanvas()->addElement($this);
|
|
|
|
return $this->renderer->render();
|
|
|
|
}
|
2014-08-27 13:51:52 +02:00
|
|
|
|
2014-12-15 13:55:20 +01:00
|
|
|
/**
|
|
|
|
* Return this graph rendered as PNG
|
|
|
|
*
|
|
|
|
* @param int $width The width of the PNG in pixel
|
|
|
|
* @param int $height The height of the PNG in pixel
|
|
|
|
*
|
|
|
|
* @return string A PNG binary string
|
|
|
|
*
|
|
|
|
* @throws IcingaException In case ImageMagick is not available
|
|
|
|
*/
|
|
|
|
public function toPng($width, $height)
|
|
|
|
{
|
|
|
|
if (! class_exists('Imagick')) {
|
|
|
|
throw new IcingaException('Cannot render PNGs without ImageMagick');
|
|
|
|
}
|
|
|
|
|
|
|
|
$image = new Imagick();
|
|
|
|
$image->readImageBlob($this->render());
|
|
|
|
$image->setImageFormat('png24');
|
|
|
|
$image->resizeImage($width, $height, imagick::FILTER_LANCZOS, 1);
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
2014-08-27 13:51:52 +02:00
|
|
|
/**
|
|
|
|
* Align the chart to the top left corner instead of centering it
|
|
|
|
*
|
|
|
|
* @param bool $align
|
|
|
|
*/
|
2014-12-15 13:55:20 +01:00
|
|
|
public function alignTopLeft($align = true)
|
2014-08-27 13:51:52 +02:00
|
|
|
{
|
|
|
|
$this->align = $align;
|
|
|
|
}
|
2013-09-09 13:55:29 +02:00
|
|
|
}
|