/usr/share/php/Icinga/Chart/GridChart.php is in php-icinga 2.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | <?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Chart;
use DOMElement;
use Icinga\Chart\Chart;
use Icinga\Chart\Axis;
use Icinga\Chart\Graph\BarGraph;
use Icinga\Chart\Graph\LineGraph;
use Icinga\Chart\Graph\StackedGraph;
use Icinga\Chart\Graph\Tooltip;
use Icinga\Chart\Primitive\Canvas;
use Icinga\Chart\Primitive\Rect;
use Icinga\Chart\Primitive\Path;
use Icinga\Chart\Render\LayoutBox;
use Icinga\Chart\Render\RenderContext;
use Icinga\Chart\Unit\AxisUnit;
/**
* Base class for grid based charts.
*
* Allows drawing of Line and Barcharts. See the graphing documentation for further details.
*
* Example:
* <pre>
* <code>
* $this->chart = new GridChart();
* $this->chart->setAxisLabel("X axis label", "Y axis label");
* $this->chart->setXAxis(Axis::CalendarUnit());
* $this->chart->drawLines(
* array(
* 'data' => array(
* array(time()-7200, 10),array(time()-3620, 30), array(time()-1800, 15), array(time(), 92))
* )
* );
* </code>
* </pre>
*/
class GridChart extends Chart
{
/**
* Internal identifier for Line Chart elements
*/
const TYPE_LINE = "LINE";
/**
* Internal identifier fo Bar Chart elements
*/
const TYPE_BAR = "BAR";
/**
* Internal array containing all elements to be drawn in the order they are drawn
*
* @var array
*/
private $graphs = array();
/**
* An associative array containing all axis of this Chart in the "name" => Axis() form.
*
* Currently only the 'default' axis is really supported
*
* @var array
*/
private $axis = array();
/**
* An associative array containing all StackedGraph objects used for cumulative graphs
*
* The array key is the 'stack' value given in the graph definitions
*
* @var array
*/
private $stacks = array();
/**
* An associative array containing all Tooltips used to render the titles
*
* Each tooltip represents the summary for all y-values of a certain x-value
* in the grid chart
*
* @var Tooltip
*/
private $tooltips = array();
public function __construct()
{
$this->title = t('Grid Chart');
$this->description = t('Contains data in a bar or line chart.');
parent::__construct();
}
/**
* Check if the current dataset has the proper structure for this chart.
*
* Needs to be overwritten by extending classes. The default implementation returns false.
*
* @return bool True when the dataset is valid, otherwise false
*/
public function isValidDataFormat()
{
foreach ($this->graphs as $values) {
foreach ($values as $value) {
if (!isset($value['data']) || !is_array($value['data'])) {
return false;
}
}
}
return true;
}
/**
* Calls Axis::addDataset for every graph added to this GridChart
*
* @see Axis::addDataset
*/
private function configureAxisFromDatasets()
{
foreach ($this->graphs as $axis => &$graphs) {
$axisObj = $this->axis[$axis];
foreach ($graphs as &$graph) {
$axisObj->addDataset($graph);
}
}
}
/**
* Add an arbitrary number of lines to be drawn
*
* Refer to the graphs.md for a detailed list of allowed attributes
*
* @param array $axis,... The line definitions to draw
*
* @return $this Fluid interface
*/
public function drawLines(array $axis)
{
$this->draw(self::TYPE_LINE, func_get_args());
return $this;
}
/**
* Add arbitrary number of bars to be drawn
*
* Refer to the graphs.md for a detailed list of allowed attributes
*
* @param array $axis
* @return $this
*/
public function drawBars(array $axis)
{
$this->draw(self::TYPE_BAR, func_get_args());
return $this;
}
/**
* Generic method for adding elements to the drawing stack
*
* @param string $type The type of the element to draw (see TYPE_ constants in this class)
* @param array $data The data given to the draw call
*/
private function draw($type, $data)
{
$axisName = 'default';
if (is_string($data[0])) {
$axisName = $data[0];
array_shift($data);
}
foreach ($data as &$graph) {
$graph['graphType'] = $type;
if (isset($graph['stack'])) {
if (!isset($this->stacks[$graph['stack']])) {
$this->stacks[$graph['stack']] = new StackedGraph();
}
$this->stacks[$graph['stack']]->addGraph($graph);
$graph['stack'] = $this->stacks[$graph['stack']];
}
if (!isset($graph['color'])) {
$colorType = isset($graph['palette']) ? $graph['palette'] : Palette::NEUTRAL;
$graph['color'] = $this->palette->getNext($colorType);
}
$this->graphs[$axisName][] = $graph;
if ($this->legend) {
$this->legend->addDataset($graph);
}
}
$this->initTooltips($data);
}
private function initTooltips($data)
{
foreach ($data as &$graph) {
foreach ($graph['data'] as $x => $point) {
if (!array_key_exists($x, $this->tooltips)) {
$this->tooltips[$x] = new Tooltip(
array(
'color' => $graph['color'],
)
);
}
$this->tooltips[$x]->addDataPoint($point);
}
}
}
/**
* Set the label for the x and y axis
*
* @param string $xAxisLabel The label to use for the x axis
* @param string $yAxisLabel The label to use for the y axis
* @param string $axisName The name of the axis, for now 'default'
*
* @return $this Fluid interface
*/
public function setAxisLabel($xAxisLabel, $yAxisLabel, $axisName = 'default')
{
$this->axis[$axisName]->setXLabel($xAxisLabel)->setYLabel($yAxisLabel);
return $this;
}
/**
* Set the AxisUnit to use for calculating the values of the x axis
*
* @param AxisUnit $unit The unit for the x axis
* @param string $axisName The name of the axis to set the label for, currently only 'default'
*
* @return $this Fluid interface
*/
public function setXAxis(AxisUnit $unit, $axisName = 'default')
{
$this->axis[$axisName]->setUnitForXAxis($unit);
return $this;
}
/**
* Set the AxisUnit to use for calculating the values of the y axis
*
* @param AxisUnit $unit The unit for the y axis
* @param string $axisName The name of the axis to set the label for, currently only 'default'
*
* @return $this Fluid interface
*/
public function setYAxis(AxisUnit $unit, $axisName = 'default')
{
$this->axis[$axisName]->setUnitForYAxis($unit);
return $this;
}
/**
* Pre-render setup of the axis
*
* @see Chart::build
*/
protected function build()
{
$this->configureAxisFromDatasets();
}
/**
* Initialize the renderer and overwrite it with an 2:1 ration renderer
*/
protected function init()
{
$this->renderer = new SVGRenderer(100, 100);
$this->setAxis(Axis::createLinearAxis());
}
/**
* Overwrite the axis to use
*
* @param Axis $axis The new axis to use
* @param string $name The name of the axis, currently only 'default'
*
* @return $this Fluid interface
*/
public function setAxis(Axis $axis, $name = 'default')
{
$this->axis = array($name => $axis);
return $this;
}
/**
* Add an axis to this graph (not really supported right now)
*
* @param Axis $axis The axis object to add
* @param string $name The name of the axis
*
* @return $this Fluid interface
*/
public function addAxis(Axis $axis, $name)
{
$this->axis[$name] = $axis;
return $this;
}
/**
* Set minimum values for the x and y axis.
*
* Setting null to an axis means this will use a value determined by the dataset
*
* @param int $xMin The minimum value for the x axis or null to use a dynamic value
* @param int $yMin The minimum value for the y axis or null to use a dynamic value
* @param string $axisName The name of the axis to set the minimum, currently only 'default'
*
* @return $this Fluid interface
*/
public function setAxisMin($xMin = null, $yMin = null, $axisName = 'default')
{
$this->axis[$axisName]->setXMin($xMin)->setYMin($yMin);
return $this;
}
/**
* Set maximum values for the x and y axis.
*
* Setting null to an axis means this will use a value determined by the dataset
*
* @param int $xMax The maximum value for the x axis or null to use a dynamic value
* @param int $yMax The maximum value for the y axis or null to use a dynamic value
* @param string $axisName The name of the axis to set the maximum, currently only 'default'
*
* @return $this Fluid interface
*/
public function setAxisMax($xMax = null, $yMax = null, $axisName = 'default')
{
$this->axis[$axisName]->setXMax($xMax)->setYMax($yMax);
return $this;
}
/**
* Render this GridChart to SVG
*
* @param RenderContext $ctx The context to use for rendering
*
* @return DOMElement
*/
public function toSvg(RenderContext $ctx)
{
$outerBox = new Canvas('outerGraph', new LayoutBox(0, 0, 100, 100));
$innerBox = new Canvas('graph', new LayoutBox(0, 0, 95, 90));
$maxPadding = array(0,0,0,0);
foreach ($this->axis as $axis) {
$padding = $axis->getRequiredPadding();
for ($i=0; $i < count($padding); $i++) {
$maxPadding[$i] = max($maxPadding[$i], $padding[$i]);
}
$innerBox->addElement($axis);
}
$this->renderGraphContent($innerBox);
$innerBox->getLayout()->setPadding($maxPadding[0], $maxPadding[1], $maxPadding[2], $maxPadding[3]);
$this->createContentClipBox($innerBox);
$outerBox->addElement($innerBox);
if ($this->legend) {
$outerBox->addElement($this->legend);
}
return $outerBox->toSvg($ctx);
}
/**
* Create a clip box that defines which area of the graph is drawable and adds it to the graph.
*
* The clipbox has the id '#clip' and can be used in the clip-mask element
*
* @param Canvas $innerBox The inner canvas of the graph to add the clip box to
*/
private function createContentClipBox(Canvas $innerBox)
{
$clipBox = new Canvas('clip', new LayoutBox(0, 0, 100, 100));
$clipBox->toClipPath();
$innerBox->addElement($clipBox);
$rect = new Rect(0.1, 0, 100, 99.9);
$clipBox->addElement($rect);
}
/**
* Render the content of the graph, i.e. the draw stack
*
* @param Canvas $innerBox The inner canvas of the graph to add the content to
*/
private function renderGraphContent(Canvas $innerBox)
{
foreach ($this->graphs as $axisName => $graphs) {
$axis = $this->axis[$axisName];
$graphObj = null;
foreach ($graphs as $dataset => $graph) {
// determine the type and create a graph object for it
switch ($graph['graphType']) {
case self::TYPE_BAR:
$graphObj = new BarGraph(
$axis->transform($graph['data']),
$graphs,
$dataset,
$this->tooltips
);
break;
case self::TYPE_LINE:
$graphObj = new LineGraph(
$axis->transform($graph['data']),
$graphs,
$dataset,
$this->tooltips
);
break;
default:
continue;
}
$el = $this->setupGraph($graphObj, $graph);
if ($el) {
$innerBox->addElement($el);
}
}
}
}
/**
* Setup the provided Graph type
*
* @param mixed $graphObject The graph class, needs the setStyleFromConfig method
* @param array $graphConfig The configration array of the graph
*
* @return mixed Either the graph to be added or null if the graph is not directly added
* to the document (e.g. stacked graphs are added by
* the StackedGraph Composite object)
*/
private function setupGraph($graphObject, array $graphConfig)
{
$graphObject->setStyleFromConfig($graphConfig);
// When in a stack return the StackedGraph object instead of the graphObject
if (isset($graphConfig['stack'])) {
$graphConfig['stack']->addToStack($graphObject);
if (!$graphConfig['stack']->stackEmpty()) {
return $graphConfig['stack'];
}
// return no object when the graph should not be rendered
return null;
}
return $graphObject;
}
}
|