| 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 PolarAreaTrait { |
| 25 |
|
| 26 |
protected $slice_angle; |
| 27 |
protected $radius_factor_x; |
| 28 |
protected $radius_factor_y; |
| 29 |
|
| 30 |
/** |
| 31 |
* Sets up the polar graph details |
| 32 |
*/ |
| 33 |
protected function calc() |
| 34 |
{ |
| 35 |
parent::calc(); |
| 36 |
|
| 37 |
$dt = $this->getOption('datetime_keys'); |
| 38 |
$max_value = $this->values->getMaxValue($this->dataset); |
| 39 |
$num_values = $dt ? $this->values->itemsCount($this->dataset) : |
| 40 |
$this->values->getMaxKey($this->dataset) + 1; |
| 41 |
$smax = sqrt($max_value); |
| 42 |
$this->radius_factor_x = $this->radius_x / $smax; |
| 43 |
$this->radius_factor_y = $this->radius_y / $smax; |
| 44 |
$this->slice_angle = 2.0 * M_PI / $num_values; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Sets up the angles and radii for slice |
| 49 |
*/ |
| 50 |
protected function getSliceInfo($num, $item, &$angle_start, &$angle_end, |
| 51 |
&$radius_x, &$radius_y) |
| 52 |
{ |
| 53 |
$angle_start = $num * $this->slice_angle; |
| 54 |
$angle_end = ($num + 1) * $this->slice_angle; |
| 55 |
|
| 56 |
$radius_x = $radius_y = 0; |
| 57 |
if($item->value) { |
| 58 |
$sval = sqrt((float)$item->value); |
| 59 |
$radius_x = $this->radius_factor_x * $sval; |
| 60 |
$radius_y = $this->radius_factor_y * $sval; |
| 61 |
} |
| 62 |
return true; |
| 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 |
if(!isset($this->slice_info[$index])) |
| 72 |
return ['middle centre', [$x, $y] ]; |
| 73 |
|
| 74 |
$ac = $this->slice_info[$index]->midAngle(); |
| 75 |
$ab = $ac - $this->slice_info[$index]->start_angle; |
| 76 |
$ac += $this->s_angle; |
| 77 |
$sin_ac = sin($ac); |
| 78 |
$cos_ac = cos($ac); |
| 79 |
$rx = $this->slice_info[$index]->radius_x; |
| 80 |
$ry = $this->slice_info[$index]->radius_y; |
| 81 |
|
| 82 |
$x1 = $label_w / 2; |
| 83 |
$y1 = $label_h / 2; |
| 84 |
$t_radius = sqrt(pow($x1, 2) + pow($y1, 2)); |
| 85 |
|
| 86 |
// see if the text fits in the slice |
| 87 |
$pos_radius = $this->getOption('label_position'); |
| 88 |
$r1 = $pos_radius * $rx; |
| 89 |
$outside = false; |
| 90 |
if(sin($ab) * $r1 > $t_radius) { |
| 91 |
// place it at the label_position distance from centre |
| 92 |
$xc = $pos_radius * $rx * $cos_ac; |
| 93 |
$yc = $pos_radius * $ry * $sin_ac; |
| 94 |
} else { |
| 95 |
// find min distance that label fits in |
| 96 |
$h = $t_radius / sin($ab); |
| 97 |
$xch = $h * $cos_ac * $this->radius_x / $this->radius_y; |
| 98 |
$ych = $h * $sin_ac; |
| 99 |
$xcr = ($rx + $t_radius) * $cos_ac; |
| 100 |
$ycr = ($ry + $t_radius) * $sin_ac; |
| 101 |
$xmax = ($this->radius_x + $t_radius) * $cos_ac; |
| 102 |
$ymax = ($this->radius_y + $t_radius) * $sin_ac; |
| 103 |
if(abs($xcr) > abs($xch) || abs($ycr) > abs($ych)) { |
| 104 |
$xc = $xcr; |
| 105 |
$yc = $ycr; |
| 106 |
} else { |
| 107 |
$xc = $xch; |
| 108 |
$yc = $ych; |
| 109 |
} |
| 110 |
// if the slice angle is very acute, prevent label going too far out |
| 111 |
if(abs($xmax) < abs($xc) || abs($ymax) < abs($yc)) { |
| 112 |
$xc = $xmax; |
| 113 |
$yc = $ymax; |
| 114 |
} |
| 115 |
$outside = true; |
| 116 |
} |
| 117 |
if($this->getOption('reverse')) |
| 118 |
$yc = -$yc; |
| 119 |
|
| 120 |
$space = 0; |
| 121 |
$multiplier = 0.5; |
| 122 |
if($pos_radius > 1 || $outside) { |
| 123 |
$space = $this->getOption(['data_label_space', $dataset]); |
| 124 |
$multiplier = 1; |
| 125 |
} |
| 126 |
$xt = ($rx + $space) * $multiplier * $cos_ac; |
| 127 |
$yt = ($this->getOption('reverse') ? -1 : 1) * ($ry + $space) * $multiplier * $sin_ac; |
| 128 |
$target = [$x + $xt, $y + $yt]; |
| 129 |
$position = new Number($xc) . ' ' . new Number($yc); |
| 130 |
return [$position, $target]; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|