2013-09-09 13:55:29 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
|
|
|
* This file is part of Icinga 2 Web.
|
|
|
|
*
|
|
|
|
* Icinga 2 Web - 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.
|
|
|
|
*
|
2013-09-25 16:32:28 +02:00
|
|
|
* @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>
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Chart;
|
|
|
|
|
2013-09-25 16:32:28 +02:00
|
|
|
use \DOMNode;
|
|
|
|
use \DOMElement;
|
|
|
|
use \DOMDocument;
|
|
|
|
use \DOMImplementation;
|
|
|
|
use \Icinga\Util\Dimension;
|
|
|
|
use \Icinga\Chart\Render\LayoutBox;
|
|
|
|
use \Icinga\Chart\Render\RenderContext;
|
|
|
|
use \Icinga\Chart\Primitive\Canvas;
|
2013-09-09 13:55:29 +02:00
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* SVG Renderer component.
|
|
|
|
*
|
|
|
|
* Creates the basic DOM tree of the SVG to use
|
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
class SVGRenderer
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The XML-document
|
|
|
|
*
|
|
|
|
* @var DOMDocument
|
|
|
|
*/
|
|
|
|
private $document;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The SVG-element
|
|
|
|
*
|
|
|
|
* @var DOMNode
|
|
|
|
*/
|
|
|
|
private $svg;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The root layer for all elements
|
|
|
|
*
|
2013-09-21 17:35:18 +02:00
|
|
|
* @var Canvas
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
|
|
|
private $rootCanvas;
|
|
|
|
|
|
|
|
/**
|
2013-09-21 17:35:18 +02:00
|
|
|
* The width of this renderer
|
2013-09-09 13:55:29 +02:00
|
|
|
*
|
2013-09-21 17:35:18 +02:00
|
|
|
* @var int
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
|
|
|
private $width = 100;
|
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* The height of this renderer
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
private $height = 100;
|
|
|
|
|
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* Create the root document and the SVG root node
|
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
private function createRootDocument()
|
|
|
|
{
|
|
|
|
$implementation = new DOMImplementation();
|
|
|
|
$docType = $implementation->createDocumentType(
|
|
|
|
'svg',
|
|
|
|
'-//W3C//DTD SVG 1.1//EN',
|
|
|
|
'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->document = $implementation->createDocument(null, null, $docType);
|
|
|
|
$this->svg = $this->createOuterBox();
|
|
|
|
$this->document->appendChild($this->svg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-21 17:35:18 +02:00
|
|
|
/**
|
|
|
|
* Create the outer SVG box containing the root svg element and namespace and return it
|
|
|
|
*
|
2013-09-25 16:32:28 +02:00
|
|
|
* @return DOMElement The SVG root node
|
2013-09-21 17:35:18 +02:00
|
|
|
*/
|
2013-09-09 13:55:29 +02:00
|
|
|
private function createOuterBox()
|
|
|
|
{
|
|
|
|
$ctx = $this->createRenderContext();
|
|
|
|
$svg = $this->document->createElement('svg');
|
|
|
|
$svg->setAttribute('xmlns', 'http://www.w3.org/2000/svg');
|
|
|
|
$svg->setATtribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
|
|
|
|
$svg->setAttribute("width", "100%");
|
|
|
|
$svg->setAttribute("height", "100%");
|
|
|
|
$svg->setAttribute(
|
|
|
|
'viewBox',
|
|
|
|
sprintf(
|
2013-09-21 17:35:18 +02:00
|
|
|
'0 0 %s %s',
|
|
|
|
$ctx->getNrOfUnitsX(),
|
|
|
|
$ctx->getNrOfUnitsY()
|
2013-09-09 13:55:29 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
return $svg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-21 17:35:18 +02:00
|
|
|
* Initialises the XML-document, SVG-element and this figure's root canvas
|
|
|
|
*
|
2013-09-25 16:32:28 +02:00
|
|
|
* @param int $width The width ratio
|
|
|
|
* @param int $height The height ratio
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
2013-09-21 17:35:18 +02:00
|
|
|
public function __construct($width, $height)
|
|
|
|
{
|
2013-09-09 13:55:29 +02:00
|
|
|
$this->width = $width;
|
|
|
|
$this->height = $height;
|
2013-09-21 17:35:18 +02:00
|
|
|
$this->rootCanvas = new Canvas('root', new LayoutBox(0, 0));
|
2013-09-09 13:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-21 17:35:18 +02:00
|
|
|
* Render the SVG-document
|
2013-09-09 13:55:29 +02:00
|
|
|
*
|
2013-09-25 16:32:28 +02:00
|
|
|
* @return string The resulting XML structure
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$this->createRootDocument();
|
|
|
|
$ctx = $this->createRenderContext();
|
|
|
|
$this->svg->appendChild($this->rootCanvas->toSvg($ctx));
|
|
|
|
$this->document->formatOutput = true;
|
|
|
|
return $this->document->saveXML();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-21 17:35:18 +02:00
|
|
|
* Create a render context that will be used for rendering elements
|
2013-09-09 13:55:29 +02:00
|
|
|
*
|
2013-09-25 16:32:28 +02:00
|
|
|
* @return RenderContext The created RenderContext instance
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
2013-09-21 17:35:18 +02:00
|
|
|
public function createRenderContext()
|
2013-09-09 13:55:29 +02:00
|
|
|
{
|
2013-09-21 17:35:18 +02:00
|
|
|
return new RenderContext($this->document, $this->width, $this->height);
|
2013-09-09 13:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-21 17:35:18 +02:00
|
|
|
* Return the root canvas of this rendered
|
2013-09-09 13:55:29 +02:00
|
|
|
*
|
2013-09-25 16:32:28 +02:00
|
|
|
* @return Canvas The canvas that will be the uppermost element in this figure
|
2013-09-09 13:55:29 +02:00
|
|
|
*/
|
2013-09-21 17:35:18 +02:00
|
|
|
public function getCanvas()
|
2013-09-09 13:55:29 +02:00
|
|
|
{
|
2013-09-21 17:35:18 +02:00
|
|
|
return $this->rootCanvas;
|
2013-09-09 13:55:29 +02:00
|
|
|
}
|
|
|
|
}
|