Move inline chart rendering into separate classes

refs #5679
This commit is contained in:
Matthias Jentsch 2014-02-19 18:59:54 +01:00
parent a2baf4217b
commit 3a10188e81
3 changed files with 175 additions and 47 deletions

View File

@ -0,0 +1,123 @@
<?php
// @codingStandardsIgnoreStart
// {{{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\Chart\Inline;
/**
* Class to render and inline chart directly from the request params.
*
* When rendering huge amounts of inline charts it is too expensive
* to bootstrap the complete application for ever single chart and
* we need to be able render Charts in a compact environment without
* the other Icinga classes.
*
* Class Inline
* @package Icinga\Chart\Inline
*/
class Inline {
/**
* The data displayed in this chart
*
* @var array
*/
protected $data;
/**
* The colors used to display this chart
*
* @var array
*/
protected $colors = array(
'#00FF00', // OK
'#FFFF00', // Warning
'#FF0000', // Critical
'#E066FF' // Unreachable
);
/**
* The labels displayed on this chart
*
* @var array
*/
protected $labels = array();
/**
* The height in percent
*
* @var int
*/
protected $height = 20;
/**
* The width in percent
*
* @var int
*/
protected $width = 20;
protected function sanitizeStringArray(array $arr)
{
$sanitized = array();
foreach ($arr as $key => $value) {
$sanitized[$key] = htmlspecialchars($value);
}
return $sanitized;
}
/**
* Populate the properties from the current request.
*/
public function initFromRequest()
{
$this->data = explode(',', $_GET['data']);
foreach ($this->data as $key => $value) {
$this->data[$key] = (int)$value;
}
for ($i = 0; $i < sizeof($this->data); $i++) {
$this->labels[] = '';
}
if (array_key_exists('this->colors', $_GET)) {
$this->colors = $this->sanitizeStringArray(explode(',', $_GET['colors']));
}
while (sizeof($this->colors) < sizeof($this->data)) {
$this->colors[] = '#FEFEFE';
}
if (array_key_exists('width', $_GET)) {
$this->width = (int)$_GET['width'];
}
if (array_key_exists('height', $_GET)) {
$this->height = (int)$_GET['height'];
}
}
}

View File

@ -0,0 +1,48 @@
<?php
// @codingStandardsIgnoreStart
// {{{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\Chart\Inline;
use Icinga\Chart\PieChart as PieChartRenderer;
/**
* Draw an inline pie-chart directly from the available request parameters.
*/
class PieChart extends Inline {
public function render()
{
$pie = new PieChartRenderer();
$pie->drawPie(array(
'data' => $this->data, 'colors' => $this->colors, 'labels' => $this->labels
));
$pie->setWidth($this->width)->setHeight($this->height);
echo $pie->render();
}
}

View File

@ -27,7 +27,7 @@
*/
// {{{ICINGA_LICENSE_HEADER}}}
use Icinga\Chart\PieChart;
use Icinga\Chart\Inline\PieChart;
use Icinga\Application\Loader;
@ -40,56 +40,13 @@ $loader = new Loader();
$loader->registerNamespace('Icinga', dirname(__FILE__) . '/../../library/Icinga');
$loader->register();
// TODO: move functionality into separate class
function sanitizeStringArray(array $arr)
{
$sanitized = array();
foreach ($arr as $key => $value) {
$sanitized[$key] = htmlspecialchars($value);
}
return $sanitized;
}
if (!array_key_exists('data', $_GET)) {
die;
}
header('Content-Type: image/svg+xml');
$data = explode(',', $_GET['data']);
foreach ($data as $key => $value) {
$data[$key] = (int)$value;
}
$labels = array();
for ($i = 0; $i < sizeof($data); $i++) {
$labels[] = '';
}
if (array_key_exists('colors', $_GET)) {
$colors = sanitizeStringArray(explode(',', $_GET['colors']));
} else {
$colors = array(
'#00FF00', // OK
'#FFFF00', // Warning
'#FF0000', // Critical
'#E066FF' // Unreachable
);
while (sizeof($colors) < sizeof($data)) {
$colors[] = '#FEFEFE';
}
}
$width = 15;
if (array_key_exists('width', $_GET)) {
$width = (int)$_GET['width'];
}
$height = 15;
if (array_key_exists('height', $_GET)) {
$height = (int)$_GET['height'];
}
$pie = new PieChart();
$pie->drawPie(array(
'data' => $data, 'colors' => $colors, 'labels' => $labels
));
$pie->setWidth($width)->setHeight($height);
$pie->initFromRequest();
echo $pie->render();