1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Chart; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Cell; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Copyright (c) 2006 - 2016 PhpSpreadsheet. |
12
|
|
|
* |
13
|
|
|
* This library is free software; you can redistribute it and/or |
14
|
|
|
* modify it under the terms of the GNU Lesser General Public |
15
|
|
|
* License as published by the Free Software Foundation; either |
16
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
17
|
|
|
* |
18
|
|
|
* This library is distributed in the hope that it will be useful, |
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
21
|
|
|
* Lesser General Public License for more details. |
22
|
|
|
* |
23
|
|
|
* You should have received a copy of the GNU Lesser General Public |
24
|
|
|
* License along with this library; if not, write to the Free Software |
25
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
26
|
|
|
* |
27
|
|
|
* @category PhpSpreadsheet |
28
|
|
|
* |
29
|
|
|
* @copyright Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) |
30
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
31
|
|
|
*/ |
32
|
|
|
class DataSeriesValues |
33
|
|
|
{ |
34
|
|
|
const DATASERIES_TYPE_STRING = 'String'; |
35
|
|
|
const DATASERIES_TYPE_NUMBER = 'Number'; |
36
|
|
|
|
37
|
|
|
private static $dataTypeValues = [ |
38
|
|
|
self::DATASERIES_TYPE_STRING, |
39
|
|
|
self::DATASERIES_TYPE_NUMBER, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Series Data Type. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $dataType; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Series Data Source. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private $dataSource; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Format Code. |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $formatCode; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Series Point Marker. |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $pointMarker; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Point Count (The number of datapoints in the dataseries). |
72
|
|
|
* |
73
|
|
|
* @var int |
74
|
|
|
*/ |
75
|
|
|
private $pointCount = 0; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Data Values. |
79
|
|
|
* |
80
|
|
|
* @var array of mixed |
81
|
|
|
*/ |
82
|
|
|
private $dataValues = []; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Fill color. |
86
|
|
|
* |
87
|
|
|
* @var string |
88
|
|
|
*/ |
89
|
|
|
private $fillColor; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Create a new DataSeriesValues object. |
93
|
|
|
* |
94
|
|
|
* |
95
|
|
|
* @param mixed $dataType |
96
|
|
|
* @param string $dataSource |
97
|
|
|
* @param null|mixed $formatCode |
98
|
|
|
* @param mixed $pointCount |
99
|
|
|
* @param mixed $dataValues |
100
|
|
|
* @param null|mixed $marker |
101
|
|
|
* @param null|string $fillColor |
102
|
|
|
*/ |
103
|
17 |
|
public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null, $fillColor = null) |
104
|
|
|
{ |
105
|
17 |
|
$this->setDataType($dataType); |
106
|
17 |
|
$this->dataSource = $dataSource; |
107
|
17 |
|
$this->formatCode = $formatCode; |
108
|
17 |
|
$this->pointCount = $pointCount; |
109
|
17 |
|
$this->dataValues = $dataValues; |
|
|
|
|
110
|
17 |
|
$this->pointMarker = $marker; |
111
|
17 |
|
$this->fillColor = $fillColor; |
112
|
17 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get Series Data Type. |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
1 |
|
public function getDataType() |
120
|
|
|
{ |
121
|
1 |
|
return $this->dataType; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set Series Data Type. |
126
|
|
|
* |
127
|
|
|
* @param string $dataType Datatype of this data series |
128
|
|
|
* Typical values are: |
129
|
|
|
* DataSeriesValues::DATASERIES_TYPE_STRING |
130
|
|
|
* Normally used for axis point values |
131
|
|
|
* DataSeriesValues::DATASERIES_TYPE_NUMBER |
132
|
|
|
* Normally used for chart data values |
133
|
|
|
* |
134
|
|
|
* @throws Exception |
135
|
|
|
* |
136
|
|
|
* @return DataSeriesValues |
137
|
|
|
*/ |
138
|
17 |
|
public function setDataType($dataType) |
139
|
|
|
{ |
140
|
17 |
|
if (!in_array($dataType, self::$dataTypeValues)) { |
141
|
1 |
|
throw new Exception('Invalid datatype for chart data series values'); |
142
|
|
|
} |
143
|
17 |
|
$this->dataType = $dataType; |
144
|
|
|
|
145
|
17 |
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get Series Data Source (formula). |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
13 |
|
public function getDataSource() |
154
|
|
|
{ |
155
|
13 |
|
return $this->dataSource; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set Series Data Source (formula). |
160
|
|
|
* |
161
|
|
|
* @param string $dataSource |
162
|
|
|
* |
163
|
|
|
* @return DataSeriesValues |
164
|
|
|
*/ |
165
|
|
|
public function setDataSource($dataSource) |
166
|
|
|
{ |
167
|
|
|
$this->dataSource = $dataSource; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get Point Marker. |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
13 |
|
public function getPointMarker() |
178
|
|
|
{ |
179
|
13 |
|
return $this->pointMarker; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set Point Marker. |
184
|
|
|
* |
185
|
|
|
* @param string $marker |
186
|
|
|
* |
187
|
|
|
* @return DataSeriesValues |
188
|
|
|
*/ |
189
|
|
|
public function setPointMarker($marker) |
190
|
|
|
{ |
191
|
|
|
$this->pointMarker = $marker; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get Series Format Code. |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
12 |
|
public function getFormatCode() |
202
|
|
|
{ |
203
|
12 |
|
return $this->formatCode; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Set Series Format Code. |
208
|
|
|
* |
209
|
|
|
* @param string $formatCode |
210
|
|
|
* |
211
|
|
|
* @return DataSeriesValues |
212
|
|
|
*/ |
213
|
|
|
public function setFormatCode($formatCode) |
214
|
|
|
{ |
215
|
|
|
$this->formatCode = $formatCode; |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Get Series Point Count. |
222
|
|
|
* |
223
|
|
|
* @return int |
224
|
|
|
*/ |
225
|
13 |
|
public function getPointCount() |
226
|
|
|
{ |
227
|
13 |
|
return $this->pointCount; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get fill color. |
232
|
|
|
* |
233
|
|
|
* @return string HEX color |
234
|
|
|
*/ |
235
|
13 |
|
public function getFillColor() |
236
|
|
|
{ |
237
|
13 |
|
return $this->fillColor; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Set fill color for series. |
242
|
|
|
* |
243
|
|
|
* @param string $color HEX color |
244
|
|
|
* |
245
|
|
|
* @return DataSeriesValues |
246
|
|
|
*/ |
247
|
|
|
public function setFillColor($color) |
248
|
|
|
{ |
249
|
|
|
if (!preg_match('/^[a-f0-9]{6}$/i', $color)) { |
250
|
|
|
throw new Exception('Invalid hex color for chart series'); |
251
|
|
|
} |
252
|
|
|
$this->fillColor = $color; |
253
|
|
|
|
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Identify if the Data Series is a multi-level or a simple series. |
259
|
|
|
* |
260
|
|
|
* @return bool|null |
261
|
|
|
*/ |
262
|
13 |
|
public function isMultiLevelSeries() |
263
|
|
|
{ |
264
|
13 |
|
if (count($this->dataValues) > 0) { |
265
|
13 |
|
return is_array($this->dataValues[0]); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return null; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Return the level count of a multi-level Data Series. |
273
|
|
|
* |
274
|
|
|
* @return int |
275
|
|
|
*/ |
276
|
2 |
|
public function multiLevelCount() |
277
|
|
|
{ |
278
|
2 |
|
$levelCount = 0; |
279
|
2 |
|
foreach ($this->dataValues as $dataValueSet) { |
280
|
2 |
|
$levelCount = max($levelCount, count($dataValueSet)); |
281
|
|
|
} |
282
|
|
|
|
283
|
2 |
|
return $levelCount; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Get Series Data Values. |
288
|
|
|
* |
289
|
|
|
* @return array of mixed |
290
|
|
|
*/ |
291
|
13 |
|
public function getDataValues() |
292
|
|
|
{ |
293
|
13 |
|
return $this->dataValues; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Get the first Series Data value. |
298
|
|
|
* |
299
|
|
|
* @return mixed |
300
|
|
|
*/ |
301
|
|
|
public function getDataValue() |
302
|
|
|
{ |
303
|
|
|
$count = count($this->dataValues); |
304
|
|
|
if ($count == 0) { |
305
|
|
|
return null; |
306
|
|
|
} elseif ($count == 1) { |
307
|
|
|
return $this->dataValues[0]; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return $this->dataValues; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Set Series Data Values. |
315
|
|
|
* |
316
|
|
|
* @param array $dataValues |
317
|
|
|
* |
318
|
|
|
* @return DataSeriesValues |
319
|
|
|
*/ |
320
|
|
|
public function setDataValues($dataValues) |
321
|
|
|
{ |
322
|
|
|
$this->dataValues = Functions::flattenArray($dataValues); |
323
|
|
|
$this->pointCount = count($dataValues); |
324
|
|
|
|
325
|
|
|
return $this; |
326
|
|
|
} |
327
|
|
|
|
328
|
13 |
|
public function refresh(Worksheet $worksheet, $flatten = true) |
329
|
|
|
{ |
330
|
13 |
|
if ($this->dataSource !== null) { |
331
|
13 |
|
$calcEngine = Calculation::getInstance($worksheet->getParent()); |
332
|
13 |
|
$newDataValues = Calculation::unwrapResult( |
333
|
13 |
|
$calcEngine->_calculateFormulaValue( |
334
|
13 |
|
'=' . $this->dataSource, |
335
|
13 |
|
null, |
336
|
13 |
|
$worksheet->getCell('A1') |
337
|
|
|
) |
338
|
|
|
); |
339
|
13 |
|
if ($flatten) { |
340
|
13 |
|
$this->dataValues = Functions::flattenArray($newDataValues); |
341
|
13 |
|
foreach ($this->dataValues as &$dataValue) { |
342
|
13 |
|
if ((!empty($dataValue)) && ($dataValue[0] == '#')) { |
343
|
13 |
|
$dataValue = 0.0; |
344
|
|
|
} |
345
|
|
|
} |
346
|
13 |
|
unset($dataValue); |
347
|
|
|
} else { |
348
|
13 |
|
$cellRange = explode('!', $this->dataSource); |
349
|
13 |
|
if (count($cellRange) > 1) { |
350
|
13 |
|
list(, $cellRange) = $cellRange; |
351
|
|
|
} |
352
|
|
|
|
353
|
13 |
|
$dimensions = Cell::rangeDimension(str_replace('$', '', $cellRange)); |
354
|
13 |
|
if (($dimensions[0] == 1) || ($dimensions[1] == 1)) { |
355
|
12 |
|
$this->dataValues = Functions::flattenArray($newDataValues); |
356
|
|
|
} else { |
357
|
2 |
|
$newArray = array_values(array_shift($newDataValues)); |
358
|
2 |
|
foreach ($newArray as $i => $newDataSet) { |
359
|
2 |
|
$newArray[$i] = [$newDataSet]; |
360
|
|
|
} |
361
|
|
|
|
362
|
2 |
|
foreach ($newDataValues as $newDataSet) { |
363
|
2 |
|
$i = 0; |
364
|
2 |
|
foreach ($newDataSet as $newDataVal) { |
365
|
2 |
|
array_unshift($newArray[$i++], $newDataVal); |
366
|
|
|
} |
367
|
|
|
} |
368
|
2 |
|
$this->dataValues = $newArray; |
369
|
|
|
} |
370
|
|
|
} |
371
|
13 |
|
$this->pointCount = count($this->dataValues); |
372
|
|
|
} |
373
|
13 |
|
} |
374
|
|
|
} |
375
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..