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  
   3  namespace PhpOffice\PhpSpreadsheet\Chart;
   4  
   5  class Legend
   6  {
   7      /** Legend positions */
   8      const XL_LEGEND_POSITION_BOTTOM = -4107; //    Below the chart.
   9      const XL_LEGEND_POSITION_CORNER = 2; //    In the upper right-hand corner of the chart border.
  10      const XL_LEGEND_POSITION_CUSTOM = -4161; //    A custom position.
  11      const XL_LEGEND_POSITION_LEFT = -4131; //    Left of the chart.
  12      const XL_LEGEND_POSITION_RIGHT = -4152; //    Right of the chart.
  13      const XL_LEGEND_POSITION_TOP = -4160; //    Above the chart.
  14  
  15      const POSITION_RIGHT = 'r';
  16      const POSITION_LEFT = 'l';
  17      const POSITION_BOTTOM = 'b';
  18      const POSITION_TOP = 't';
  19      const POSITION_TOPRIGHT = 'tr';
  20  
  21      const POSITION_XLREF = [
  22          self::XL_LEGEND_POSITION_BOTTOM => self::POSITION_BOTTOM,
  23          self::XL_LEGEND_POSITION_CORNER => self::POSITION_TOPRIGHT,
  24          self::XL_LEGEND_POSITION_CUSTOM => '??',
  25          self::XL_LEGEND_POSITION_LEFT => self::POSITION_LEFT,
  26          self::XL_LEGEND_POSITION_RIGHT => self::POSITION_RIGHT,
  27          self::XL_LEGEND_POSITION_TOP => self::POSITION_TOP,
  28      ];
  29  
  30      /**
  31       * Legend position.
  32       *
  33       * @var string
  34       */
  35      private $position = self::POSITION_RIGHT;
  36  
  37      /**
  38       * Allow overlay of other elements?
  39       *
  40       * @var bool
  41       */
  42      private $overlay = true;
  43  
  44      /**
  45       * Legend Layout.
  46       *
  47       * @var ?Layout
  48       */
  49      private $layout;
  50  
  51      /**
  52       * Create a new Legend.
  53       *
  54       * @param string $position
  55       * @param bool $overlay
  56       */
  57      public function __construct($position = self::POSITION_RIGHT, ?Layout $layout = null, $overlay = false)
  58      {
  59          $this->setPosition($position);
  60          $this->layout = $layout;
  61          $this->setOverlay($overlay);
  62      }
  63  
  64      /**
  65       * Get legend position as an excel string value.
  66       *
  67       * @return string
  68       */
  69      public function getPosition()
  70      {
  71          return $this->position;
  72      }
  73  
  74      /**
  75       * Get legend position using an excel string value.
  76       *
  77       * @param string $position see self::POSITION_*
  78       *
  79       * @return bool
  80       */
  81      public function setPosition($position)
  82      {
  83          if (!in_array($position, self::POSITION_XLREF)) {
  84              return false;
  85          }
  86  
  87          $this->position = $position;
  88  
  89          return true;
  90      }
  91  
  92      /**
  93       * Get legend position as an Excel internal numeric value.
  94       *
  95       * @return false|int
  96       */
  97      public function getPositionXL()
  98      {
  99          return array_search($this->position, self::POSITION_XLREF);
 100      }
 101  
 102      /**
 103       * Set legend position using an Excel internal numeric value.
 104       *
 105       * @param int $positionXL see self::XL_LEGEND_POSITION_*
 106       *
 107       * @return bool
 108       */
 109      public function setPositionXL($positionXL)
 110      {
 111          if (!isset(self::POSITION_XLREF[$positionXL])) {
 112              return false;
 113          }
 114  
 115          $this->position = self::POSITION_XLREF[$positionXL];
 116  
 117          return true;
 118      }
 119  
 120      /**
 121       * Get allow overlay of other elements?
 122       *
 123       * @return bool
 124       */
 125      public function getOverlay()
 126      {
 127          return $this->overlay;
 128      }
 129  
 130      /**
 131       * Set allow overlay of other elements?
 132       *
 133       * @param bool $overlay
 134       */
 135      public function setOverlay($overlay): void
 136      {
 137          $this->overlay = $overlay;
 138      }
 139  
 140      /**
 141       * Get Layout.
 142       *
 143       * @return ?Layout
 144       */
 145      public function getLayout()
 146      {
 147          return $this->layout;
 148      }
 149  }