| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2017-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 |
class PieExploder { |
| 25 |
|
| 26 |
protected $graph; |
| 27 |
protected $smallest_value; |
| 28 |
protected $largest_value; |
| 29 |
protected $explode; |
| 30 |
protected $explode_amount; |
| 31 |
protected $reverse; |
| 32 |
protected $auto_aspect; |
| 33 |
protected $radius_x; |
| 34 |
protected $radius_y; |
| 35 |
|
| 36 |
public function __construct(&$graph, $smallest, $largest) |
| 37 |
{ |
| 38 |
$this->graph =& $graph; |
| 39 |
$this->smallest_value = $smallest; |
| 40 |
$this->largest_value = $largest; |
| 41 |
$this->explode = $graph->getOption('explode'); |
| 42 |
$this->reverse = $graph->getOption('reverse'); |
| 43 |
$amount = $graph->getOption('explode_amount'); |
| 44 |
$this->explode_amount = is_numeric($amount) ? $amount : 20; |
| 45 |
$this->auto_aspect = ($graph->getOption('aspect_ratio') == 'auto'); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Reduces the radii to fit the exploded portion |
| 50 |
*/ |
| 51 |
public function fixRadii(&$radius_x, &$radius_y) |
| 52 |
{ |
| 53 |
$this->explode_amount = min($radius_x - 10, $radius_y - 10, |
| 54 |
max(2, (int)$this->explode_amount)); |
| 55 |
if($this->auto_aspect && $radius_x != $radius_y) { |
| 56 |
$mx = $my = 1.0; |
| 57 |
if($radius_x > $radius_y) { |
| 58 |
$my = $radius_y / $radius_x; |
| 59 |
} else { |
| 60 |
$mx = $radius_x / $radius_y; |
| 61 |
} |
| 62 |
$radius_x -= $this->explode_amount * $mx; |
| 63 |
$radius_y -= $this->explode_amount * $my; |
| 64 |
} else { |
| 65 |
$radius_x -= $this->explode_amount; |
| 66 |
$radius_y -= $this->explode_amount; |
| 67 |
} |
| 68 |
$this->radius_x = $radius_x; |
| 69 |
$this->radius_y = $radius_y; |
| 70 |
return $this->explode_amount; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Returns the x,y offset caused by explosion |
| 75 |
*/ |
| 76 |
public function getExplode($item, $angle_start, $angle_end) |
| 77 |
{ |
| 78 |
if($item === null) |
| 79 |
return [0,0]; |
| 80 |
$iamt = $item->explode; |
| 81 |
if($iamt !== null) { |
| 82 |
$amt = $iamt; |
| 83 |
} else { |
| 84 |
$range = $this->largest_value - $this->smallest_value; |
| 85 |
switch($this->explode) { |
| 86 |
case 'none' : |
| 87 |
$amt = 0; |
| 88 |
break; |
| 89 |
case 'all' : |
| 90 |
$amt = 1; |
| 91 |
break; |
| 92 |
case 'large' : |
| 93 |
$amt = $range <= 0 ? 0 : ($item->value - $this->smallest_value) / $range; |
| 94 |
break; |
| 95 |
default : |
| 96 |
$amt = $range <= 0 ? 0 : ($this->largest_value - $item->value) / $range; |
| 97 |
} |
| 98 |
} |
| 99 |
$explode = $this->explode_amount * $amt; |
| 100 |
$explode_direction = $angle_start + ($angle_end - $angle_start) * 0.5; |
| 101 |
$xo = $explode * cos($explode_direction); |
| 102 |
$yo = $explode * sin($explode_direction); |
| 103 |
|
| 104 |
$aspect = $this->radius_y / $this->radius_x; |
| 105 |
if($aspect < 1.0) |
| 106 |
$yo *= $aspect; |
| 107 |
elseif($aspect > 1.0) |
| 108 |
$xo /= $aspect; |
| 109 |
if($this->reverse) |
| 110 |
$yo = -$yo; |
| 111 |
|
| 112 |
return [$xo, $yo]; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|