PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / vendor / davaxi / sparkline / src / Sparkline / DataTrait.php
matomo / app / vendor / davaxi / sparkline / src / Sparkline Last commit date
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