| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2019-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 GroupedBarTrait { |
| 25 |
|
| 26 |
use MultiGraphTrait; |
| 27 |
|
| 28 |
protected $group_bar_spacing; |
| 29 |
protected $dataset_offsets = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* Draws the bars |
| 33 |
*/ |
| 34 |
protected function drawBars() |
| 35 |
{ |
| 36 |
$this->barSetup(); |
| 37 |
$dataset_count = count($this->multi_graph); |
| 38 |
$datasets = $this->multi_graph->getEnabledDatasets(); |
| 39 |
|
| 40 |
// bars must be drawn from left to right, since they might overlap |
| 41 |
$bars = ''; |
| 42 |
$legend_entries = []; |
| 43 |
foreach($this->multi_graph as $bnum => $itemlist) { |
| 44 |
$item = $itemlist[0]; |
| 45 |
$bar_pos = $this->gridPosition($item, $bnum); |
| 46 |
if($bar_pos !== null) { |
| 47 |
for($j = 0; $j < $dataset_count; ++$j) { |
| 48 |
if(!in_array($j, $datasets)) |
| 49 |
continue; |
| 50 |
$item = $itemlist[$j]; |
| 51 |
$bars .= $this->drawBar($item, $bnum, 0, $this->datasetYAxis($j), $j); |
| 52 |
$legend_entries[$j][$bnum] = $item; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// legend entries are added in order of dataset |
| 58 |
foreach($legend_entries as $j => $dataset) |
| 59 |
foreach($dataset as $bnum => $item) |
| 60 |
$this->setBarLegendEntry($j, $bnum, $item); |
| 61 |
|
| 62 |
return $bars; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Sets up bar details |
| 67 |
*/ |
| 68 |
protected function barSetup() |
| 69 |
{ |
| 70 |
parent::barSetup(); |
| 71 |
$datasets = $this->multi_graph->getEnabledDatasets(); |
| 72 |
$dataset_count = count($datasets); |
| 73 |
|
| 74 |
list($chunk_width, $bspace, $chunk_unit_width) = |
| 75 |
$this->barPosition($this->getOption('bar_width'), $this->getOption('bar_width_min'), |
| 76 |
$this->x_axes[$this->main_x_axis]->unit(), $dataset_count, |
| 77 |
$this->getOption('bar_space'), $this->getOption('group_space')); |
| 78 |
$this->group_bar_spacing = $chunk_unit_width; |
| 79 |
$this->setBarWidth($chunk_width, $bspace); |
| 80 |
|
| 81 |
$offset = 0; |
| 82 |
foreach($datasets as $d) { |
| 83 |
$this->dataset_offsets[$d] = $offset; |
| 84 |
++$offset; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Fills in the x and width of bar |
| 90 |
*/ |
| 91 |
protected function barX($item, $index, &$bar, $axis, $dataset) |
| 92 |
{ |
| 93 |
$bar_x = $this->gridPosition($item, $index); |
| 94 |
if($bar_x === null) |
| 95 |
return null; |
| 96 |
|
| 97 |
$d_offset = $this->dataset_offsets[$dataset]; |
| 98 |
$bar['x'] = $bar_x + $this->calculated_bar_space + |
| 99 |
($d_offset * $this->group_bar_spacing); |
| 100 |
$bar['width'] = $this->calculated_bar_width; |
| 101 |
return $bar_x; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Calculates the bar width, gap to first bar, gap between bars |
| 106 |
* returns an array containing all three |
| 107 |
*/ |
| 108 |
static function barPosition($bar_width, $bar_width_min, $unit_width, |
| 109 |
$group_size, $bar_space, $group_space) |
| 110 |
{ |
| 111 |
if(is_numeric($bar_width) && $bar_width >= 1) { |
| 112 |
return self::barPositionFixed($bar_width, $unit_width, |
| 113 |
$group_size, $group_space); |
| 114 |
} else { |
| 115 |
// bar width dependent on space |
| 116 |
$gap_count = $group_size - 1; |
| 117 |
$gap = $gap_count > 0 ? $group_space : 0; |
| 118 |
|
| 119 |
$bar_width = $bar_space >= $unit_width ? '1' : $unit_width - $bar_space; |
| 120 |
if($gap_count > 0 && $gap * $gap_count > $bar_width - $group_size) |
| 121 |
$gap = ($bar_width - $group_size) / $gap_count; |
| 122 |
$bar_width = ($bar_width - ($gap * ($group_size - 1))) |
| 123 |
/ $group_size; |
| 124 |
|
| 125 |
if($bar_width < $bar_width_min) |
| 126 |
return self::barPositionFixed($bar_width_min, $unit_width, |
| 127 |
$group_size, $group_space); |
| 128 |
$spacing = $bar_width + $gap; |
| 129 |
$offset = $bar_space / 2; |
| 130 |
} |
| 131 |
return [$bar_width, $offset, $spacing]; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Calculate bar width, gaps, using fixed bar width |
| 136 |
*/ |
| 137 |
static function barPositionFixed($bar_width, $unit_width, $group_size, |
| 138 |
$group_space) |
| 139 |
{ |
| 140 |
$gap = $group_size > 1 ? $group_space : 0; |
| 141 |
if($group_size > 1 && ($bar_width + $gap) * $group_size > $unit_width) { |
| 142 |
|
| 143 |
// bars don't fit with group_space option, so they must overlap |
| 144 |
// (and make sure the bars are at least 1 pixel apart) |
| 145 |
$spacing = max(1, ($unit_width - $bar_width) / ($group_size - 1)); |
| 146 |
$offset = 0; |
| 147 |
} else { |
| 148 |
// space the bars group_space apart, centred in unit space |
| 149 |
$spacing = $bar_width + $gap; |
| 150 |
$offset = max(0, ($unit_width - ($spacing * $group_size)) / 2); |
| 151 |
} |
| 152 |
return [$bar_width, $offset, $spacing]; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|