2013-09-09 13:55:29 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Chart;
|
|
|
|
|
2014-07-09 18:04:03 +02:00
|
|
|
use Exception;
|
|
|
|
use Icinga\Chart\Legend;
|
|
|
|
use Icinga\Chart\Palette;
|
|
|
|
use Icinga\Chart\Primitive\Drawable;
|
|
|
|
use Icinga\Chart\SVGRenderer;
|
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
|
|
|
|
{
|
|
|
|
/**
|
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
|
|
|
|
|
|
|
/**
|
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,
|
|
|
|
* as this will be called at the end o fthe 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
|
|
|
|
*
|
2013-09-25 16:32:28 +02:00
|
|
|
* @return string The SVG created by the SvgRenderer
|
2013-09-21 17:35:18 +02:00
|
|
|
*
|
2013-09-25 16:32:28 +02:00
|
|
|
* @throws Exception Thrown wen the dataset is not valid for this graph
|
|
|
|
* @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()) {
|
|
|
|
throw new Exception('Dataset for graph doesn\'t have the proper structure');
|
|
|
|
}
|
|
|
|
$this->build();
|
|
|
|
|
|
|
|
$this->renderer->getCanvas()->addElement($this);
|
|
|
|
return $this->renderer->render();
|
|
|
|
}
|
|
|
|
}
|