| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2022 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 HorizontalBarGraphTrait { |
| 25 |
|
| 26 |
use BarGraphTrait; |
| 27 |
|
| 28 |
public function __construct($w, $h, array $settings, array $fixed_settings = []) |
| 29 |
{ |
| 30 |
// backwards compatibility |
| 31 |
if(isset($settings['show_bar_labels']) && !isset($settings['show_data_labels'])) |
| 32 |
$settings['show_data_labels'] = $settings['show_bar_labels']; |
| 33 |
|
| 34 |
$fs = ['label_centre' => !isset($settings['datetime_keys'])]; |
| 35 |
$fs = array_merge($fs, $fixed_settings); |
| 36 |
parent::__construct($w, $h, $settings, $fs); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns the height of a bar rectangle |
| 41 |
*/ |
| 42 |
protected function barWidth() |
| 43 |
{ |
| 44 |
if(is_numeric($this->getOption('bar_width')) && $this->getOption('bar_width') >= 1) |
| 45 |
return $this->getOption('bar_width'); |
| 46 |
$unit_h = $this->y_axes[$this->main_y_axis]->unit(); |
| 47 |
$bh = $unit_h - $this->getOption('bar_space'); |
| 48 |
return max(1, $bh, $this->getOption('bar_width_min')); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Fills in the x-position and width of a bar |
| 53 |
* @param number $value bar value |
| 54 |
* @param array &$bar bar element array [out] |
| 55 |
* @param number $start bar start value |
| 56 |
* @return number unclamped bar position |
| 57 |
*/ |
| 58 |
protected function barY($value, &$bar, $start = null) |
| 59 |
{ |
| 60 |
if($start) |
| 61 |
$value += $start; |
| 62 |
|
| 63 |
$startpos = $start === null ? $this->originX() : $this->gridX($start); |
| 64 |
if($startpos === null) |
| 65 |
$startpos = $this->originX(); |
| 66 |
$pos = $this->gridX($value); |
| 67 |
if($pos === null) { |
| 68 |
$bar['width'] = 0; |
| 69 |
} else { |
| 70 |
$l1 = $this->clampHorizontal($startpos); |
| 71 |
$l2 = $this->clampHorizontal($pos); |
| 72 |
$bar['x'] = min($l1, $l2); |
| 73 |
$bar['width'] = abs($l1-$l2); |
| 74 |
} |
| 75 |
return $pos; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Fills in the y and height of bar |
| 80 |
*/ |
| 81 |
protected function barX($item, $index, &$bar, $axis, $dataset) |
| 82 |
{ |
| 83 |
$bar_y = $this->gridPosition($item, $index); |
| 84 |
if($bar_y === null) |
| 85 |
return null; |
| 86 |
|
| 87 |
$axis = $this->y_axes[$this->main_y_axis]; |
| 88 |
if($axis->reversed()) |
| 89 |
$bar['y'] = $bar_y - $this->calculated_bar_space - $this->calculated_bar_width; |
| 90 |
else |
| 91 |
$bar['y'] = $bar_y + $this->calculated_bar_space; |
| 92 |
$bar['height'] = $this->calculated_bar_width; |
| 93 |
return $bar_y; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Returns the space before a bar |
| 98 |
*/ |
| 99 |
protected function barSpace($bar_width) |
| 100 |
{ |
| 101 |
return max(0, ($this->y_axes[$this->main_y_axis]->unit() - $bar_width) / 2); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Override to check minimum space requirement |
| 106 |
*/ |
| 107 |
protected function addDataLabel($dataset, $index, &$element, &$item, |
| 108 |
$x, $y, $w, $h, $content = null, $duplicate = true) |
| 109 |
{ |
| 110 |
if($w < $this->getOption(['data_label_min_space', $dataset])) |
| 111 |
return false; |
| 112 |
return parent::addDataLabel($dataset, $index, $element, $item, $x, $y, |
| 113 |
$w, $h, $content, $duplicate); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Returns the position for a data label |
| 118 |
*/ |
| 119 |
public function dataLabelPosition($dataset, $index, &$item, $x, $y, $w, $h, |
| 120 |
$label_w, $label_h) |
| 121 |
{ |
| 122 |
list($pos, $target) = parent::dataLabelPosition($dataset, $index, $item, |
| 123 |
$x, $y, $w, $h, $label_w, $label_h); |
| 124 |
$bpos = $this->getOption('bar_label_position'); |
| 125 |
if(!empty($bpos)) |
| 126 |
$pos = $bpos; |
| 127 |
|
| 128 |
if($label_w > $w && Graph::isPositionInside($pos)) |
| 129 |
$pos = str_replace(['left','centre','right'], 'outside right inside', $pos); |
| 130 |
|
| 131 |
// flip sides for negative values |
| 132 |
if($item !== null && $item->value < 0) { |
| 133 |
if(strpos($pos, 'right') !== false) |
| 134 |
$pos = str_replace('right', 'left', $pos); |
| 135 |
elseif(strpos($pos, 'left') !== false) |
| 136 |
$pos = str_replace('left', 'right', $pos); |
| 137 |
} |
| 138 |
return [$pos, $target]; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns the ordering for legend entries |
| 143 |
*/ |
| 144 |
public function getLegendOrder() |
| 145 |
{ |
| 146 |
return 'reverse'; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|