ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DataSeriesValues.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
11 {
12  const DATASERIES_TYPE_STRING = 'String';
13  const DATASERIES_TYPE_NUMBER = 'Number';
14 
15  private static $dataTypeValues = [
16  self::DATASERIES_TYPE_STRING,
17  self::DATASERIES_TYPE_NUMBER,
18  ];
19 
25  private $dataType;
26 
32  private $dataSource;
33 
39  private $formatCode;
40 
46  private $pointMarker;
47 
53  private $pointCount = 0;
54 
60  private $dataValues = [];
61 
67  private $fillColor;
68 
74  private $lineWidth = 12700;
75 
87  public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null, $fillColor = null)
88  {
89  $this->setDataType($dataType);
90  $this->dataSource = $dataSource;
91  $this->formatCode = $formatCode;
92  $this->pointCount = $pointCount;
93  $this->dataValues = $dataValues;
94  $this->pointMarker = $marker;
95  $this->fillColor = $fillColor;
96  }
97 
103  public function getDataType()
104  {
105  return $this->dataType;
106  }
107 
120  public function setDataType($dataType)
121  {
122  if (!in_array($dataType, self::$dataTypeValues)) {
123  throw new Exception('Invalid datatype for chart data series values');
124  }
125  $this->dataType = $dataType;
126 
127  return $this;
128  }
129 
135  public function getDataSource()
136  {
137  return $this->dataSource;
138  }
139 
147  public function setDataSource($dataSource)
148  {
149  $this->dataSource = $dataSource;
150 
151  return $this;
152  }
153 
159  public function getPointMarker()
160  {
161  return $this->pointMarker;
162  }
163 
171  public function setPointMarker($marker)
172  {
173  $this->pointMarker = $marker;
174 
175  return $this;
176  }
177 
183  public function getFormatCode()
184  {
185  return $this->formatCode;
186  }
187 
195  public function setFormatCode($formatCode)
196  {
197  $this->formatCode = $formatCode;
198 
199  return $this;
200  }
201 
207  public function getPointCount()
208  {
209  return $this->pointCount;
210  }
211 
217  public function getFillColor()
218  {
219  return $this->fillColor;
220  }
221 
229  public function setFillColor($color)
230  {
231  if (is_array($color)) {
232  foreach ($color as $colorValue) {
233  $this->validateColor($colorValue);
234  }
235  } else {
236  $this->validateColor($color);
237  }
238  $this->fillColor = $color;
239 
240  return $this;
241  }
242 
250  private function validateColor($color)
251  {
252  if (!preg_match('/^[a-f0-9]{6}$/i', $color)) {
253  throw new Exception(sprintf('Invalid hex color for chart series (color: "%s")', $color));
254  }
255 
256  return true;
257  }
258 
264  public function getLineWidth()
265  {
266  return $this->lineWidth;
267  }
268 
276  public function setLineWidth($width)
277  {
278  $minWidth = 12700;
279  $this->lineWidth = max($minWidth, $width);
280 
281  return $this;
282  }
283 
289  public function isMultiLevelSeries()
290  {
291  if (count($this->dataValues) > 0) {
292  return is_array(array_values($this->dataValues)[0]);
293  }
294 
295  return null;
296  }
297 
303  public function multiLevelCount()
304  {
305  $levelCount = 0;
306  foreach ($this->dataValues as $dataValueSet) {
307  $levelCount = max($levelCount, count($dataValueSet));
308  }
309 
310  return $levelCount;
311  }
312 
318  public function getDataValues()
319  {
320  return $this->dataValues;
321  }
322 
328  public function getDataValue()
329  {
330  $count = count($this->dataValues);
331  if ($count == 0) {
332  return null;
333  } elseif ($count == 1) {
334  return $this->dataValues[0];
335  }
336 
337  return $this->dataValues;
338  }
339 
347  public function setDataValues($dataValues)
348  {
349  $this->dataValues = Functions::flattenArray($dataValues);
350  $this->pointCount = count($dataValues);
351 
352  return $this;
353  }
354 
355  public function refresh(Worksheet $worksheet, $flatten = true): void
356  {
357  if ($this->dataSource !== null) {
358  $calcEngine = Calculation::getInstance($worksheet->getParent());
359  $newDataValues = Calculation::unwrapResult(
360  $calcEngine->_calculateFormulaValue(
361  '=' . $this->dataSource,
362  null,
363  $worksheet->getCell('A1')
364  )
365  );
366  if ($flatten) {
367  $this->dataValues = Functions::flattenArray($newDataValues);
368  foreach ($this->dataValues as &$dataValue) {
369  if (is_string($dataValue) && !empty($dataValue) && $dataValue[0] == '#') {
370  $dataValue = 0.0;
371  }
372  }
373  unset($dataValue);
374  } else {
375  [$worksheet, $cellRange] = Worksheet::extractSheetTitle($this->dataSource, true);
376  $dimensions = Coordinate::rangeDimension(str_replace('$', '', $cellRange));
377  if (($dimensions[0] == 1) || ($dimensions[1] == 1)) {
378  $this->dataValues = Functions::flattenArray($newDataValues);
379  } else {
380  $newArray = array_values(array_shift($newDataValues));
381  foreach ($newArray as $i => $newDataSet) {
382  $newArray[$i] = [$newDataSet];
383  }
384 
385  foreach ($newDataValues as $newDataSet) {
386  $i = 0;
387  foreach ($newDataSet as $newDataVal) {
388  array_unshift($newArray[$i++], $newDataVal);
389  }
390  }
391  $this->dataValues = $newArray;
392  }
393  }
394  $this->pointCount = count($this->dataValues);
395  }
396  }
397 }
validateColor($color)
Method for validating hex color.
setDataType($dataType)
Set Series Data Type.
static extractSheetTitle($pRange, $returnRange=false)
Extract worksheet title from range.
Definition: Worksheet.php:2719
getDataValue()
Get the first Series Data value.
setFormatCode($formatCode)
Set Series Format Code.
static flattenArray($array)
Convert a multi-dimensional array to a simple 1-dimensional array.
Definition: Functions.php:583
__construct($dataType=self::DATASERIES_TYPE_NUMBER, $dataSource=null, $formatCode=null, $pointCount=0, $dataValues=[], $marker=null, $fillColor=null)
Create a new DataSeriesValues object.
refresh(Worksheet $worksheet, $flatten=true)
setLineWidth($width)
Set line width for the series.
setDataValues($dataValues)
Set Series Data Values.
setDataSource($dataSource)
Set Series Data Source (formula).
static unwrapResult($value)
Remove quotes used as a wrapper to identify string values.
static getInstance(?Spreadsheet $spreadsheet=null)
Get an instance of this class.
multiLevelCount()
Return the level count of a multi-level Data Series.
isMultiLevelSeries()
Identify if the Data Series is a multi-level or a simple series.
$i
Definition: disco.tpl.php:19
getDataSource()
Get Series Data Source (formula).
static rangeDimension($pRange)
Calculate range dimension.
Definition: Coordinate.php:222
setFillColor($color)
Set fill color for series.