Legend.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 bool $overlay
  48. */
  49. public function __construct($position = self::POSITION_RIGHT, ?Layout $layout = null, $overlay = false)
  50. {
  51. $this->setPosition($position);
  52. $this->layout = $layout;
  53. $this->setOverlay($overlay);
  54. }
  55. /**
  56. * Get legend position as an excel string value.
  57. *
  58. * @return string
  59. */
  60. public function getPosition()
  61. {
  62. return $this->position;
  63. }
  64. /**
  65. * Get legend position using an excel string value.
  66. *
  67. * @param string $position see self::POSITION_*
  68. *
  69. * @return bool
  70. */
  71. public function setPosition($position)
  72. {
  73. if (!in_array($position, self::$positionXLref)) {
  74. return false;
  75. }
  76. $this->position = $position;
  77. return true;
  78. }
  79. /**
  80. * Get legend position as an Excel internal numeric value.
  81. *
  82. * @return int
  83. */
  84. public function getPositionXL()
  85. {
  86. return array_search($this->position, self::$positionXLref);
  87. }
  88. /**
  89. * Set legend position using an Excel internal numeric value.
  90. *
  91. * @param int $positionXL see self::XL_LEGEND_POSITION_*
  92. *
  93. * @return bool
  94. */
  95. public function setPositionXL($positionXL)
  96. {
  97. if (!isset(self::$positionXLref[$positionXL])) {
  98. return false;
  99. }
  100. $this->position = self::$positionXLref[$positionXL];
  101. return true;
  102. }
  103. /**
  104. * Get allow overlay of other elements?
  105. *
  106. * @return bool
  107. */
  108. public function getOverlay()
  109. {
  110. return $this->overlay;
  111. }
  112. /**
  113. * Set allow overlay of other elements?
  114. *
  115. * @param bool $overlay
  116. */
  117. public function setOverlay($overlay): void
  118. {
  119. $this->overlay = $overlay;
  120. }
  121. /**
  122. * Get Layout.
  123. *
  124. * @return Layout
  125. */
  126. public function getLayout()
  127. {
  128. return $this->layout;
  129. }
  130. }