-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathData.php
293 lines (264 loc) · 7.18 KB
/
Data.php
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
* Copyright (C) 2013-2022 Graham Breach
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* For more information, please contact <graham@goat1000.com>
*/
namespace Goat1000\SVGGraph;
/**
* Class for standard data
*/
class Data implements \Countable, \ArrayAccess, \Iterator {
private $datasets = 0;
private $data;
private $assoc = null;
private $datetime = null;
private $min_value = [];
private $max_value = [];
private $min_key = [];
private $max_key = [];
public $error = null;
public function __construct(&$data, $force_assoc, $datetime_keys)
{
if(empty($data[0])) {
$this->error = 'No data';
return;
}
$this->data = $data;
$this->datasets = count($data);
if($force_assoc)
$this->assoc = true;
if($datetime_keys) {
if($this->rekey('Goat1000\\SVGGraph\\Graph::dateConvert')) {
$this->datetime = true;
$this->assoc = false;
return;
}
$this->error = 'Too many date/time conversion errors';
}
}
/**
* Implement Iterator interface to prevent iteration...
*/
private function notIterator()
{
throw new \Exception('Cannot iterate ' . __CLASS__);
}
#[\ReturnTypeWillChange]
public function current() { $this->notIterator(); }
#[\ReturnTypeWillChange]
public function key() { $this->notIterator(); }
#[\ReturnTypeWillChange]
public function next() { $this->notIterator(); }
#[\ReturnTypeWillChange]
public function rewind() { $this->notIterator(); }
#[\ReturnTypeWillChange]
public function valid() { $this->notIterator(); }
/**
* ArrayAccess methods
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return array_key_exists($offset, $this->data);
}
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return new DataIterator($this->data, $offset);
}
/**
* Don't allow writing to the data
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
throw new \Exception('Read-only');
}
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
throw new \Exception('Read-only');
}
/**
* Countable method
*/
#[\ReturnTypeWillChange]
public function count()
{
return $this->datasets;
}
/**
* Returns minimum data value for a dataset
*/
public function getMinValue($dataset = 0)
{
if(!isset($this->min_value[$dataset])) {
$this->min_value[$dataset] = null;
if(count($this->data[$dataset]))
$this->min_value[$dataset] = Graph::min($this->data[$dataset]);
}
return $this->min_value[$dataset];
}
/**
* Returns maximum data value for a dataset
*/
public function getMaxValue($dataset = 0)
{
if(!isset($this->max_value[$dataset])) {
$this->max_value[$dataset] = null;
if(count($this->data[$dataset]))
$this->max_value[$dataset] = max($this->data[$dataset]);
}
return $this->max_value[$dataset];
}
/**
* Returns the minimum key value
*/
public function getMinKey($dataset = 0)
{
if(!isset($this->min_key[$dataset])) {
$this->min_key[$dataset] = null;
if(count($this->data[$dataset])) {
$this->min_key[$dataset] = $this->associativeKeys() ? 0 :
min(array_keys($this->data[$dataset]));
}
}
return $this->min_key[$dataset];
}
/**
* Returns the maximum key value
*/
public function getMaxKey($dataset = 0)
{
if(!isset($this->max_key[$dataset])) {
$this->max_key[$dataset] = null;
if(count($this->data[$dataset])) {
$this->max_key[$dataset] = $this->associativeKeys() ?
count($this->data[$dataset]) - 1 :
max(array_keys($this->data[$dataset]));
}
}
return $this->max_key[$dataset];
}
/**
* Returns the key at a given index
*/
public function getKey($index, $dataset = 0)
{
if(!$this->associativeKeys())
return $index;
// round index to nearest integer, or PHP will floor() it
$index = (int)round($index);
if($index >= 0) {
$slice = array_slice($this->data[$dataset], $index, 1, true);
// use foreach to get key and value
foreach($slice as $k => $v)
return $k;
}
return null;
}
/**
* Returns TRUE if the keys are associative
*/
public function associativeKeys()
{
if($this->assoc !== null)
return $this->assoc;
foreach(array_keys($this->data[0]) as $k)
if(!is_integer($k))
return ($this->assoc = true);
return ($this->assoc = false);
}
/**
* Returns the number of data items
*/
public function itemsCount($dataset = 0)
{
if($dataset < 0)
$dataset = 0;
return count($this->data[$dataset]);
}
/**
* Returns the min and max sum values
*/
public function getMinMaxSumValues($start = 0, $end = null)
{
if($start != 0 || ($end !== null && $end != 0))
throw new \Exception('Dataset not found');
// structured data is used for multi-data, so just
// return the min and max
return [$this->getMinValue(), $this->getMaxValue()];
}
/**
* Returns the min/max sum values for an array of datasets
*/
public function getMinMaxSumValuesFor($datasets)
{
// Data class can't handle multiple datasets
if(count($datasets) > 1)
throw new \InvalidArgumentException('Multiple datasets not supported');
$d = array_pop($datasets);
if($d < 0 || $d >= $this->datasets)
throw new \Exception('Dataset not found');
return [$this->getMinValue($d), $this->getMaxValue($d)];
}
/**
* Returns TRUE if the item exists, setting the $value
*/
public function getData($index, $name, &$value)
{
// base class doesn't support this, so always return false
return false;
}
/**
* Doesn't return a structured data item
*/
public function getItem($index, $dataset = 0)
{
return null;
}
/**
* Transforms the keys using a callback function
*/
public function rekey($callback)
{
$new_data = [];
$count = $invalid = 0;
for($d = 0; $d < $this->datasets; ++$d) {
$new_data[$d] = [];
foreach($this->data[$d] as $key => $value) {
$new_key = call_user_func($callback, $key);
// if the callback returns null, skip the value
if($new_key === null) {
++$invalid;
continue;
}
$new_data[$d][$new_key] = $value;
}
++$count;
}
// if too many invalid, probably a format error
if($count && $invalid / $count > 0.05)
return false;
$this->data = $new_data;
// forget previous min/max
$this->min_key = [];
$this->max_key = [];
return true;
}
}