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
DataTrait.php
167 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Davaxi\Sparkline; |
| 4 | |
| 5 | /** |
| 6 | * Trait DataTrait. |
| 7 | */ |
| 8 | trait DataTrait |
| 9 | { |
| 10 | /** |
| 11 | * @var int |
| 12 | * Base of value |
| 13 | */ |
| 14 | protected $base; |
| 15 | /** |
| 16 | * @var int |
| 17 | * Original value of chart |
| 18 | */ |
| 19 | protected $originValue = 0; |
| 20 | /** |
| 21 | * @var array |
| 22 | */ |
| 23 | protected $data = [[0, 0]]; |
| 24 | /** |
| 25 | * @param $base |
| 26 | * Set base for values |
| 27 | */ |
| 28 | public function setBase($base) |
| 29 | { |
| 30 | $this->base = $base; |
| 31 | } |
| 32 | /** |
| 33 | * @param float $originValue |
| 34 | * Set origin value of chart |
| 35 | */ |
| 36 | public function setOriginValue(float $originValue) |
| 37 | { |
| 38 | $this->originValue = $originValue; |
| 39 | } |
| 40 | /** |
| 41 | * @param ...$allSeries |
| 42 | */ |
| 43 | public function setData(...$allSeries) |
| 44 | { |
| 45 | $allSeries = func_get_args(); |
| 46 | $this->data = []; |
| 47 | foreach ($allSeries as $data) { |
| 48 | $this->addSeries($data); |
| 49 | } |
| 50 | } |
| 51 | /** |
| 52 | * @param array $data |
| 53 | */ |
| 54 | public function addSeries(array $data) |
| 55 | { |
| 56 | $data = array_values($data); |
| 57 | $count = count($data); |
| 58 | if (!$count) { |
| 59 | $this->data[] = [0, 0]; |
| 60 | return; |
| 61 | } |
| 62 | if ($count < static::MIN_DATA_LENGTH) { |
| 63 | $this->data[] = array_fill(0, 2, $data[0]); |
| 64 | return; |
| 65 | } |
| 66 | $this->data[] = $data; |
| 67 | } |
| 68 | /** |
| 69 | * @return int |
| 70 | */ |
| 71 | public function getSeriesCount() : int |
| 72 | { |
| 73 | return count($this->data); |
| 74 | } |
| 75 | /** |
| 76 | * @param int $seriesIndex |
| 77 | * @return array |
| 78 | */ |
| 79 | public function getNormalizedData(int $seriesIndex = 0) : array |
| 80 | { |
| 81 | $data = $this->data[$seriesIndex]; |
| 82 | foreach ($data as $i => $value) { |
| 83 | $data[$i] = max(0, $value - $this->originValue); |
| 84 | } |
| 85 | return $data; |
| 86 | } |
| 87 | /** |
| 88 | * @param int $seriesIndex |
| 89 | * @return array |
| 90 | */ |
| 91 | public function getData(int $seriesIndex = 0) : array |
| 92 | { |
| 93 | return $this->data[$seriesIndex]; |
| 94 | } |
| 95 | /** |
| 96 | * @param int $seriesIndex |
| 97 | * @return int |
| 98 | */ |
| 99 | public function getCount(int $seriesIndex = 0) : int |
| 100 | { |
| 101 | return count($this->data[$seriesIndex]); |
| 102 | } |
| 103 | /** |
| 104 | * @param int $seriesIndex |
| 105 | * @return array |
| 106 | */ |
| 107 | protected function getMaxValueWithIndex(int $seriesIndex = 0) : array |
| 108 | { |
| 109 | $max = max($this->data[$seriesIndex]); |
| 110 | $maxKeys = array_keys($this->data[$seriesIndex], $max); |
| 111 | $maxIndex = end($maxKeys); |
| 112 | if ($this->base) { |
| 113 | $max = $this->base; |
| 114 | } |
| 115 | return [$maxIndex, $max]; |
| 116 | } |
| 117 | /** |
| 118 | * @param int $seriesIndex |
| 119 | * @return float |
| 120 | */ |
| 121 | protected function getMaxValue(int $seriesIndex = 0) : float |
| 122 | { |
| 123 | if ($this->base) { |
| 124 | return $this->base; |
| 125 | } |
| 126 | return max($this->data[$seriesIndex]); |
| 127 | } |
| 128 | /** |
| 129 | * TODO: this could be cached somehow |
| 130 | * @return float |
| 131 | */ |
| 132 | protected function getMaxValueAcrossSeries() : float |
| 133 | { |
| 134 | if ($this->base) { |
| 135 | return $this->base; |
| 136 | } |
| 137 | $maxes = array_map('max', $this->data); |
| 138 | return max($maxes); |
| 139 | } |
| 140 | protected function getMaxNumberOfDataPointsAcrossSerieses() |
| 141 | { |
| 142 | $counts = array_map('count', $this->data); |
| 143 | return max($counts); |
| 144 | } |
| 145 | /** |
| 146 | * @param int $seriesIndex |
| 147 | * @return array |
| 148 | */ |
| 149 | protected function getMinValueWithIndex(int $seriesIndex = 0) : array |
| 150 | { |
| 151 | $min = min($this->data[$seriesIndex]); |
| 152 | $minKey = array_keys($this->data[$seriesIndex], $min); |
| 153 | $minIndex = end($minKey); |
| 154 | return [$minIndex, $min]; |
| 155 | } |
| 156 | /** |
| 157 | * @param int $seriesIndex |
| 158 | * @return array |
| 159 | */ |
| 160 | protected function getExtremeValues(int $seriesIndex = 0) : array |
| 161 | { |
| 162 | list($minIndex, $min) = $this->getMinValueWithIndex($seriesIndex); |
| 163 | list($maxIndex, $max) = $this->getMaxValueWithIndex($seriesIndex); |
| 164 | return [$minIndex, $min, $maxIndex, $max]; |
| 165 | } |
| 166 | } |
| 167 |