| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2021-2023 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 ExplodedPieGraphTrait { |
| 25 |
|
| 26 |
protected $pie_exploder = null; |
| 27 |
protected $explode_amount = 0; |
| 28 |
|
| 29 |
/** |
| 30 |
* Calculates reduced radius of pie |
| 31 |
*/ |
| 32 |
protected function calc() |
| 33 |
{ |
| 34 |
parent::calc(); |
| 35 |
$this->explode_amount = $this->pie_exploder->fixRadii($this->radius_x, |
| 36 |
$this->radius_y); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns a single slice of pie |
| 41 |
*/ |
| 42 |
protected function getSlice($item, $angle_start, $angle_end, $radius_x, |
| 43 |
$radius_y, &$attr, $single_slice, $colour_index) |
| 44 |
{ |
| 45 |
if($single_slice) |
| 46 |
return parent::getSlice($item, $angle_start, $angle_end, $radius_x, |
| 47 |
$radius_y, $attr, $single_slice, $colour_index); |
| 48 |
|
| 49 |
// find and apply explosiveness |
| 50 |
list($xo, $yo) = $this->pie_exploder->getExplode($item, $angle_start + |
| 51 |
$this->s_angle, $angle_end + $this->s_angle); |
| 52 |
|
| 53 |
$translated = $attr; |
| 54 |
$xform = new Transform; |
| 55 |
$xform->translate($xo, $yo); |
| 56 |
if(isset($translated['transform'])) |
| 57 |
$translated['transform']->add($xform); |
| 58 |
else |
| 59 |
$translated['transform'] = $xform; |
| 60 |
return parent::getSlice($item, $angle_start, $angle_end, $radius_x, |
| 61 |
$radius_y, $translated, $single_slice, $colour_index); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* Returns the position for the label |
| 67 |
*/ |
| 68 |
public function dataLabelPosition($dataset, $index, &$item, $x, $y, $w, $h, |
| 69 |
$label_w, $label_h) |
| 70 |
{ |
| 71 |
list($pos, $target) = parent::dataLabelPosition($dataset, $index, $item, |
| 72 |
$x, $y, $w, $h, $label_w, $label_h); |
| 73 |
|
| 74 |
if(isset($this->slice_info[$index])) { |
| 75 |
list($xo, $yo) = $this->pie_exploder->getExplode($item, |
| 76 |
$this->slice_info[$index]->start_angle + $this->s_angle, |
| 77 |
$this->slice_info[$index]->end_angle + $this->s_angle); |
| 78 |
|
| 79 |
list($x1, $y1) = explode(' ', $pos); |
| 80 |
if(is_numeric($x1) && is_numeric($y1)) { |
| 81 |
$x1 += $xo; |
| 82 |
$y1 += $yo; |
| 83 |
} else { |
| 84 |
$x1 = $xo; |
| 85 |
$y1 = $yo; |
| 86 |
} |
| 87 |
|
| 88 |
// explode target position too |
| 89 |
$target[0] += $xo; |
| 90 |
$target[1] += $yo; |
| 91 |
|
| 92 |
$pos = new Number($x1) . ' ' . new Number($y1); |
| 93 |
} else { |
| 94 |
$pos = 'middle centre'; |
| 95 |
} |
| 96 |
return [$pos, $target]; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Checks that the data are valid |
| 101 |
*/ |
| 102 |
protected function checkValues() |
| 103 |
{ |
| 104 |
parent::checkValues(); |
| 105 |
|
| 106 |
$largest = $this->getMaxValue(); |
| 107 |
$smallest = $largest; |
| 108 |
|
| 109 |
// want smallest non-0 value |
| 110 |
foreach($this->values[0] as $item) |
| 111 |
if($item->value < $smallest) |
| 112 |
$smallest = $item->value; |
| 113 |
|
| 114 |
$this->pie_exploder = new PieExploder($this, $smallest, $largest); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|