DataTrait.php
2 years ago
FormatTrait.php
6 months ago
Picture.php
6 months ago
PointTrait.php
2 years ago
StyleTrait.php
2 years ago
FormatTrait.php
204 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Davaxi\Sparkline; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | /** |
| 7 | * Trait FormatTrait. |
| 8 | */ |
| 9 | trait FormatTrait |
| 10 | { |
| 11 | /** |
| 12 | * @var int |
| 13 | * Recommended: 50 < 800 |
| 14 | */ |
| 15 | protected $width = 80; |
| 16 | /** |
| 17 | * @var int |
| 18 | * Recommended: 20 < 800 |
| 19 | */ |
| 20 | protected $height = 20; |
| 21 | /** |
| 22 | * @var int |
| 23 | */ |
| 24 | protected $ratioComputing = 4; |
| 25 | /** |
| 26 | * @var array |
| 27 | */ |
| 28 | protected $padding = ['top' => 0, 'right' => 0, 'bottom' => 0, 'left' => 0]; |
| 29 | /** |
| 30 | * @param string $format (Width x Height) |
| 31 | */ |
| 32 | public function setFormat(string $format) |
| 33 | { |
| 34 | $values = explode('x', $format); |
| 35 | if (count($values) !== static::FORMAT_DIMENSION) { |
| 36 | throw new InvalidArgumentException('Invalid format params. Expected string Width x Height'); |
| 37 | } |
| 38 | $this->setWidth($values[0]); |
| 39 | $this->setHeight($values[1]); |
| 40 | } |
| 41 | /** |
| 42 | * @param int $width |
| 43 | */ |
| 44 | public function setWidth(int $width) |
| 45 | { |
| 46 | $this->width = $width; |
| 47 | } |
| 48 | /** |
| 49 | * @param int $height |
| 50 | */ |
| 51 | public function setHeight(int $height) |
| 52 | { |
| 53 | $this->height = $height; |
| 54 | } |
| 55 | /** |
| 56 | * Set padding : format top right bottom left |
| 57 | * ex: 0 10 0 10. |
| 58 | * |
| 59 | * @param string $padding |
| 60 | */ |
| 61 | public function setPadding(string $padding) |
| 62 | { |
| 63 | list($top, $right, $bottom, $left) = $this->paddingStringToArray($padding); |
| 64 | $this->padding['top'] = $top; |
| 65 | $this->padding['right'] = $right; |
| 66 | $this->padding['bottom'] = $bottom; |
| 67 | $this->padding['left'] = $left; |
| 68 | } |
| 69 | /** |
| 70 | * @return int |
| 71 | */ |
| 72 | protected function getNormalizedHeight() : int |
| 73 | { |
| 74 | return $this->height * $this->ratioComputing; |
| 75 | } |
| 76 | /** |
| 77 | * @return int |
| 78 | */ |
| 79 | protected function getInnerHeight() : int |
| 80 | { |
| 81 | return $this->height - $this->padding['top'] - $this->padding['bottom']; |
| 82 | } |
| 83 | /** |
| 84 | * @return array |
| 85 | */ |
| 86 | protected function getNormalizedPadding() : array |
| 87 | { |
| 88 | return array_map(function ($value) { |
| 89 | return round($value * $this->ratioComputing); |
| 90 | }, $this->padding); |
| 91 | } |
| 92 | /** |
| 93 | * @return int |
| 94 | */ |
| 95 | protected function getInnerNormalizedHeight() : int |
| 96 | { |
| 97 | return $this->getInnerHeight() * $this->ratioComputing; |
| 98 | } |
| 99 | /** |
| 100 | * @return int |
| 101 | */ |
| 102 | protected function getNormalizedWidth() : int |
| 103 | { |
| 104 | return $this->width * $this->ratioComputing; |
| 105 | } |
| 106 | /** |
| 107 | * @return int |
| 108 | */ |
| 109 | protected function getInnerWidth() : int |
| 110 | { |
| 111 | return $this->width - ($this->padding['left'] + $this->padding['right']); |
| 112 | } |
| 113 | /** |
| 114 | * @return int |
| 115 | */ |
| 116 | protected function getInnerNormalizedWidth() : int |
| 117 | { |
| 118 | return $this->getInnerWidth() * $this->ratioComputing; |
| 119 | } |
| 120 | /** |
| 121 | * @return array |
| 122 | */ |
| 123 | protected function getNormalizedSize() : array |
| 124 | { |
| 125 | return [$this->getNormalizedWidth(), $this->getNormalizedHeight()]; |
| 126 | } |
| 127 | /** |
| 128 | * @return array |
| 129 | */ |
| 130 | protected function getInnerNormalizedSize() : array |
| 131 | { |
| 132 | return [$this->getInnerNormalizedWidth(), $this->getInnerNormalizedHeight()]; |
| 133 | } |
| 134 | /** |
| 135 | * @param int $count |
| 136 | * |
| 137 | * @return float |
| 138 | */ |
| 139 | protected function getStepWidth(int $count) : float |
| 140 | { |
| 141 | $innerWidth = $this->getInnerNormalizedWidth(); |
| 142 | return $innerWidth / ($count - 1); |
| 143 | } |
| 144 | /** |
| 145 | * @param array $data |
| 146 | * @param int $height |
| 147 | * |
| 148 | * @return array |
| 149 | */ |
| 150 | protected function getDataForChartElements(array $data, int $height) : array |
| 151 | { |
| 152 | $max = $this->getMaxValueAcrossSeries(); |
| 153 | $minHeight = 1 * $this->ratioComputing; |
| 154 | $maxHeight = $height - $minHeight; |
| 155 | foreach ($data as $i => $value) { |
| 156 | $value = (int) $value; |
| 157 | if ($value <= 0) { |
| 158 | $value = 0; |
| 159 | } |
| 160 | if ($value > 0) { |
| 161 | $value = round($value / $max * $height); |
| 162 | } |
| 163 | $data[$i] = max($minHeight, min($value, $maxHeight)); |
| 164 | } |
| 165 | return $data; |
| 166 | } |
| 167 | /** |
| 168 | * @param array $data |
| 169 | * @param int $count count of steps in sparkline image (does not have to == count($data)) |
| 170 | * @return array |
| 171 | */ |
| 172 | protected function getChartElements(array $data, int $count) : array |
| 173 | { |
| 174 | $step = $this->getStepWidth($count); |
| 175 | $height = $this->getInnerNormalizedHeight(); |
| 176 | $width = $this->getInnerNormalizedWidth(); |
| 177 | $normalizedPadding = $this->getNormalizedPadding(); |
| 178 | $data = $this->getDataForChartElements($data, $height); |
| 179 | $pictureX1 = $pictureX2 = (int) ceil($normalizedPadding['left']); |
| 180 | $pictureY1 = (int) ceil($normalizedPadding['top'] + $height - $data[0]); |
| 181 | $polygon = []; |
| 182 | $line = []; |
| 183 | // Initialize |
| 184 | $polygon[] = $normalizedPadding['left']; |
| 185 | $polygon[] = $normalizedPadding['top'] + $height; |
| 186 | // First element |
| 187 | $polygon[] = $pictureX1; |
| 188 | $polygon[] = $pictureY1; |
| 189 | for ($i = 1; $i < count($data); ++$i) { |
| 190 | $pictureX2 = min((int) ceil($pictureX1 + $step), $normalizedPadding['right'] + $width); |
| 191 | $pictureY2 = min((int) ceil($normalizedPadding['top'] + $height - $data[$i]), $normalizedPadding['top'] + $height); |
| 192 | $line[] = [$pictureX1, $pictureY1, $pictureX2, $pictureY2]; |
| 193 | $polygon[] = $pictureX2; |
| 194 | $polygon[] = $pictureY2; |
| 195 | $pictureX1 = $pictureX2; |
| 196 | $pictureY1 = $pictureY2; |
| 197 | } |
| 198 | // Last |
| 199 | $polygon[] = $pictureX2; |
| 200 | $polygon[] = $normalizedPadding['top'] + $height; |
| 201 | return [$polygon, $line]; |
| 202 | } |
| 203 | } |
| 204 |