Legend.php 3.5 KB

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