{svg}'; /** * The colors used to display the slices of this pie-chart. * * @var array */ private $colors = array('#049BAF', '#ffaa44', '#ff5566', '#ddccdd'); /** * The title of the chart * * @var string */ private $title; /** * @var int */ private $size = 16; /** * The data displayed by the pie-chart * * @var array */ private $data; /** * @var */ private $class = ''; /** * Set the data to be displayed. * * @param $data array * * @return $this */ public function setData(array $data) { $this->data = $data; return $this; } /** * Set the size of the inline pie * * @param int $size Sets both, the height and width * * @return $this */ public function setSize($size = null) { $this->size = $size; return $this; } /** * Set the class to define the * * @param $class * * @return $this */ public function setSparklineClass($class) { $this->class = $class; return $this; } /** * Set the colors used by the slices of the pie chart. * * @param array $colors * * @return $this */ public function setColors(array $colors = null) { $this->colors = $colors; return $this; } /** * Set the title of the displayed Data * * @param string $title * * @return $this */ public function setTitle($title) { $this->title = $this->view()->escape($title); return $this; } /** * Create a new InlinePie * * @param array $data The data displayed by the slices * @param string $title The title of this Pie * @param array $colors An array of RGB-Color values to use */ public function __construct(array $data, $title, $colors = null) { $this->setTitle($title); if (array_key_exists('data', $data)) { $this->data = $data['data']; if (array_key_exists('colors', $data)) { $this->colors = $data['colors']; } } else { $this->setData($data); } if (isset($colors)) { $this->setColors($colors); } else { $this->setColors($this->colors); } } /** * Renders this widget via the given view and returns the * HTML as a string * * @return string */ public function render() { $pie = new PieChart(); $pie->alignTopLeft(); $pie->disableLegend(); $pie->drawPie([ 'data' => $this->data, 'colors' => $this->colors ]); if ($this->view()->layout()->getLayout() === 'pdf') { try { $png = $pie->toPng($this->size, $this->size); return ''; } catch (IcingaException $_) { return ''; } } $pie->title = $this->title; $pie->description = $this->title; $template = $this->template; $template = str_replace('{class}', $this->class, $template); $template = str_replace('{svg}', $pie->render(), $template); return $template; } public static function createFromStateSummary(stdClass $states, $title, array $colors) { $handledUnhandledStates = []; foreach ($states as $key => $value) { if (StringHelper::endsWith($key, '_handled') || StringHelper::endsWith($key, '_unhandled')) { $handledUnhandledStates[$key] = $value; } } $chart = new self(array_values($handledUnhandledStates), $title, $colors); return $chart ->setSize(50) ->setTitle('') ->setSparklineClass('sparkline-multi'); } }