GridLines.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Chart;
  3. /**
  4. * Created by PhpStorm.
  5. * User: Wiktor Trzonkowski
  6. * Date: 7/2/14
  7. * Time: 2:36 PM.
  8. */
  9. class GridLines extends Properties
  10. {
  11. /**
  12. * Properties of Class:
  13. * Object State (State for Minor Tick Mark) @var bool
  14. * Line Properties @var array of mixed
  15. * Shadow Properties @var array of mixed
  16. * Glow Properties @var array of mixed
  17. * Soft Properties @var array of mixed.
  18. */
  19. private $objectState = false;
  20. private $lineProperties = [
  21. 'color' => [
  22. 'type' => self::EXCEL_COLOR_TYPE_STANDARD,
  23. 'value' => null,
  24. 'alpha' => 0,
  25. ],
  26. 'style' => [
  27. 'width' => '9525',
  28. 'compound' => self::LINE_STYLE_COMPOUND_SIMPLE,
  29. 'dash' => self::LINE_STYLE_DASH_SOLID,
  30. 'cap' => self::LINE_STYLE_CAP_FLAT,
  31. 'join' => self::LINE_STYLE_JOIN_BEVEL,
  32. 'arrow' => [
  33. 'head' => [
  34. 'type' => self::LINE_STYLE_ARROW_TYPE_NOARROW,
  35. 'size' => self::LINE_STYLE_ARROW_SIZE_5,
  36. ],
  37. 'end' => [
  38. 'type' => self::LINE_STYLE_ARROW_TYPE_NOARROW,
  39. 'size' => self::LINE_STYLE_ARROW_SIZE_8,
  40. ],
  41. ],
  42. ],
  43. ];
  44. private $shadowProperties = [
  45. 'presets' => self::SHADOW_PRESETS_NOSHADOW,
  46. 'effect' => null,
  47. 'color' => [
  48. 'type' => self::EXCEL_COLOR_TYPE_STANDARD,
  49. 'value' => 'black',
  50. 'alpha' => 85,
  51. ],
  52. 'size' => [
  53. 'sx' => null,
  54. 'sy' => null,
  55. 'kx' => null,
  56. ],
  57. 'blur' => null,
  58. 'direction' => null,
  59. 'distance' => null,
  60. 'algn' => null,
  61. 'rotWithShape' => null,
  62. ];
  63. private $glowProperties = [
  64. 'size' => null,
  65. 'color' => [
  66. 'type' => self::EXCEL_COLOR_TYPE_STANDARD,
  67. 'value' => 'black',
  68. 'alpha' => 40,
  69. ],
  70. ];
  71. private $softEdges = [
  72. 'size' => null,
  73. ];
  74. /**
  75. * Get Object State.
  76. *
  77. * @return bool
  78. */
  79. public function getObjectState()
  80. {
  81. return $this->objectState;
  82. }
  83. /**
  84. * Change Object State to True.
  85. *
  86. * @return GridLines
  87. */
  88. private function activateObject()
  89. {
  90. $this->objectState = true;
  91. return $this;
  92. }
  93. /**
  94. * Set Line Color Properties.
  95. *
  96. * @param string $value
  97. * @param int $alpha
  98. * @param string $type
  99. */
  100. public function setLineColorProperties($value, $alpha = 0, $type = self::EXCEL_COLOR_TYPE_STANDARD)
  101. {
  102. $this->activateObject()
  103. ->lineProperties['color'] = $this->setColorProperties(
  104. $value,
  105. $alpha,
  106. $type
  107. );
  108. }
  109. /**
  110. * Set Line Color Properties.
  111. *
  112. * @param float $line_width
  113. * @param string $compound_type
  114. * @param string $dash_type
  115. * @param string $cap_type
  116. * @param string $join_type
  117. * @param string $head_arrow_type
  118. * @param string $head_arrow_size
  119. * @param string $end_arrow_type
  120. * @param string $end_arrow_size
  121. */
  122. public function setLineStyleProperties($line_width = null, $compound_type = null, $dash_type = null, $cap_type = null, $join_type = null, $head_arrow_type = null, $head_arrow_size = null, $end_arrow_type = null, $end_arrow_size = null)
  123. {
  124. $this->activateObject();
  125. ($line_width !== null)
  126. ? $this->lineProperties['style']['width'] = $this->getExcelPointsWidth((float) $line_width)
  127. : null;
  128. ($compound_type !== null)
  129. ? $this->lineProperties['style']['compound'] = (string) $compound_type
  130. : null;
  131. ($dash_type !== null)
  132. ? $this->lineProperties['style']['dash'] = (string) $dash_type
  133. : null;
  134. ($cap_type !== null)
  135. ? $this->lineProperties['style']['cap'] = (string) $cap_type
  136. : null;
  137. ($join_type !== null)
  138. ? $this->lineProperties['style']['join'] = (string) $join_type
  139. : null;
  140. ($head_arrow_type !== null)
  141. ? $this->lineProperties['style']['arrow']['head']['type'] = (string) $head_arrow_type
  142. : null;
  143. ($head_arrow_size !== null)
  144. ? $this->lineProperties['style']['arrow']['head']['size'] = (string) $head_arrow_size
  145. : null;
  146. ($end_arrow_type !== null)
  147. ? $this->lineProperties['style']['arrow']['end']['type'] = (string) $end_arrow_type
  148. : null;
  149. ($end_arrow_size !== null)
  150. ? $this->lineProperties['style']['arrow']['end']['size'] = (string) $end_arrow_size
  151. : null;
  152. }
  153. /**
  154. * Get Line Color Property.
  155. *
  156. * @param string $parameter
  157. *
  158. * @return string
  159. */
  160. public function getLineColorProperty($parameter)
  161. {
  162. return $this->lineProperties['color'][$parameter];
  163. }
  164. /**
  165. * Get Line Style Property.
  166. *
  167. * @param array|string $elements
  168. *
  169. * @return string
  170. */
  171. public function getLineStyleProperty($elements)
  172. {
  173. return $this->getArrayElementsValue($this->lineProperties['style'], $elements);
  174. }
  175. /**
  176. * Set Glow Properties.
  177. *
  178. * @param float $size
  179. * @param string $color_value
  180. * @param int $color_alpha
  181. * @param string $color_type
  182. */
  183. public function setGlowProperties($size, $color_value = null, $color_alpha = null, $color_type = null)
  184. {
  185. $this
  186. ->activateObject()
  187. ->setGlowSize($size)
  188. ->setGlowColor($color_value, $color_alpha, $color_type);
  189. }
  190. /**
  191. * Get Glow Color Property.
  192. *
  193. * @param string $property
  194. *
  195. * @return string
  196. */
  197. public function getGlowColor($property)
  198. {
  199. return $this->glowProperties['color'][$property];
  200. }
  201. /**
  202. * Get Glow Size.
  203. *
  204. * @return string
  205. */
  206. public function getGlowSize()
  207. {
  208. return $this->glowProperties['size'];
  209. }
  210. /**
  211. * Set Glow Size.
  212. *
  213. * @param float $size
  214. *
  215. * @return GridLines
  216. */
  217. private function setGlowSize($size)
  218. {
  219. $this->glowProperties['size'] = $this->getExcelPointsWidth((float) $size);
  220. return $this;
  221. }
  222. /**
  223. * Set Glow Color.
  224. *
  225. * @param string $color
  226. * @param int $alpha
  227. * @param string $type
  228. *
  229. * @return GridLines
  230. */
  231. private function setGlowColor($color, $alpha, $type)
  232. {
  233. if ($color !== null) {
  234. $this->glowProperties['color']['value'] = (string) $color;
  235. }
  236. if ($alpha !== null) {
  237. $this->glowProperties['color']['alpha'] = $this->getTrueAlpha((int) $alpha);
  238. }
  239. if ($type !== null) {
  240. $this->glowProperties['color']['type'] = (string) $type;
  241. }
  242. return $this;
  243. }
  244. /**
  245. * Get Line Style Arrow Parameters.
  246. *
  247. * @param string $arrow_selector
  248. * @param string $property_selector
  249. *
  250. * @return string
  251. */
  252. public function getLineStyleArrowParameters($arrow_selector, $property_selector)
  253. {
  254. return $this->getLineStyleArrowSize($this->lineProperties['style']['arrow'][$arrow_selector]['size'], $property_selector);
  255. }
  256. /**
  257. * Set Shadow Properties.
  258. *
  259. * @param int $sh_presets
  260. * @param string $sh_color_value
  261. * @param string $sh_color_type
  262. * @param int $sh_color_alpha
  263. * @param string $sh_blur
  264. * @param int $sh_angle
  265. * @param float $sh_distance
  266. */
  267. public function setShadowProperties($sh_presets, $sh_color_value = null, $sh_color_type = null, $sh_color_alpha = null, $sh_blur = null, $sh_angle = null, $sh_distance = null)
  268. {
  269. $this->activateObject()
  270. ->setShadowPresetsProperties((int) $sh_presets)
  271. ->setShadowColor(
  272. $sh_color_value === null ? $this->shadowProperties['color']['value'] : $sh_color_value,
  273. $sh_color_alpha === null ? (int) $this->shadowProperties['color']['alpha'] : $this->getTrueAlpha($sh_color_alpha),
  274. $sh_color_type === null ? $this->shadowProperties['color']['type'] : $sh_color_type
  275. )
  276. ->setShadowBlur($sh_blur)
  277. ->setShadowAngle($sh_angle)
  278. ->setShadowDistance($sh_distance);
  279. }
  280. /**
  281. * Set Shadow Presets Properties.
  282. *
  283. * @param int $shadow_presets
  284. *
  285. * @return GridLines
  286. */
  287. private function setShadowPresetsProperties($shadow_presets)
  288. {
  289. $this->shadowProperties['presets'] = $shadow_presets;
  290. $this->setShadowProperiesMapValues($this->getShadowPresetsMap($shadow_presets));
  291. return $this;
  292. }
  293. /**
  294. * Set Shadow Properties Values.
  295. *
  296. * @param array $properties_map
  297. * @param mixed &$reference
  298. *
  299. * @return GridLines
  300. */
  301. private function setShadowProperiesMapValues(array $properties_map, &$reference = null)
  302. {
  303. $base_reference = $reference;
  304. foreach ($properties_map as $property_key => $property_val) {
  305. if (is_array($property_val)) {
  306. if ($reference === null) {
  307. $reference = &$this->shadowProperties[$property_key];
  308. } else {
  309. $reference = &$reference[$property_key];
  310. }
  311. $this->setShadowProperiesMapValues($property_val, $reference);
  312. } else {
  313. if ($base_reference === null) {
  314. $this->shadowProperties[$property_key] = $property_val;
  315. } else {
  316. $reference[$property_key] = $property_val;
  317. }
  318. }
  319. }
  320. return $this;
  321. }
  322. /**
  323. * Set Shadow Color.
  324. *
  325. * @param string $color
  326. * @param int $alpha
  327. * @param string $type
  328. *
  329. * @return GridLines
  330. */
  331. private function setShadowColor($color, $alpha, $type)
  332. {
  333. if ($color !== null) {
  334. $this->shadowProperties['color']['value'] = (string) $color;
  335. }
  336. if ($alpha !== null) {
  337. $this->shadowProperties['color']['alpha'] = $this->getTrueAlpha((int) $alpha);
  338. }
  339. if ($type !== null) {
  340. $this->shadowProperties['color']['type'] = (string) $type;
  341. }
  342. return $this;
  343. }
  344. /**
  345. * Set Shadow Blur.
  346. *
  347. * @param float $blur
  348. *
  349. * @return GridLines
  350. */
  351. private function setShadowBlur($blur)
  352. {
  353. if ($blur !== null) {
  354. $this->shadowProperties['blur'] = (string) $this->getExcelPointsWidth($blur);
  355. }
  356. return $this;
  357. }
  358. /**
  359. * Set Shadow Angle.
  360. *
  361. * @param int $angle
  362. *
  363. * @return GridLines
  364. */
  365. private function setShadowAngle($angle)
  366. {
  367. if ($angle !== null) {
  368. $this->shadowProperties['direction'] = (string) $this->getExcelPointsAngle($angle);
  369. }
  370. return $this;
  371. }
  372. /**
  373. * Set Shadow Distance.
  374. *
  375. * @param float $distance
  376. *
  377. * @return GridLines
  378. */
  379. private function setShadowDistance($distance)
  380. {
  381. if ($distance !== null) {
  382. $this->shadowProperties['distance'] = (string) $this->getExcelPointsWidth($distance);
  383. }
  384. return $this;
  385. }
  386. /**
  387. * Get Shadow Property.
  388. *
  389. * @param string|string[] $elements
  390. *
  391. * @return string
  392. */
  393. public function getShadowProperty($elements)
  394. {
  395. return $this->getArrayElementsValue($this->shadowProperties, $elements);
  396. }
  397. /**
  398. * Set Soft Edges Size.
  399. *
  400. * @param float $size
  401. */
  402. public function setSoftEdgesSize($size)
  403. {
  404. if ($size !== null) {
  405. $this->activateObject();
  406. $this->softEdges['size'] = (string) $this->getExcelPointsWidth($size);
  407. }
  408. }
  409. /**
  410. * Get Soft Edges Size.
  411. *
  412. * @return string
  413. */
  414. public function getSoftEdgesSize()
  415. {
  416. return $this->softEdges['size'];
  417. }
  418. }