2014-03-26 14:56:35 +01:00
|
|
|
<?php
|
2014-04-28 14:03:52 +02:00
|
|
|
// @codeCoverageIgnoreStart
|
2014-03-26 14:56:35 +01:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
|
|
|
* This file is part of Icinga Web 2.
|
|
|
|
*
|
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Web\Widget\Chart;
|
|
|
|
|
|
|
|
use Icinga\Web\Widget\AbstractWidget;
|
|
|
|
use Icinga\Web\Url;
|
2014-06-18 13:53:38 +02:00
|
|
|
use Icinga\Util\Format;
|
|
|
|
use Icinga\Logger\Logger;
|
2014-03-26 14:56:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A SVG-PieChart intended to be displayed as a small icon next to labels, to offer a better visualization of the
|
|
|
|
* shown data
|
|
|
|
*
|
|
|
|
* NOTE: When InlinePies are shown in a dynamically loaded view, like the side-bar or in the dashboard, the SVGs will
|
|
|
|
* be replaced with a jQuery-Sparkline to save resources @see loader.js
|
|
|
|
*
|
|
|
|
* @package Icinga\Web\Widget\Chart
|
|
|
|
*/
|
|
|
|
class InlinePie extends AbstractWidget
|
|
|
|
{
|
2014-06-25 14:53:39 +02:00
|
|
|
const NUMBER_FORMAT_NONE = 'none';
|
|
|
|
const NUMBER_FORMAT_TIME = 'time';
|
2014-06-18 13:53:38 +02:00
|
|
|
const NUMBER_FORMAT_BYTES = 'bytes';
|
|
|
|
const NUMBER_FORMAT_RATIO = 'ratio';
|
|
|
|
|
2014-03-26 14:56:35 +01:00
|
|
|
/**
|
2014-06-18 13:53:38 +02:00
|
|
|
* The template string used for rendering this widget
|
2014-03-26 14:56:35 +01:00
|
|
|
* The template string used for rendering this widget
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $template =<<<'EOD'
|
2014-06-18 13:53:38 +02:00
|
|
|
<span
|
|
|
|
class="sparkline"
|
|
|
|
sparkTitle="{title}"
|
|
|
|
sparkWidth="{width}"
|
|
|
|
sparkHeight="{height}"
|
|
|
|
sparkBorderWidth="{borderWidth}"
|
|
|
|
sparkBorderColor="{borderColor}"
|
|
|
|
sparkTooltipChartTitle="{title}"
|
|
|
|
style="{style}"
|
|
|
|
labels="{labels}"
|
|
|
|
formatted="{formatted}"
|
2014-06-18 17:37:01 +02:00
|
|
|
hideEmptyLabel={hideEmptyLabel}
|
2014-06-18 13:53:38 +02:00
|
|
|
values="{data}"
|
|
|
|
tooltipFormat="{tooltipFormat}"
|
|
|
|
sparkSliceColors="[{colors}]"
|
|
|
|
sparkType="pie"></span>
|
2014-06-06 16:35:33 +02:00
|
|
|
<noscript>
|
2014-05-07 17:03:27 +02:00
|
|
|
<img class="inlinepie"
|
2014-03-26 14:56:35 +01:00
|
|
|
title="{title}" src="{url}" style="width: {width}px; height: {height}px; {style}"
|
|
|
|
data-icinga-colors="{colors}" data-icinga-values="{data}"
|
2014-05-07 17:03:27 +02:00
|
|
|
/>
|
2014-06-06 16:35:33 +02:00
|
|
|
</noscript>
|
2014-03-26 14:56:35 +01:00
|
|
|
EOD;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Url
|
|
|
|
*/
|
|
|
|
private $url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The colors used to display the slices of this pie-chart.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-03-31 12:35:39 +02:00
|
|
|
private $colors = array('#44bb77', '#ffaa44', '#ff5566', '#ddccdd');
|
2014-03-26 14:56:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The width of the rendered chart
|
|
|
|
*
|
2014-03-26 17:06:21 +01:00
|
|
|
* @var int The value in px
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
2014-05-07 17:03:27 +02:00
|
|
|
private $width = 28;
|
2014-03-26 14:56:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The height of the rendered chart
|
|
|
|
*
|
2014-03-26 17:06:21 +01:00
|
|
|
* @var int The value in px
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
2014-05-07 17:03:27 +02:00
|
|
|
private $height = 28;
|
2014-03-26 14:56:35 +01:00
|
|
|
|
2014-06-18 13:53:38 +02:00
|
|
|
/**
|
|
|
|
* PieChart border width
|
|
|
|
*
|
|
|
|
* @var float
|
|
|
|
*/
|
2014-06-22 16:38:12 +02:00
|
|
|
private $borderWidth = 0;
|
2014-06-18 13:53:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The color of the border
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $borderColor = '#888';
|
|
|
|
|
2014-03-26 14:56:35 +01:00
|
|
|
/**
|
|
|
|
* The title of the chart
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-06-25 14:29:17 +02:00
|
|
|
private $title = '';
|
2014-03-26 14:56:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The style for the HtmlElement
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $style = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The data displayed by the pie-chart
|
|
|
|
*
|
2014-06-18 13:53:38 +02:00
|
|
|
* @var array
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
|
|
|
private $data;
|
|
|
|
|
2014-06-18 13:53:38 +02:00
|
|
|
/**
|
|
|
|
* The labels to display for each data set
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-06-18 17:37:01 +02:00
|
|
|
private $labels = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the tooltip for the "empty" area should be hidden
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $hideEmptyLabel = false;
|
2014-06-18 13:53:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The format string used to display tooltips
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $tooltipFormat = '<b>{{title}}</b></br> {{label}}: {{formatted}} ({{percent}}%)';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number format used to render numeric values in tooltips
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-06-25 14:53:39 +02:00
|
|
|
private $format = self::NUMBER_FORMAT_NONE;
|
2014-06-18 13:53:38 +02:00
|
|
|
|
2014-06-18 17:37:01 +02:00
|
|
|
/**
|
|
|
|
* Set if the tooltip for the empty area should be hidden
|
|
|
|
*
|
|
|
|
* @param bool $hide Whether to hide the empty area
|
|
|
|
*/
|
|
|
|
public function setHideEmptyLabel($hide = true)
|
|
|
|
{
|
|
|
|
$this->hideEmptyLabel = $hide;
|
|
|
|
}
|
|
|
|
|
2014-03-26 14:56:35 +01:00
|
|
|
/**
|
|
|
|
* Set the data to be displayed.
|
|
|
|
*
|
|
|
|
* @param $data array
|
|
|
|
*/
|
|
|
|
public function setData(array $data)
|
|
|
|
{
|
|
|
|
$this->data = $data;
|
|
|
|
$this->url->setParam('data', implode(',', $data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The labels to be displayed in the pie-chart
|
|
|
|
*
|
2014-06-25 14:29:17 +02:00
|
|
|
* @param null $labels
|
2014-04-16 19:43:56 +02:00
|
|
|
*
|
2014-06-25 14:29:17 +02:00
|
|
|
* @return $this
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
2014-06-25 14:29:17 +02:00
|
|
|
public function setLabels($labels = null)
|
2014-03-26 14:56:35 +01:00
|
|
|
{
|
2014-06-25 14:29:17 +02:00
|
|
|
if ($labels != null) {
|
|
|
|
$this->url->setParam('labels', implode(',', $labels));
|
2014-06-18 13:53:38 +02:00
|
|
|
} else {
|
|
|
|
$this->url->removeKey('labels');
|
|
|
|
}
|
2014-06-25 14:29:17 +02:00
|
|
|
$this->labels = $labels;
|
2014-04-16 19:43:56 +02:00
|
|
|
return $this;
|
2014-03-26 14:56:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the colors used by the slices of the pie chart.
|
|
|
|
*
|
|
|
|
* @param array $colors
|
|
|
|
*/
|
|
|
|
public function setColors(array $colors = null)
|
|
|
|
{
|
|
|
|
$this->colors = $colors;
|
|
|
|
if (isset($colors)) {
|
|
|
|
$this->url->setParam('colors', implode(',', $colors));
|
|
|
|
} else {
|
|
|
|
$this->url->setParam('colors', null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 13:53:38 +02:00
|
|
|
/**
|
|
|
|
* Set the used number format
|
|
|
|
*
|
|
|
|
* @param $format string 'bytes' or 'time'
|
|
|
|
*/
|
|
|
|
public function setNumberFormat($format)
|
|
|
|
{
|
|
|
|
$this->format = $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-18 14:33:03 +02:00
|
|
|
* A format string used to render the content of the piechart tooltips
|
2014-06-18 13:53:38 +02:00
|
|
|
*
|
2014-06-18 14:33:03 +02:00
|
|
|
* Placeholders using curly braces '{FOO}' are replace with their specific values. The format
|
|
|
|
* String may contain HTML-Markup. The available replaceable values are:
|
2014-06-18 13:53:38 +02:00
|
|
|
* <ul>
|
|
|
|
* <li><b>label</b>: The description for the current value </li>
|
|
|
|
* <li><b>formatted</b>: A string representing the formatted value </li>
|
|
|
|
* <li><b>value</b>: The raw (non-formatted) value used to render the piechart </li>
|
|
|
|
* <li><b>percent</b>: The percentage of the current value </li>
|
|
|
|
* </ul>
|
|
|
|
* Note: Changes will only affect JavaScript sparklines and not the SVG charts used for fallback
|
|
|
|
*/
|
|
|
|
public function setTooltipFormat($format)
|
|
|
|
{
|
|
|
|
$this->tooltipFormat = $format;
|
|
|
|
}
|
|
|
|
|
2014-03-26 14:56:35 +01:00
|
|
|
/**
|
|
|
|
* @param $height
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setHeight($height)
|
|
|
|
{
|
|
|
|
$this->height = $height;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-06-18 13:53:38 +02:00
|
|
|
/**
|
|
|
|
* Set the border width of the pie chart
|
|
|
|
*
|
|
|
|
* @param float $width Width in px
|
|
|
|
*/
|
|
|
|
public function setBorderWidth($width)
|
|
|
|
{
|
|
|
|
$this->borderWidth = $width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the color of the pie chart border
|
|
|
|
*
|
|
|
|
* @param string $col The color string
|
|
|
|
*/
|
|
|
|
public function setBorderColor($col)
|
|
|
|
{
|
|
|
|
$this->borderColor = $col;
|
|
|
|
}
|
|
|
|
|
2014-03-26 14:56:35 +01:00
|
|
|
/**
|
|
|
|
* @param $width
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setWidth($width)
|
|
|
|
{
|
|
|
|
$this->width = $width;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the styling of the created HtmlElement
|
|
|
|
*
|
2014-06-18 14:33:03 +02:00
|
|
|
* @param string $style
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
|
|
|
public function setStyle($style)
|
|
|
|
{
|
|
|
|
$this->style = $style;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-18 14:33:03 +02:00
|
|
|
* Set the title of the displayed Data
|
2014-03-26 14:56:35 +01:00
|
|
|
*
|
2014-06-18 14:33:03 +02:00
|
|
|
* @param string $title
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
|
|
|
public function setTitle($title)
|
|
|
|
{
|
|
|
|
$this->title = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new InlinePie
|
|
|
|
*
|
2014-06-18 14:33:03 +02:00
|
|
|
* @param array $data The data displayed by the slices
|
|
|
|
* @param array $colors An array of RGB-Color values to use
|
2014-03-26 14:56:35 +01:00
|
|
|
*/
|
2014-06-25 14:29:17 +02:00
|
|
|
public function __construct(array $data, $colors = null)
|
2014-03-26 14:56:35 +01:00
|
|
|
{
|
|
|
|
$this->url = Url::fromPath('svg/chart.php');
|
|
|
|
if (array_key_exists('data', $data)) {
|
|
|
|
$this->data = $data['data'];
|
|
|
|
if (array_key_exists('labels', $data)) {
|
|
|
|
$this->labels = $data['labels'];
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 13:53:38 +02:00
|
|
|
/**
|
|
|
|
* Create a serialization containing the current label array
|
|
|
|
*
|
|
|
|
* @return string A serialized array of labels
|
|
|
|
*/
|
2014-06-25 14:29:17 +02:00
|
|
|
private function createLabelString () {
|
2014-06-18 14:33:03 +02:00
|
|
|
$labels = $this->labels;
|
|
|
|
foreach ($labels as $key => $label) {
|
2014-06-25 14:29:17 +02:00
|
|
|
$labels[$key] = preg_replace('/|/', '', $label);
|
2014-06-18 14:33:03 +02:00
|
|
|
}
|
|
|
|
return isset($this->labels) && is_array($this->labels) ? implode('|', $this->labels) : '';
|
2014-06-18 13:53:38 +02:00
|
|
|
}
|
|
|
|
|
2014-03-26 14:56:35 +01:00
|
|
|
/**
|
|
|
|
* Renders this widget via the given view and returns the
|
|
|
|
* HTML as a string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$template = $this->template;
|
2014-06-25 14:29:17 +02:00
|
|
|
$template = preg_replace('{{url}}', $this->url, $template);
|
|
|
|
|
2014-06-18 13:53:38 +02:00
|
|
|
// style
|
2014-06-25 14:29:17 +02:00
|
|
|
$template = preg_replace('{{width}}', htmlspecialchars($this->width), $template);
|
|
|
|
$template = preg_replace('{{height}}', htmlspecialchars($this->height), $template);
|
|
|
|
$template = preg_replace('{{title}}', htmlspecialchars($this->title), $template);
|
|
|
|
$template = preg_replace('{{style}}', $this->style, $template);
|
|
|
|
$template = preg_replace('{{colors}}', implode(',', $this->colors), $template);
|
|
|
|
$template = preg_replace('{{borderWidth}}', htmlspecialchars($this->borderWidth), $template);
|
|
|
|
$template = preg_replace('{{borderColor}}', htmlspecialchars($this->borderColor), $template);
|
|
|
|
$template = preg_replace('{{hideEmptyLabel}}', $this->hideEmptyLabel ? 'true' : 'false', $template);
|
2014-06-18 13:53:38 +02:00
|
|
|
|
|
|
|
// values
|
|
|
|
$formatted = array();
|
|
|
|
foreach ($this->data as $key => $value) {
|
|
|
|
$formatted[$key] = $this->formatValue($value);
|
|
|
|
}
|
2014-06-25 14:29:17 +02:00
|
|
|
$template = preg_replace('{{data}}', htmlspecialchars(implode(',', $this->data)), $template);
|
|
|
|
$template = preg_replace('{{formatted}}', htmlspecialchars(implode('|', $formatted)), $template);
|
|
|
|
$template = preg_replace('{{labels}}', htmlspecialchars($this->createLabelString()), $template);
|
|
|
|
$template = preg_replace('{{tooltipFormat}}', $this->tooltipFormat, $template);
|
2014-03-26 14:56:35 +01:00
|
|
|
return $template;
|
|
|
|
}
|
2014-06-18 13:53:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the given value depending on the current value of numberFormat
|
|
|
|
*
|
|
|
|
* @param float $value The value to format
|
|
|
|
*
|
|
|
|
* @return string The formatted value
|
|
|
|
*/
|
|
|
|
private function formatValue($value)
|
|
|
|
{
|
2014-06-25 14:53:39 +02:00
|
|
|
if ($this->format === self::NUMBER_FORMAT_NONE) {
|
|
|
|
return (string)$value;
|
|
|
|
} elseif ($this->format === self::NUMBER_FORMAT_BYTES) {
|
2014-06-18 13:53:38 +02:00
|
|
|
return Format::bytes($value);
|
2014-06-25 14:53:39 +02:00
|
|
|
} elseif ($this->format === self::NUMBER_FORMAT_TIME) {
|
2014-06-18 13:53:38 +02:00
|
|
|
return Format::duration($value);
|
2014-06-25 14:53:39 +02:00
|
|
|
} elseif ($this->format === self::NUMBER_FORMAT_RATIO) {
|
2014-06-18 13:53:38 +02:00
|
|
|
return $value;
|
|
|
|
} else {
|
|
|
|
Logger::warning('Unknown format string "' . $this->format . '" for InlinePie, value not formatted.');
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
2014-03-26 14:56:35 +01:00
|
|
|
}
|
2014-04-28 14:03:52 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|