blob: ea4792b4e39828c24638f063addd901bcc16f63b (
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
|
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Chart\Unit;
/**
* Linear tick distribution over the axis
*/
class LinearUnit implements AxisUnit
{
/**
* The minimum value to display
*
* @var int
*/
protected $min;
/**
* The maximum value to display
*
* @var int
*/
protected $max;
/**
* True when the minimum value is static and isn't affected by the dataset
*
* @var bool
*/
protected $staticMin = false;
/**
* True when the maximum value is static and isn't affected by the dataset
*
* @var bool
*/
protected $staticMax = false;
/**
* The number of ticks to use
*
* @var int
*/
protected $nrOfTicks = 10;
/**
* The currently displayed tick
*
* @var int
*/
protected $currentTick = 0;
/**
* The currently displayed value
* @var int
*/
protected $currentValue = 0;
/**
* Create and initialize this AxisUnit
*
* @param int $nrOfTicks The number of ticks to use
*/
public function __construct($nrOfTicks = 10)
{
$this->min = PHP_INT_MAX;
$this->max = ~PHP_INT_MAX;
$this->nrOfTicks = $nrOfTicks;
}
/**
* 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->max = max($this->max, $datapoints[count($datapoints) - 1]);
}
if (!$this->staticMin) {
$this->min = min($this->min, $datapoints[0]);
}
$this->currentTick = 0;
$this->currentValue = $this->min;
if ($this->max === $this->min) {
$this->max = $this->min + 10;
}
$this->nrOfTicks = $this->max - $this->min;
return $this;
}
/**
* Transform the absolute value to an axis relative value
*
* @param int $value The absolute coordinate from the dataset
* @return float|int The axis relative coordinate (between 0 and 100)
*/
public function transform($value)
{
if ($value < $this->min) {
return 0;
} elseif ($value > $this->max) {
return 100;
} else {
return 100 * ($value - $this->min) / $this->nrOfTicks;
}
}
/**
* Return the position of the current tick
*
* @return int
*/
#[\ReturnTypeWillChange]
public function current()
{
return $this->currentTick;
}
/**
* Calculate the next tick and tick value
*/
public function next(): void
{
$this->currentTick += (100 / $this->nrOfTicks);
$this->currentValue += (($this->max - $this->min) / $this->nrOfTicks);
}
/**
* Return the label for the current tick
*
* @return string The label for the current tick
*/
#[\ReturnTypeWillChange]
public function key()
{
return (string) intval($this->currentValue);
}
/**
* True when we're at a valid tick (iterator interface)
*
* @return bool
*/
public function valid(): bool
{
return $this->currentTick >= 0 && $this->currentTick <= 100;
}
/**
* Reset the current tick and label value
*/
public function rewind(): void
{
$this->currentTick = 0;
$this->currentValue = $this->min;
}
/**
* Set the axis maximum value to a fixed value
*
* @param int $max The new maximum value
*/
public function setMax($max)
{
if ($max !== null) {
$this->max = $max;
$this->staticMax = true;
}
}
/**
* Set the axis minimum value to a fixed value
*
* @param int $min The new minimum value
*/
public function setMin($min)
{
if ($min !== null) {
$this->min = $min;
$this->staticMin = true;
}
}
/**
* Return the current minimum value of the axis
*
* @return int The minimum set for this axis
*/
public function getMin()
{
return $this->min;
}
/**
* Return the current maximum value of the axis
*
* @return int The maximum set for this axis
*/
public function getMax()
{
return $this->max;
}
/**
* Get the amount of ticks necessary to display this AxisUnit
*
* @return int
*/
public function getTicks()
{
return $this->nrOfTicks;
}
}
|