blob: 70961e22f43eb99084bf703197291cc28830ed20 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Chart\Unit;
/**
* Logarithmic tick distribution over the axis
*
* This class does not use the actual logarithm, but a slightly altered version called the
* Log-Modulo transformation. This is necessary, since a regular logarithmic scale is not able to display negative
* values and zero-points. See <a href="http://blogs.sas.com/content/iml/2014/07/14/log-transformation-of-pos-neg>
* this article </a> for a more detailed description.
*/
class LogarithmicUnit implements AxisUnit
{
/**
* @var int
*/
protected $base;
/**
* @var
*/
protected $currentTick;
/**
* @var
*/
protected $minExp;
/**
* @var
*/
protected $maxExp;
/**
* True when the minimum value is static and isn't affected by the data set
*
* @var bool
*/
protected $staticMin = false;
/**
* True when the maximum value is static and isn't affected by the data set
*
* @var bool
*/
protected $staticMax = false;
/**
* Create and initialize this AxisUnit
*
* @param int $nrOfTicks The number of ticks to use
*/
public function __construct($base = 10)
{
$this->base = $base;
$this->minExp = PHP_INT_MAX;
$this->maxExp = ~PHP_INT_MAX;
}
/**
* Add a dataset and calculate the minimum and maximum value for this AxisUnit
*
* @param array $dataset The dataset to add
* @param int $idx The idx (0 for x, 1 for y)
*
* @return $this Fluent interface
*/
public function addValues(array $dataset, $idx = 0)
{
$datapoints = array();
foreach ($dataset['data'] as $points) {
$datapoints[] = $points[$idx];
}
if (empty($datapoints)) {
return $this;
}
sort($datapoints);
if (!$this->staticMax) {
$this->maxExp = max($this->maxExp, $this->logCeil($datapoints[count($datapoints) - 1]));
}
if (!$this->staticMin) {
$this->minExp = min($this->minExp, $this->logFloor($datapoints[0]));
}
$this->currentTick = 0;
return $this;
}
/**
* Transform the absolute value to an axis relative value
*
* @param int $value The absolute coordinate from the data set
* @return float|int The axis relative coordinate (between 0 and 100)
*/
public function transform($value)
{
if ($value < $this->pow($this->minExp)) {
return 0;
} elseif ($value > $this->pow($this->maxExp)) {
return 100;
} else {
return 100 * ($this->log($value) - $this->minExp) / $this->getTicks();
}
}
/**
* Return the position of the current tick
*
* @return int
*/
public function current(): int
{
return $this->currentTick * (100 / $this->getTicks());
}
/**
* Calculate the next tick and tick value
*/
public function next(): void
{
++ $this->currentTick;
}
/**
* Return the label for the current tick
*
* @return string The label for the current tick
*/
public function key(): string
{
$currentBase = $this->currentTick + $this->minExp;
if (abs($currentBase) > 4) {
return $this->base . 'E' . $currentBase;
}
return (string) intval($this->pow($currentBase));
}
/**
* True when we're at a valid tick (iterator interface)
*
* @return bool
*/
public function valid(): bool
{
return $this->currentTick >= 0 && $this->currentTick < $this->getTicks();
}
/**
* Reset the current tick and label value
*/
public function rewind(): void
{
$this->currentTick = 0;
}
/**
* Perform a log-modulo transformation
*
* @param $value The value to transform
*
* @return double The transformed value
*/
protected function log($value)
{
$sign = $value > 0 ? 1 : -1;
return $sign * log1p($sign * $value) / log($this->base);
}
/**
* Calculate the biggest exponent necessary to display the given data point
*
* @param $value
*
* @return float
*/
protected function logCeil($value)
{
return ceil($this->log($value)) + 1;
}
/**
* Calculate the smallest exponent necessary to display the given data point
*
* @param $value
*
* @return float
*/
protected function logFloor($value)
{
return floor($this->log($value));
}
/**
* Inverse function to the log-modulo transformation
*
* @param $value
*
* @return double
*/
protected function pow($value)
{
if ($value == 0) {
return 0;
}
$sign = $value > 0 ? 1 : -1;
return $sign * (pow($this->base, $sign * $value));
}
/**
* Set the axis minimum value to a fixed value
*
* @param int $min The new minimum value
*/
public function setMin($min)
{
$this->minExp = $this->logFloor($min);
$this->staticMin = true;
}
/**
* Set the axis maximum value to a fixed value
*
* @param int $max The new maximum value
*/
public function setMax($max)
{
$this->maxExp = $this->logCeil($max);
$this->staticMax = true;
}
/**
* Return the current minimum value of the axis
*
* @return int The minimum set for this axis
*/
public function getMin()
{
return $this->pow($this->minExp);
}
/**
* Return the current maximum value of the axis
*
* @return int The maximum set for this axis
*/
public function getMax()
{
return $this->pow($this->maxExp);
}
/**
* Get the amount of ticks necessary to display this AxisUnit
*
* @return int
*/
public function getTicks()
{
return $this->maxExp - $this->minExp;
}
}
|