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
PointTrait.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Davaxi\Sparkline; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | /** |
| 7 | * Trait PointTrait. |
| 8 | */ |
| 9 | trait PointTrait |
| 10 | { |
| 11 | /** |
| 12 | * @var array |
| 13 | */ |
| 14 | protected $points = []; |
| 15 | /** |
| 16 | * @param int|string $index |
| 17 | * @param float $dotRadius |
| 18 | * @param string $colorHex |
| 19 | * @param int $seriesIndex |
| 20 | */ |
| 21 | public function addPoint($index, float $dotRadius, string $colorHex, int $seriesIndex = 0) |
| 22 | { |
| 23 | $mapping = $this->getPointIndexMapping($seriesIndex); |
| 24 | if (array_key_exists($index, $mapping)) { |
| 25 | $index = $mapping[$index]; |
| 26 | if ($index < 0) { |
| 27 | return; |
| 28 | } |
| 29 | } |
| 30 | if (!is_numeric($index)) { |
| 31 | throw new InvalidArgumentException('Invalid index : ' . $index); |
| 32 | } |
| 33 | $this->checkPointIndex($index, $seriesIndex); |
| 34 | $this->points[] = ['series' => $seriesIndex, 'index' => $index, 'radius' => $dotRadius, 'color' => $this->colorHexToRGB($colorHex)]; |
| 35 | } |
| 36 | /** |
| 37 | * @param int $seriesIndex |
| 38 | * @return array |
| 39 | */ |
| 40 | protected function getPointIndexMapping(int $seriesIndex = 0) : array |
| 41 | { |
| 42 | $count = $this->getCount($seriesIndex); |
| 43 | list($minIndex, $min, $maxIndex, $max) = $this->getExtremeValues($seriesIndex); |
| 44 | $mapping = []; |
| 45 | $mapping['first'] = $count > 1 ? 0 : -1; |
| 46 | $mapping['last'] = $count > 1 ? $count - 1 : -1; |
| 47 | $mapping['minimum'] = $min !== $max ? $minIndex : -1; |
| 48 | $mapping['maximum'] = $min !== $max ? $maxIndex : -1; |
| 49 | return $mapping; |
| 50 | } |
| 51 | /** |
| 52 | * @param int $index |
| 53 | * @param int $seriesIndex |
| 54 | */ |
| 55 | protected function checkPointIndex(int $index, int $seriesIndex) |
| 56 | { |
| 57 | $count = $this->getCount($seriesIndex); |
| 58 | if ($index < 0 || $index >= $count) { |
| 59 | throw new InvalidArgumentException('Index out of range [0-' . ($count - 1) . '] : ' . $index); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 |