/usr/share/php/Icinga/Chart/Axis.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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | <?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Chart;
use DOMElement;
use Icinga\Chart\Primitive\Drawable;
use Icinga\Chart\Primitive\Line;
use Icinga\Chart\Primitive\Text;
use Icinga\Chart\Render\RenderContext;
use Icinga\Chart\Render\Rotator;
use Icinga\Chart\Unit\AxisUnit;
use Icinga\Chart\Unit\CalendarUnit;
use Icinga\Chart\Unit\LinearUnit;
/**
* Axis class for the GridChart class.
*
* Implements drawing functions for the axis and its labels but delegates tick and label calculations
* to the AxisUnit implementations
*
* @see GridChart
* @see AxisUnit
*/
class Axis implements Drawable
{
/**
* Draw the label text horizontally
*/
const LABEL_ROTATE_HORIZONTAL = 'normal';
/**
* Draw the label text diagonally
*/
const LABEL_ROTATE_DIAGONAL = 'diagonal';
/**
* Whether to draw the horizontal lines for the background grid
*
* @var bool
*/
private $drawXGrid = true;
/**
* Whether to draw the vertical lines for the background grid
*
* @var bool
*/
private $drawYGrid = true;
/**
* The label for the x axis
*
* @var string
*/
private $xLabel = "";
/**
* The label for the y axis
*
* @var string
*/
private $yLabel = "";
/**
* The AxisUnit implementation to use for calculating the ticks for the x axis
*
* @var AxisUnit
*/
private $xUnit = null;
/**
* The AxisUnit implementation to use for calculating the ticks for the y axis
*
* @var AxisUnit
*/
private $yUnit = null;
/**
* The minimum amount of units each step must take up
*
* @var int
*/
public $minUnitsPerStep = 80;
/**
* The minimum amount of units each tick must take up
*
* @var int
*/
public $minUnitsPerTick = 15;
/**
* If the displayed labels should be aligned horizontally or diagonally
*/
protected $labelRotationStyle = self::LABEL_ROTATE_HORIZONTAL;
/**
* Inform the axis about an added dataset
*
* This is especially needed when one or more AxisUnit implementations dynamically define
* their min or max values, as this is the point where they detect the min and max value
* from the datasets
*
* @param array $dataset An dataset to respect on axis generation
*/
public function addDataset(array $dataset)
{
$this->xUnit->addValues($dataset, 0);
$this->yUnit->addValues($dataset, 1);
}
/**
* Set the AxisUnit implementation to use for generating the x axis
*
* @param AxisUnit $unit The AxisUnit implementation to use for the x axis
*
* @return $this This Axis Object
* @see Axis::CalendarUnit
* @see Axis::LinearUnit
*/
public function setUnitForXAxis(AxisUnit $unit)
{
$this->xUnit = $unit;
return $this;
}
/**
* Set the AxisUnit implementation to use for generating the y axis
*
* @param AxisUnit $unit The AxisUnit implementation to use for the y axis
*
* @return $this This Axis Object
* @see Axis::CalendarUnit
* @see Axis::LinearUnit
*/
public function setUnitForYAxis(AxisUnit $unit)
{
$this->yUnit = $unit;
return $this;
}
/**
* Return the padding this axis requires
*
* @return array An array containing the padding for all sides
*/
public function getRequiredPadding()
{
return array(10, 5, 15, 10);
}
/**
* Render the horizontal axis
*
* @param RenderContext $ctx The context to use for rendering
* @param DOMElement $group The DOMElement this axis will be added to
*/
private function renderHorizontalAxis(RenderContext $ctx, DOMElement $group)
{
$steps = $this->ticksPerX($this->xUnit->getTicks(), $ctx->getNrOfUnitsX(), $this->minUnitsPerStep);
$ticks = $this->ticksPerX($this->xUnit->getTicks(), $ctx->getNrOfUnitsX(), $this->minUnitsPerTick);
// Steps should always be ticks
if ($ticks !== $steps) {
$steps = $ticks * 5;
}
// Check whether there is enough room for regular labels
$labelRotationStyle = $this->labelRotationStyle;
if ($this->labelsOversized($this->xUnit, 6)) {
$labelRotationStyle = self::LABEL_ROTATE_DIAGONAL;
}
/*
$line = new Line(0, 100, 100, 100);
$line->setStrokeWidth(2);
$group->appendChild($line->toSvg($ctx));
*/
// contains the approximate end position of the last label
$lastLabelEnd = -1;
$shift = 0;
$i = 0;
foreach ($this->xUnit as $label => $pos) {
if ($i % $ticks === 0) {
/*
$tick = new Line($pos, 100, $pos, 101);
$group->appendChild($tick->toSvg($ctx));
*/
}
if ($i % $steps === 0) {
if ($labelRotationStyle === self::LABEL_ROTATE_HORIZONTAL) {
// If the last label would overlap this label we shift the y axis a bit
if ($lastLabelEnd > $pos) {
$shift = ($shift + 5) % 10;
} else {
$shift = 0;
}
}
$labelField = new Text($pos + 0.5, ($this->xLabel ? 107 : 105) + $shift, $label);
if ($labelRotationStyle === self::LABEL_ROTATE_HORIZONTAL) {
$labelField->setAlignment(Text::ALIGN_MIDDLE)
->setFontSize('2.5em');
} else {
$labelField->setFontSize('2.5em');
}
if ($labelRotationStyle === self::LABEL_ROTATE_DIAGONAL) {
$labelField = new Rotator($labelField, 45);
}
$labelField = $labelField->toSvg($ctx);
$group->appendChild($labelField);
if ($this->drawYGrid) {
$bgLine = new Line($pos, 0, $pos, 100);
$bgLine->setStrokeWidth(0.5)
->setStrokeColor('#BFBFBF');
$group->appendChild($bgLine->toSvg($ctx));
}
$lastLabelEnd = $pos + strlen($label) * 1.2;
}
$i++;
}
}
/**
* Render the vertical axis
*
* @param RenderContext $ctx The context to use for rendering
* @param DOMElement $group The DOMElement this axis will be added to
*/
private function renderVerticalAxis(RenderContext $ctx, DOMElement $group)
{
$steps = $this->ticksPerX($this->yUnit->getTicks(), $ctx->getNrOfUnitsY(), $this->minUnitsPerStep);
$ticks = $this->ticksPerX($this->yUnit->getTicks(), $ctx->getNrOfUnitsY(), $this->minUnitsPerTick);
// Steps should always be ticks
if ($ticks !== $steps) {
$steps = $ticks * 5;
}
/*
$line = new Line(0, 0, 0, 100);
$line->setStrokeWidth(2);
$group->appendChild($line->toSvg($ctx));
*/
$i = 0;
foreach ($this->yUnit as $label => $pos) {
$pos = 100 - $pos;
if ($i % $ticks === 0) {
// draw a tick
//$tick = new Line(0, $pos, -1, $pos);
//$group->appendChild($tick->toSvg($ctx));
}
if ($i % $steps === 0) {
// draw a step
$labelField = new Text(-0.5, $pos + 0.5, $label);
$labelField->setFontSize('2.5em')
->setAlignment(Text::ALIGN_END);
$group->appendChild($labelField->toSvg($ctx));
if ($this->drawXGrid) {
$bgLine = new Line(0, $pos, 100, $pos);
$bgLine->setStrokeWidth(0.5)
->setStrokeColor('#BFBFBF');
$group->appendChild($bgLine->toSvg($ctx));
}
}
$i++;
}
if ($this->yLabel || $this->xLabel) {
if ($this->yLabel && $this->xLabel) {
$txt = $this->yLabel . ' / ' . $this->xLabel;
} else if ($this->xLabel) {
$txt = $this->xLabel;
} else {
$txt = $this->yLabel;
}
$axisLabel = new Text(50, -3, $txt);
$axisLabel->setFontSize('2em')
->setFontWeight('bold')
->setAlignment(Text::ALIGN_MIDDLE);
$group->appendChild($axisLabel->toSvg($ctx));
}
}
/**
* Factory method, create an Axis instance using Linear ticks as the unit
*
* @return Axis The axis that has been created
* @see LinearUnit
*/
public static function createLinearAxis()
{
$axis = new Axis();
$axis->setUnitForXAxis(self::linearUnit());
$axis->setUnitForYAxis(self::linearUnit());
return $axis;
}
/**
* Set the label for the x axis
*
* An empty string means 'no label'.
*
* @param string $label The label to use for the x axis
*
* @return $this Fluid interface
*/
public function setXLabel($label)
{
$this->xLabel = $label;
return $this;
}
/**
* Set the label for the y axis
*
* An empty string means 'no label'.
*
* @param string $label The label to use for the y axis
*
* @return $this Fluid interface
*/
public function setYLabel($label)
{
$this->yLabel = $label;
return $this;
}
/**
* Set the labels minimum value for the x axis
*
* Setting the value to null let's the axis unit decide which value to use for the minimum
*
* @param int $xMin The minimum value to use for the x axis
*
* @return $this Fluid interface
*/
public function setXMin($xMin)
{
$this->xUnit->setMin($xMin);
return $this;
}
/**
* Set the labels minimum value for the y axis
*
* Setting the value to null let's the axis unit decide which value to use for the minimum
*
* @param int $yMin The minimum value to use for the x axis
*
* @return $this Fluid interface
*/
public function setYMin($yMin)
{
$this->yUnit->setMin($yMin);
return $this;
}
/**
* Set the labels maximum value for the x axis
*
* Setting the value to null let's the axis unit decide which value to use for the maximum
*
* @param int $xMax The minimum value to use for the x axis
*
* @return $this Fluid interface
*/
public function setXMax($xMax)
{
$this->xUnit->setMax($xMax);
return $this;
}
/**
* Set the labels maximum value for the y axis
*
* Setting the value to null let's the axis unit decide which value to use for the maximum
*
* @param int $yMax The minimum value to use for the y axis
*
* @return $this Fluid interface
*/
public function setYMax($yMax)
{
$this->yUnit->setMax($yMax);
return $this;
}
/**
* Transform all coordinates of the given dataset to coordinates that fit the graph's coordinate system
*
* @param array $dataSet The absolute coordinates as provided in the draw call
*
* @return array A graph relative representation of the given coordinates
*/
public function transform(array &$dataSet)
{
$result = array();
foreach ($dataSet as &$points) {
$result[] = array(
$this->xUnit->transform($points[0]),
100 - $this->yUnit->transform($points[1])
);
}
return $result;
}
/**
* Create an AxisUnit that can be used in the axis to represent timestamps
*
* @return CalendarUnit
*/
public static function calendarUnit()
{
return new CalendarUnit();
}
/**
* Create an AxisUnit that can be used in the axis to represent a dataset as equally distributed
* ticks
*
* @param int $ticks
* @return LinearUnit
*/
public static function linearUnit($ticks = 10)
{
return new LinearUnit($ticks);
}
/**
* Return the SVG representation of this object
*
* @param RenderContext $ctx The context to use for calculations
*
* @return DOMElement
* @see Drawable::toSvg
*/
public function toSvg(RenderContext $ctx)
{
$group = $ctx->getDocument()->createElement('g');
$this->renderHorizontalAxis($ctx, $group);
$this->renderVerticalAxis($ctx, $group);
return $group;
}
protected function ticksPerX($ticks, $units, $min)
{
$per = 1;
while ($per * $units / $ticks < $min) {
$per++;
}
return $per;
}
/**
* Returns whether at least one label of the given Axis
* is bigger than the given maxLength
*
* @param AxisUnit $axis The axis that contains the labels that will be checked
*
* @return boolean Whether at least one label is bigger than maxLength
*/
private function labelsOversized(AxisUnit $axis, $maxLength = 5)
{
$oversized = false;
foreach ($axis as $label => $pos) {
if (strlen($label) > $maxLength) {
$oversized = true;
}
}
return $oversized;
}
}
|