Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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      /** @var GridLines */
  52      private $borderLines;
  53  
  54      /** @var ChartColor */
  55      private $fillColor;
  56  
  57      /** @var ?AxisText */
  58      private $legendText;
  59  
  60      /**
  61       * Create a new Legend.
  62       *
  63       * @param string $position
  64       * @param ?Layout $layout
  65       * @param bool $overlay
  66       */
  67      public function __construct($position = self::POSITION_RIGHT, ?Layout $layout = null, $overlay = false)
  68      {
  69          $this->setPosition($position);
  70          $this->layout = $layout;
  71          $this->setOverlay($overlay);
  72          $this->borderLines = new GridLines();
  73          $this->fillColor = new ChartColor();
  74      }
  75  
  76      public function getFillColor(): ChartColor
  77      {
  78          return $this->fillColor;
  79      }
  80  
  81      /**
  82       * Get legend position as an excel string value.
  83       *
  84       * @return string
  85       */
  86      public function getPosition()
  87      {
  88          return $this->position;
  89      }
  90  
  91      /**
  92       * Get legend position using an excel string value.
  93       *
  94       * @param string $position see self::POSITION_*
  95       *
  96       * @return bool
  97       */
  98      public function setPosition($position)
  99      {
 100          if (!in_array($position, self::POSITION_XLREF)) {
 101              return false;
 102          }
 103  
 104          $this->position = $position;
 105  
 106          return true;
 107      }
 108  
 109      /**
 110       * Get legend position as an Excel internal numeric value.
 111       *
 112       * @return false|int
 113       */
 114      public function getPositionXL()
 115      {
 116          // Scrutinizer thinks the following could return string. It is wrong.
 117          return array_search($this->position, self::POSITION_XLREF);
 118      }
 119  
 120      /**
 121       * Set legend position using an Excel internal numeric value.
 122       *
 123       * @param int $positionXL see self::XL_LEGEND_POSITION_*
 124       *
 125       * @return bool
 126       */
 127      public function setPositionXL($positionXL)
 128      {
 129          if (!isset(self::POSITION_XLREF[$positionXL])) {
 130              return false;
 131          }
 132  
 133          $this->position = self::POSITION_XLREF[$positionXL];
 134  
 135          return true;
 136      }
 137  
 138      /**
 139       * Get allow overlay of other elements?
 140       *
 141       * @return bool
 142       */
 143      public function getOverlay()
 144      {
 145          return $this->overlay;
 146      }
 147  
 148      /**
 149       * Set allow overlay of other elements?
 150       *
 151       * @param bool $overlay
 152       */
 153      public function setOverlay($overlay): void
 154      {
 155          $this->overlay = $overlay;
 156      }
 157  
 158      /**
 159       * Get Layout.
 160       *
 161       * @return ?Layout
 162       */
 163      public function getLayout()
 164      {
 165          return $this->layout;
 166      }
 167  
 168      public function getLegendText(): ?AxisText
 169      {
 170          return $this->legendText;
 171      }
 172  
 173      public function setLegendText(?AxisText $legendText): self
 174      {
 175          $this->legendText = $legendText;
 176  
 177          return $this;
 178      }
 179  
 180      public function getBorderLines(): GridLines
 181      {
 182          return $this->borderLines;
 183      }
 184  
 185      public function setBorderLines(GridLines $borderLines): self
 186      {
 187          $this->borderLines = $borderLines;
 188  
 189          return $this;
 190      }
 191  }