| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2019 Graham Breach |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Lesser General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
/** |
| 19 |
* For more information, please contact <graham@goat1000.com> |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Goat1000\SVGGraph; |
| 23 |
|
| 24 |
trait FloatingBarTrait { |
| 25 |
|
| 26 |
private $min_value = null; |
| 27 |
private $max_value = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns an array with x, y, width and height set |
| 31 |
*/ |
| 32 |
protected function barDimensions($item, $index, $start, $axis, $dataset) |
| 33 |
{ |
| 34 |
$bar = []; |
| 35 |
$bar_x = $this->barX($item, $index, $bar, $axis, $dataset); |
| 36 |
if($bar_x === null) |
| 37 |
return []; |
| 38 |
|
| 39 |
$start = $item->value; |
| 40 |
$value = $item->end - $start; |
| 41 |
$y_pos = $this->barY($value, $bar, $start, $axis); |
| 42 |
if($y_pos === null) |
| 43 |
return []; |
| 44 |
return $bar; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Override to replace value |
| 49 |
*/ |
| 50 |
protected function setTooltip(&$element, &$item, $dataset, $key, $value = null, |
| 51 |
$duplicate = false) |
| 52 |
{ |
| 53 |
$value = $item->end - $item->value; |
| 54 |
return parent::setTooltip($element, $item, $dataset, $key, $value, $duplicate); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns the maximum bar end |
| 59 |
*/ |
| 60 |
public function getMaxValue() |
| 61 |
{ |
| 62 |
if($this->max_value !== null) |
| 63 |
return $this->max_value; |
| 64 |
$max = null; |
| 65 |
foreach($this->values[0] as $item) { |
| 66 |
$s = $item->value; |
| 67 |
$e = $item->end; |
| 68 |
if($s === null || $e === null) |
| 69 |
continue; |
| 70 |
$m = max($s, $e); |
| 71 |
if($max === null || $m > $max) |
| 72 |
$max = $m; |
| 73 |
} |
| 74 |
return ($this->max_value = $max); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns the minimum bar end |
| 79 |
*/ |
| 80 |
public function getMinValue() |
| 81 |
{ |
| 82 |
if($this->min_value !== null) |
| 83 |
return $this->min_value; |
| 84 |
$min = null; |
| 85 |
foreach($this->values[0] as $item) { |
| 86 |
$s = $item->value; |
| 87 |
$e = $item->end; |
| 88 |
if($s === null || $e === null) |
| 89 |
continue; |
| 90 |
$m = min($s, $e); |
| 91 |
if($min === null || $m < $min) |
| 92 |
$min = $m; |
| 93 |
} |
| 94 |
return ($this->min_value = $min); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns TRUE if the item is visible on the graph |
| 99 |
*/ |
| 100 |
public function isVisible($item, $dataset = 0) |
| 101 |
{ |
| 102 |
if($item->value === null || $item->end === null) |
| 103 |
return false; |
| 104 |
return ($item->end - $item->value != 0); |
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
|