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] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Chart series.
  19   *
  20   * @package    core
  21   * @copyright  2016 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use coding_exception;
  29  use JsonSerializable;
  30  
  31  /**
  32   * Chart series class.
  33   *
  34   * @package    core
  35   * @copyright  2016 Frédéric Massart - FMCorz.net
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class chart_series implements JsonSerializable {
  39  
  40      /** Default type for a series. */
  41      const TYPE_DEFAULT = null;
  42      /** Series of type line. */
  43      const TYPE_LINE = 'line';
  44  
  45      /** @var string[] Colors of the series. */
  46      protected $colors = [];
  47      /** @var string Fill mode for area charts. See https://www.chartjs.org/docs/latest/charts/area.html */
  48      protected $fill = null;
  49      /** @var string Label for this series. */
  50      protected $label;
  51      /** @var string[] Labels for the values of the series. */
  52      protected $labels = null;
  53      /** @var bool Whether the line of the serie should be smooth or not. */
  54      protected $smooth = null;
  55      /** @var string Type of the series. */
  56      protected $type = self::TYPE_DEFAULT;
  57      /** @var float[] Values of the series. */
  58      protected $values = [];
  59      /** @var int Index of the X axis. */
  60      protected $xaxis = null;
  61      /** @var int Index of the Y axis. */
  62      protected $yaxis = null;
  63  
  64      /**
  65       * Constructor.
  66       *
  67       * @param string $label The label of the series.
  68       * @param float[] $values The values of this series.
  69       */
  70      public function __construct($label, $values) {
  71          $this->values = $values;
  72          $this->label = $label;
  73      }
  74  
  75      /**
  76       * Get the color.
  77       *
  78       * @return string|null
  79       */
  80      public function get_color() {
  81          return isset($this->color[0]) ? $this->color[0] : null;
  82      }
  83  
  84      /**
  85       * Get the colors for each value in the series.
  86       *
  87       * @return string[]
  88       */
  89      public function get_colors() {
  90          return $this->colors;
  91      }
  92  
  93      /**
  94       * Get the number of values in this series.
  95       *
  96       * @return int
  97       */
  98      public function get_count() {
  99          return count($this->values);
 100      }
 101  
 102      /**
 103       * Get area fill mode for series.
 104       */
 105      public function get_fill() {
 106          return $this->fill;
 107      }
 108  
 109      /**
 110       * Get the label of the series.
 111       *
 112       * @return string
 113       */
 114      public function get_label() {
 115          return $this->label;
 116      }
 117  
 118      /**
 119       * Set labels for the values of the series.
 120       *
 121       * @return array
 122       */
 123      public function get_labels() {
 124          return $this->labels;
 125      }
 126  
 127      /**
 128       * Get whether the line of the serie should be smooth or not.
 129       *
 130       * @return bool
 131       */
 132      public function get_smooth() {
 133          return $this->smooth;
 134      }
 135  
 136      /**
 137       * Get the type of series.
 138       *
 139       * @return string
 140       */
 141      public function get_type() {
 142          return $this->type;
 143      }
 144  
 145      /**
 146       * Get the values of the series.
 147       *
 148       * @return string[]
 149       */
 150      public function get_values() {
 151          return $this->values;
 152      }
 153  
 154      /**
 155       * Get the index of the X axis.
 156       *
 157       * @return int
 158       */
 159      public function get_xaxis() {
 160          return $this->xaxis;
 161      }
 162  
 163      /**
 164       * Get the index of the Y axis.
 165       *
 166       * @return int
 167       */
 168      public function get_yaxis() {
 169          return $this->yaxis;
 170      }
 171  
 172      /**
 173       * Whether there is a color per value.
 174       *
 175       * @return bool
 176       */
 177      public function has_colored_values() {
 178          return count($this->colors) == $this->get_count();
 179      }
 180  
 181      /**
 182       * Serialize the object.
 183       *
 184       * @return array
 185       */
 186      public function jsonSerialize(): array {
 187          $data = [
 188              'label' => $this->label,
 189              'labels' => $this->labels,
 190              'type' => $this->type,
 191              'values' => $this->values,
 192              'colors' => $this->colors,
 193              'fill' => $this->fill,
 194              'axes' => [
 195                  'x' => $this->xaxis,
 196                  'y' => $this->yaxis,
 197              ],
 198              'smooth' => $this->smooth
 199          ];
 200          return $data;
 201      }
 202  
 203      /**
 204       * Set the color of the series.
 205       *
 206       * @param string $color CSS compatible color.
 207       */
 208      public function set_color($color) {
 209          $this->colors = [$color];
 210      }
 211  
 212      /**
 213       * Set a color for each value in the series.
 214       *
 215       * @param string[] $colors CSS compatible colors.
 216       */
 217      public function set_colors(array $colors) {
 218          $this->colors = $colors;
 219      }
 220  
 221      /**
 222       * Set fill mode for the series.
 223       * @param string $fill
 224       */
 225      public function set_fill($fill) {
 226          $this->fill = $fill;
 227      }
 228  
 229      /**
 230       * Set labels for the values of the series.
 231       *
 232       * @param array $labels The labels for the series values.
 233       */
 234      public function set_labels($labels) {
 235          $this->labels = $labels;
 236      }
 237  
 238      /**
 239       * Set whether the line of the serie should be smooth or not.
 240       *
 241       * Only applicable for line chart or a line series, if null it assumes the chart default (not smooth).
 242       *
 243       * @param bool $smooth True if the line should be smooth, false for tensioned lines.
 244       */
 245      public function set_smooth($smooth) {
 246          $this->smooth = $smooth;
 247      }
 248  
 249      /**
 250       * Set the type of the series.
 251       *
 252       * @param string $type Constant value from self::TYPE_*.
 253       */
 254      public function set_type($type) {
 255          if (!in_array($type, [self::TYPE_DEFAULT, self::TYPE_LINE])) {
 256              throw new coding_exception('Invalid serie type.');
 257          }
 258          $this->type = $type;
 259      }
 260  
 261      /**
 262       * Set the index of the X axis.
 263       *
 264       * @param int $index The index.
 265       */
 266      public function set_xaxis($index) {
 267          $this->xaxis = $index;
 268      }
 269  
 270      /**
 271       * Set the index of the Y axis.
 272       *
 273       * @param int $index The index.
 274       */
 275      public function set_yaxis($index) {
 276          $this->yaxis = $index;
 277      }
 278  
 279  }