Legend.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. const POSITION_XLREF = [
  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. private string $position = self::POSITION_RIGHT;
  29. /**
  30. * Allow overlay of other elements?
  31. */
  32. private bool $overlay = true;
  33. /**
  34. * Legend Layout.
  35. */
  36. private ?Layout $layout;
  37. private GridLines $borderLines;
  38. private ChartColor $fillColor;
  39. private ?AxisText $legendText = null;
  40. /**
  41. * Create a new Legend.
  42. */
  43. public function __construct(string $position = self::POSITION_RIGHT, ?Layout $layout = null, bool $overlay = false)
  44. {
  45. $this->setPosition($position);
  46. $this->layout = $layout;
  47. $this->setOverlay($overlay);
  48. $this->borderLines = new GridLines();
  49. $this->fillColor = new ChartColor();
  50. }
  51. public function getFillColor(): ChartColor
  52. {
  53. return $this->fillColor;
  54. }
  55. /**
  56. * Get legend position as an excel string value.
  57. */
  58. public function getPosition(): string
  59. {
  60. return $this->position;
  61. }
  62. /**
  63. * Get legend position using an excel string value.
  64. *
  65. * @param string $position see self::POSITION_*
  66. */
  67. public function setPosition(string $position): bool
  68. {
  69. if (!in_array($position, self::POSITION_XLREF)) {
  70. return false;
  71. }
  72. $this->position = $position;
  73. return true;
  74. }
  75. /**
  76. * Get legend position as an Excel internal numeric value.
  77. */
  78. public function getPositionXL(): false|int
  79. {
  80. return array_search($this->position, self::POSITION_XLREF);
  81. }
  82. /**
  83. * Set legend position using an Excel internal numeric value.
  84. *
  85. * @param int $positionXL see self::XL_LEGEND_POSITION_*
  86. */
  87. public function setPositionXL(int $positionXL): bool
  88. {
  89. if (!isset(self::POSITION_XLREF[$positionXL])) {
  90. return false;
  91. }
  92. $this->position = self::POSITION_XLREF[$positionXL];
  93. return true;
  94. }
  95. /**
  96. * Get allow overlay of other elements?
  97. */
  98. public function getOverlay(): bool
  99. {
  100. return $this->overlay;
  101. }
  102. /**
  103. * Set allow overlay of other elements?
  104. */
  105. public function setOverlay(bool $overlay): void
  106. {
  107. $this->overlay = $overlay;
  108. }
  109. /**
  110. * Get Layout.
  111. */
  112. public function getLayout(): ?Layout
  113. {
  114. return $this->layout;
  115. }
  116. public function getLegendText(): ?AxisText
  117. {
  118. return $this->legendText;
  119. }
  120. public function setLegendText(?AxisText $legendText): self
  121. {
  122. $this->legendText = $legendText;
  123. return $this;
  124. }
  125. public function getBorderLines(): GridLines
  126. {
  127. return $this->borderLines;
  128. }
  129. public function setBorderLines(GridLines $borderLines): self
  130. {
  131. $this->borderLines = $borderLines;
  132. return $this;
  133. }
  134. /**
  135. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  136. */
  137. public function __clone()
  138. {
  139. $this->layout = ($this->layout === null) ? null : clone $this->layout;
  140. $this->legendText = ($this->legendText === null) ? null : clone $this->legendText;
  141. $this->borderLines = clone $this->borderLines;
  142. $this->fillColor = clone $this->fillColor;
  143. }
  144. }