[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/classes/ -> chart_series.php (source)

   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 Label for this series. */
  48      protected $label;
  49      /** @var string[] Labels for the values of the series. */
  50      protected $labels = null;
  51      /** @var bool Whether the line of the serie should be smooth or not. */
  52      protected $smooth = null;
  53      /** @var string Type of the series. */
  54      protected $type = self::TYPE_DEFAULT;
  55      /** @var float[] Values of the series. */
  56      protected $values = [];
  57      /** @var int Index of the X axis. */
  58      protected $xaxis = null;
  59      /** @var int Index of the Y axis. */
  60      protected $yaxis = null;
  61  
  62      /**
  63       * Constructor.
  64       *
  65       * @param string $label The label of the series.
  66       * @param float[] $values The values of this series.
  67       */
  68      public function __construct($label, $values) {
  69          $this->values = $values;
  70          $this->label = $label;
  71      }
  72  
  73      /**
  74       * Get the color.
  75       *
  76       * @return string|null
  77       */
  78      public function get_color() {
  79          return isset($this->color[0]) ? $this->color[0] : null;
  80      }
  81  
  82      /**
  83       * Get the colors for each value in the series.
  84       *
  85       * @return string[]
  86       */
  87      public function get_colors() {
  88          return $this->colors;
  89      }
  90  
  91      /**
  92       * Get the number of values in this series.
  93       *
  94       * @return int
  95       */
  96      public function get_count() {
  97          return count($this->values);
  98      }
  99  
 100      /**
 101       * Get the label of the series.
 102       *
 103       * @return string
 104       */
 105      public function get_label() {
 106          return $this->label;
 107      }
 108  
 109      /**
 110       * Set labels for the values of the series.
 111       *
 112       * @return array
 113       */
 114      public function get_labels() {
 115          return $this->labels;
 116      }
 117  
 118      /**
 119       * Get whether the line of the serie should be smooth or not.
 120       *
 121       * @return bool
 122       */
 123      public function get_smooth() {
 124          return $this->smooth;
 125      }
 126  
 127      /**
 128       * Get the type of series.
 129       *
 130       * @return string
 131       */
 132      public function get_type() {
 133          return $this->type;
 134      }
 135  
 136      /**
 137       * Get the values of the series.
 138       *
 139       * @return string[]
 140       */
 141      public function get_values() {
 142          return $this->values;
 143      }
 144  
 145      /**
 146       * Get the index of the X axis.
 147       *
 148       * @return int
 149       */
 150      public function get_xaxis() {
 151          return $this->xaxis;
 152      }
 153  
 154      /**
 155       * Get the index of the Y axis.
 156       *
 157       * @return int
 158       */
 159      public function get_yaxis() {
 160          return $this->yaxis;
 161      }
 162  
 163      /**
 164       * Whether there is a color per value.
 165       *
 166       * @return bool
 167       */
 168      public function has_colored_values() {
 169          return count($this->colors) == $this->get_count();
 170      }
 171  
 172      /**
 173       * Serialize the object.
 174       *
 175       * @return array
 176       */
 177      public function jsonSerialize() { // @codingStandardsIgnoreLine (CONTRIB-6469).
 178          $data = [
 179              'label' => $this->label,
 180              'labels' => $this->labels,
 181              'type' => $this->type,
 182              'values' => $this->values,
 183              'colors' => $this->colors,
 184              'axes' => [
 185                  'x' => $this->xaxis,
 186                  'y' => $this->yaxis,
 187              ],
 188              'smooth' => $this->smooth
 189          ];
 190          return $data;
 191      }
 192  
 193      /**
 194       * Set the color of the series.
 195       *
 196       * @param string $color CSS compatible color.
 197       */
 198      public function set_color($color) {
 199          $this->colors = [$color];
 200      }
 201  
 202      /**
 203       * Set a color for each value in the series.
 204       *
 205       * @param string[] $colors CSS compatible colors.
 206       */
 207      public function set_colors(array $colors) {
 208          $this->colors = $colors;
 209      }
 210  
 211      /**
 212       * Set labels for the values of the series.
 213       *
 214       * @param array $labels The labels for the series values.
 215       */
 216      public function set_labels($labels) {
 217          $this->labels = $labels;
 218      }
 219  
 220      /**
 221       * Set whether the line of the serie should be smooth or not.
 222       *
 223       * Only applicable for line chart or a line series, if null it assumes the chart default (not smooth).
 224       *
 225       * @param bool $smooth True if the line should be smooth, false for tensioned lines.
 226       */
 227      public function set_smooth($smooth) {
 228          $this->smooth = $smooth;
 229      }
 230  
 231      /**
 232       * Set the type of the series.
 233       *
 234       * @param string $type Constant value from self::TYPE_*.
 235       */
 236      public function set_type($type) {
 237          if (!in_array($type, [self::TYPE_DEFAULT, self::TYPE_LINE])) {
 238              throw new coding_exception('Invalid serie type.');
 239          }
 240          $this->type = $type;
 241      }
 242  
 243      /**
 244       * Set the index of the X axis.
 245       *
 246       * @param int $index The index.
 247       */
 248      public function set_xaxis($index) {
 249          $this->xaxis = $index;
 250      }
 251  
 252      /**
 253       * Set the index of the Y axis.
 254       *
 255       * @param int $index The index.
 256       */
 257      public function set_yaxis($index) {
 258          $this->yaxis = $index;
 259      }
 260  
 261  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1