Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Chart;
   4  
   5  use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
   6  
   7  class DataSeries
   8  {
   9      const TYPE_BARCHART = 'barChart';
  10      const TYPE_BARCHART_3D = 'bar3DChart';
  11      const TYPE_LINECHART = 'lineChart';
  12      const TYPE_LINECHART_3D = 'line3DChart';
  13      const TYPE_AREACHART = 'areaChart';
  14      const TYPE_AREACHART_3D = 'area3DChart';
  15      const TYPE_PIECHART = 'pieChart';
  16      const TYPE_PIECHART_3D = 'pie3DChart';
  17      const TYPE_DOUGHNUTCHART = 'doughnutChart';
  18      const TYPE_DONUTCHART = self::TYPE_DOUGHNUTCHART; // Synonym
  19      const TYPE_SCATTERCHART = 'scatterChart';
  20      const TYPE_SURFACECHART = 'surfaceChart';
  21      const TYPE_SURFACECHART_3D = 'surface3DChart';
  22      const TYPE_RADARCHART = 'radarChart';
  23      const TYPE_BUBBLECHART = 'bubbleChart';
  24      const TYPE_STOCKCHART = 'stockChart';
  25      const TYPE_CANDLECHART = self::TYPE_STOCKCHART; // Synonym
  26  
  27      const GROUPING_CLUSTERED = 'clustered';
  28      const GROUPING_STACKED = 'stacked';
  29      const GROUPING_PERCENT_STACKED = 'percentStacked';
  30      const GROUPING_STANDARD = 'standard';
  31  
  32      const DIRECTION_BAR = 'bar';
  33      const DIRECTION_HORIZONTAL = self::DIRECTION_BAR;
  34      const DIRECTION_COL = 'col';
  35      const DIRECTION_COLUMN = self::DIRECTION_COL;
  36      const DIRECTION_VERTICAL = self::DIRECTION_COL;
  37  
  38      const STYLE_LINEMARKER = 'lineMarker';
  39      const STYLE_SMOOTHMARKER = 'smoothMarker';
  40      const STYLE_MARKER = 'marker';
  41      const STYLE_FILLED = 'filled';
  42  
  43      const EMPTY_AS_GAP = 'gap';
  44      const EMPTY_AS_ZERO = 'zero';
  45      const EMPTY_AS_SPAN = 'span';
  46  
  47      /**
  48       * Series Plot Type.
  49       *
  50       * @var string
  51       */
  52      private $plotType;
  53  
  54      /**
  55       * Plot Grouping Type.
  56       *
  57       * @var string
  58       */
  59      private $plotGrouping;
  60  
  61      /**
  62       * Plot Direction.
  63       *
  64       * @var string
  65       */
  66      private $plotDirection;
  67  
  68      /**
  69       * Plot Style.
  70       *
  71       * @var null|string
  72       */
  73      private $plotStyle;
  74  
  75      /**
  76       * Order of plots in Series.
  77       *
  78       * @var int[]
  79       */
  80      private $plotOrder = [];
  81  
  82      /**
  83       * Plot Label.
  84       *
  85       * @var DataSeriesValues[]
  86       */
  87      private $plotLabel = [];
  88  
  89      /**
  90       * Plot Category.
  91       *
  92       * @var DataSeriesValues[]
  93       */
  94      private $plotCategory = [];
  95  
  96      /**
  97       * Smooth Line. Must be specified for both DataSeries and DataSeriesValues.
  98       *
  99       * @var bool
 100       */
 101      private $smoothLine;
 102  
 103      /**
 104       * Plot Values.
 105       *
 106       * @var DataSeriesValues[]
 107       */
 108      private $plotValues = [];
 109  
 110      /**
 111       * Plot Bubble Sizes.
 112       *
 113       * @var DataSeriesValues[]
 114       */
 115      private $plotBubbleSizes = [];
 116  
 117      /**
 118       * Create a new DataSeries.
 119       *
 120       * @param null|mixed $plotType
 121       * @param null|mixed $plotGrouping
 122       * @param int[] $plotOrder
 123       * @param DataSeriesValues[] $plotLabel
 124       * @param DataSeriesValues[] $plotCategory
 125       * @param DataSeriesValues[] $plotValues
 126       * @param null|string $plotDirection
 127       * @param bool $smoothLine
 128       * @param null|string $plotStyle
 129       */
 130      public function __construct($plotType = null, $plotGrouping = null, array $plotOrder = [], array $plotLabel = [], array $plotCategory = [], array $plotValues = [], $plotDirection = null, $smoothLine = false, $plotStyle = null)
 131      {
 132          $this->plotType = $plotType;
 133          $this->plotGrouping = $plotGrouping;
 134          $this->plotOrder = $plotOrder;
 135          $keys = array_keys($plotValues);
 136          $this->plotValues = $plotValues;
 137          if (!isset($plotLabel[$keys[0]])) {
 138              $plotLabel[$keys[0]] = new DataSeriesValues();
 139          }
 140          $this->plotLabel = $plotLabel;
 141  
 142          if (!isset($plotCategory[$keys[0]])) {
 143              $plotCategory[$keys[0]] = new DataSeriesValues();
 144          }
 145          $this->plotCategory = $plotCategory;
 146  
 147          $this->smoothLine = $smoothLine;
 148          $this->plotStyle = $plotStyle;
 149  
 150          if ($plotDirection === null) {
 151              $plotDirection = self::DIRECTION_COL;
 152          }
 153          $this->plotDirection = $plotDirection;
 154      }
 155  
 156      /**
 157       * Get Plot Type.
 158       *
 159       * @return string
 160       */
 161      public function getPlotType()
 162      {
 163          return $this->plotType;
 164      }
 165  
 166      /**
 167       * Set Plot Type.
 168       *
 169       * @param string $plotType
 170       *
 171       * @return $this
 172       */
 173      public function setPlotType($plotType)
 174      {
 175          $this->plotType = $plotType;
 176  
 177          return $this;
 178      }
 179  
 180      /**
 181       * Get Plot Grouping Type.
 182       *
 183       * @return string
 184       */
 185      public function getPlotGrouping()
 186      {
 187          return $this->plotGrouping;
 188      }
 189  
 190      /**
 191       * Set Plot Grouping Type.
 192       *
 193       * @param string $groupingType
 194       *
 195       * @return $this
 196       */
 197      public function setPlotGrouping($groupingType)
 198      {
 199          $this->plotGrouping = $groupingType;
 200  
 201          return $this;
 202      }
 203  
 204      /**
 205       * Get Plot Direction.
 206       *
 207       * @return string
 208       */
 209      public function getPlotDirection()
 210      {
 211          return $this->plotDirection;
 212      }
 213  
 214      /**
 215       * Set Plot Direction.
 216       *
 217       * @param string $plotDirection
 218       *
 219       * @return $this
 220       */
 221      public function setPlotDirection($plotDirection)
 222      {
 223          $this->plotDirection = $plotDirection;
 224  
 225          return $this;
 226      }
 227  
 228      /**
 229       * Get Plot Order.
 230       *
 231       * @return int[]
 232       */
 233      public function getPlotOrder()
 234      {
 235          return $this->plotOrder;
 236      }
 237  
 238      /**
 239       * Get Plot Labels.
 240       *
 241       * @return DataSeriesValues[]
 242       */
 243      public function getPlotLabels()
 244      {
 245          return $this->plotLabel;
 246      }
 247  
 248      /**
 249       * Get Plot Label by Index.
 250       *
 251       * @param mixed $index
 252       *
 253       * @return DataSeriesValues|false
 254       */
 255      public function getPlotLabelByIndex($index)
 256      {
 257          $keys = array_keys($this->plotLabel);
 258          if (in_array($index, $keys)) {
 259              return $this->plotLabel[$index];
 260          }
 261  
 262          return false;
 263      }
 264  
 265      /**
 266       * Get Plot Categories.
 267       *
 268       * @return DataSeriesValues[]
 269       */
 270      public function getPlotCategories()
 271      {
 272          return $this->plotCategory;
 273      }
 274  
 275      /**
 276       * Get Plot Category by Index.
 277       *
 278       * @param mixed $index
 279       *
 280       * @return DataSeriesValues|false
 281       */
 282      public function getPlotCategoryByIndex($index)
 283      {
 284          $keys = array_keys($this->plotCategory);
 285          if (in_array($index, $keys)) {
 286              return $this->plotCategory[$index];
 287          } elseif (isset($keys[$index])) {
 288              return $this->plotCategory[$keys[$index]];
 289          }
 290  
 291          return false;
 292      }
 293  
 294      /**
 295       * Get Plot Style.
 296       *
 297       * @return null|string
 298       */
 299      public function getPlotStyle()
 300      {
 301          return $this->plotStyle;
 302      }
 303  
 304      /**
 305       * Set Plot Style.
 306       *
 307       * @param null|string $plotStyle
 308       *
 309       * @return $this
 310       */
 311      public function setPlotStyle($plotStyle)
 312      {
 313          $this->plotStyle = $plotStyle;
 314  
 315          return $this;
 316      }
 317  
 318      /**
 319       * Get Plot Values.
 320       *
 321       * @return DataSeriesValues[]
 322       */
 323      public function getPlotValues()
 324      {
 325          return $this->plotValues;
 326      }
 327  
 328      /**
 329       * Get Plot Values by Index.
 330       *
 331       * @param mixed $index
 332       *
 333       * @return DataSeriesValues|false
 334       */
 335      public function getPlotValuesByIndex($index)
 336      {
 337          $keys = array_keys($this->plotValues);
 338          if (in_array($index, $keys)) {
 339              return $this->plotValues[$index];
 340          }
 341  
 342          return false;
 343      }
 344  
 345      /**
 346       * Get Plot Bubble Sizes.
 347       *
 348       * @return DataSeriesValues[]
 349       */
 350      public function getPlotBubbleSizes(): array
 351      {
 352          return $this->plotBubbleSizes;
 353      }
 354  
 355      /**
 356       * Set Plot Bubble Sizes.
 357       *
 358       * @param DataSeriesValues[] $plotBubbleSizes
 359       */
 360      public function setPlotBubbleSizes(array $plotBubbleSizes): self
 361      {
 362          $this->plotBubbleSizes = $plotBubbleSizes;
 363  
 364          return $this;
 365      }
 366  
 367      /**
 368       * Get Number of Plot Series.
 369       *
 370       * @return int
 371       */
 372      public function getPlotSeriesCount()
 373      {
 374          return count($this->plotValues);
 375      }
 376  
 377      /**
 378       * Get Smooth Line.
 379       *
 380       * @return bool
 381       */
 382      public function getSmoothLine()
 383      {
 384          return $this->smoothLine;
 385      }
 386  
 387      /**
 388       * Set Smooth Line.
 389       *
 390       * @param bool $smoothLine
 391       *
 392       * @return $this
 393       */
 394      public function setSmoothLine($smoothLine)
 395      {
 396          $this->smoothLine = $smoothLine;
 397  
 398          return $this;
 399      }
 400  
 401      public function refresh(Worksheet $worksheet): void
 402      {
 403          foreach ($this->plotValues as $plotValues) {
 404              if ($plotValues !== null) {
 405                  $plotValues->refresh($worksheet, true);
 406              }
 407          }
 408          foreach ($this->plotLabel as $plotValues) {
 409              if ($plotValues !== null) {
 410                  $plotValues->refresh($worksheet, true);
 411              }
 412          }
 413          foreach ($this->plotCategory as $plotValues) {
 414              if ($plotValues !== null) {
 415                  $plotValues->refresh($worksheet, false);
 416              }
 417          }
 418      }
 419  }