| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2011-2022 Graham Breach |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Lesser General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
/** |
| 19 |
* For more information, please contact <graham@goat1000.com> |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Goat1000\SVGGraph; |
| 23 |
|
| 24 |
class MultiGraph implements \Countable, \ArrayAccess, \Iterator { |
| 25 |
|
| 26 |
private $values; |
| 27 |
private $datasets = 0; |
| 28 |
private $enabled_datasets = null; |
| 29 |
private $force_assoc; |
| 30 |
private $datetime_keys; |
| 31 |
private $max_key = null; |
| 32 |
private $min_key = null; |
| 33 |
private $max_value = null; |
| 34 |
private $min_value = null; |
| 35 |
private $max_sum_value = null; |
| 36 |
private $min_sum_value = null; |
| 37 |
private $item_list = []; |
| 38 |
private $position = 0; |
| 39 |
private $item_cache = []; |
| 40 |
private $item_cache_pos = -1; |
| 41 |
|
| 42 |
public function __construct($values, $force_assoc, $datetime_keys, $int_keys) |
| 43 |
{ |
| 44 |
$this->values =& $values; |
| 45 |
$this->force_assoc = $force_assoc; |
| 46 |
$this->datetime_keys = $datetime_keys; |
| 47 |
$keys = []; |
| 48 |
|
| 49 |
// convert unstructured data to structured |
| 50 |
if(count($values) > 1 && $this->values instanceof Data) { |
| 51 |
$this->values = StructuredData::convertFrom($values, $force_assoc, |
| 52 |
$datetime_keys, $int_keys); |
| 53 |
} |
| 54 |
$this->datasets = count($this->values); |
| 55 |
$this->enabled_datasets = range(0, $this->datasets - 1); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Sets the list of datasets that are enabled |
| 60 |
*/ |
| 61 |
public function setEnabledDatasets($datasets) |
| 62 |
{ |
| 63 |
if($datasets === null) |
| 64 |
$datasets = range(0, $this->datasets - 1); |
| 65 |
if(!is_array($datasets)) |
| 66 |
$datasets = [$datasets]; |
| 67 |
|
| 68 |
$enabled = []; |
| 69 |
foreach($datasets as $d) |
| 70 |
if($d >= 0 && $d < $this->datasets) |
| 71 |
$enabled[] = $d; |
| 72 |
$this->enabled_datasets = $enabled; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns the list of enabled datasets |
| 77 |
*/ |
| 78 |
public function getEnabledDatasets() |
| 79 |
{ |
| 80 |
return $this->enabled_datasets; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Pass all unhandled functions through to the structured data instance |
| 85 |
*/ |
| 86 |
public function __call($name, $arguments) |
| 87 |
{ |
| 88 |
return call_user_func_array([$this->values, $name], $arguments); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Implement Iterator interface |
| 93 |
*/ |
| 94 |
#[\ReturnTypeWillChange] |
| 95 |
public function current() |
| 96 |
{ |
| 97 |
if($this->item_cache_pos != $this->position) { |
| 98 |
$this->item_cache[0] = $this->values[0]->getItemByIndex($this->position); |
| 99 |
|
| 100 |
// use NewFrom to create other data items quicker |
| 101 |
for($i = 1; $i < $this->datasets; ++$i) |
| 102 |
$this->item_cache[$i] = $this->item_cache[0]->newFrom($i); |
| 103 |
$this->item_cache_pos = $this->position; |
| 104 |
} |
| 105 |
return $this->item_cache; |
| 106 |
} |
| 107 |
#[\ReturnTypeWillChange] |
| 108 |
public function key() |
| 109 |
{ |
| 110 |
return $this->position; |
| 111 |
} |
| 112 |
#[\ReturnTypeWillChange] |
| 113 |
public function next() |
| 114 |
{ |
| 115 |
++$this->position; |
| 116 |
} |
| 117 |
#[\ReturnTypeWillChange] |
| 118 |
public function rewind() |
| 119 |
{ |
| 120 |
$this->position = 0; |
| 121 |
} |
| 122 |
#[\ReturnTypeWillChange] |
| 123 |
public function valid() |
| 124 |
{ |
| 125 |
return $this->position < $this->itemsCount(); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* ArrayAccess methods |
| 130 |
*/ |
| 131 |
#[\ReturnTypeWillChange] |
| 132 |
public function offsetExists($offset) |
| 133 |
{ |
| 134 |
return ($offset >= 0 && $offset < $this->datasets); |
| 135 |
} |
| 136 |
|
| 137 |
#[\ReturnTypeWillChange] |
| 138 |
public function offsetGet($offset) |
| 139 |
{ |
| 140 |
return $this->values[$offset]; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Don't allow writing to the data |
| 145 |
*/ |
| 146 |
#[\ReturnTypeWillChange] |
| 147 |
public function offsetSet($offset, $value) |
| 148 |
{ |
| 149 |
throw new \Exception('Read-only'); |
| 150 |
} |
| 151 |
#[\ReturnTypeWillChange] |
| 152 |
public function offsetUnset($offset) |
| 153 |
{ |
| 154 |
throw new \Exception('Read-only'); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Countable method |
| 159 |
*/ |
| 160 |
#[\ReturnTypeWillChange] |
| 161 |
public function count() |
| 162 |
{ |
| 163 |
return $this->datasets; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Returns the number of items |
| 168 |
*/ |
| 169 |
public function itemsCount() |
| 170 |
{ |
| 171 |
// use -1 for all items |
| 172 |
return $this->values->itemsCount(-1); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Returns the maximum value |
| 177 |
*/ |
| 178 |
public function getMaxValue() |
| 179 |
{ |
| 180 |
if($this->max_value !== null) |
| 181 |
return $this->max_value; |
| 182 |
$maxima = []; |
| 183 |
$chunk_count = count($this->values); |
| 184 |
for($i = 0; $i < $chunk_count; ++$i) { |
| 185 |
if(!in_array($i, $this->enabled_datasets)) |
| 186 |
continue; |
| 187 |
$maxima[] = $this->values->getMaxValue($i); |
| 188 |
} |
| 189 |
|
| 190 |
$this->max_value = count($maxima) ? max($maxima) : null; |
| 191 |
return $this->max_value; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
/** |
| 196 |
* Returns the minimum value |
| 197 |
*/ |
| 198 |
public function getMinValue() |
| 199 |
{ |
| 200 |
if($this->min_value !== null) |
| 201 |
return $this->min_value; |
| 202 |
$minima = []; |
| 203 |
$chunk_count = count($this->values); |
| 204 |
for($i = 0; $i < $chunk_count; ++$i) { |
| 205 |
if(!in_array($i, $this->enabled_datasets)) |
| 206 |
continue; |
| 207 |
$min_val = $this->values->getMinValue($i); |
| 208 |
if($min_val !== null) |
| 209 |
$minima[] = $min_val; |
| 210 |
} |
| 211 |
|
| 212 |
$this->min_value = count($minima) ? min($minima) : null; |
| 213 |
return $this->min_value; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
/** |
| 218 |
* Returns the maximum key value |
| 219 |
*/ |
| 220 |
public function getMaxKey() |
| 221 |
{ |
| 222 |
if($this->max_key !== null) |
| 223 |
return $this->max_key; |
| 224 |
|
| 225 |
$max = []; |
| 226 |
for($i = 0; $i < $this->datasets; ++$i) { |
| 227 |
if(!in_array($i, $this->enabled_datasets)) |
| 228 |
continue; |
| 229 |
$max[] = $this->values->getMaxKey($i); |
| 230 |
} |
| 231 |
$this->max_key = count($max) ? max($max) : null; |
| 232 |
return $this->max_key; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Returns the minimum key value |
| 237 |
*/ |
| 238 |
public function getMinKey() |
| 239 |
{ |
| 240 |
if($this->min_key !== null) |
| 241 |
return $this->min_key; |
| 242 |
|
| 243 |
$min = []; |
| 244 |
for($i = 0; $i < $this->datasets; ++$i) { |
| 245 |
if(!in_array($i, $this->enabled_datasets)) |
| 246 |
continue; |
| 247 |
$min[] = $this->values->getMinKey($i); |
| 248 |
} |
| 249 |
$this->min_key = count($min) ? min($min) : null; |
| 250 |
return $this->min_key; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Returns the maximum sum value |
| 255 |
*/ |
| 256 |
public function getMaxSumValue() |
| 257 |
{ |
| 258 |
if($this->max_sum_value === null) |
| 259 |
$this->calcMinMaxSumValues(); |
| 260 |
return $this->max_sum_value; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Returns the minimum sum value (the negative part) |
| 265 |
*/ |
| 266 |
public function getMinSumValue() |
| 267 |
{ |
| 268 |
if($this->min_sum_value === null) |
| 269 |
$this->calcMinMaxSumValues(); |
| 270 |
return $this->min_sum_value; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
/** |
| 275 |
* Calculates the minimum and maximum sum values |
| 276 |
*/ |
| 277 |
public function calcMinMaxSumValues() |
| 278 |
{ |
| 279 |
list($this->min_sum_value, $this->max_sum_value) = |
| 280 |
$this->values->getMinMaxSumValuesFor($this->enabled_datasets); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Access to the structured data values |
| 285 |
*/ |
| 286 |
public function &getValues() |
| 287 |
{ |
| 288 |
return $this->values; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Pass through to the structured data values |
| 293 |
*/ |
| 294 |
public function getData($index, $name, &$value) |
| 295 |
{ |
| 296 |
// the reference means __call can't handle this |
| 297 |
return $this->values->getData($index, $name, $value); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|