Axis.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Chart;
  3. /**
  4. * Created by PhpStorm.
  5. * User: Wiktor Trzonkowski
  6. * Date: 6/17/14
  7. * Time: 12:11 PM.
  8. */
  9. class Axis extends Properties
  10. {
  11. /**
  12. * Axis Number.
  13. *
  14. * @var array of mixed
  15. */
  16. private $axisNumber = [
  17. 'format' => self::FORMAT_CODE_GENERAL,
  18. 'source_linked' => 1,
  19. ];
  20. /**
  21. * Axis Options.
  22. *
  23. * @var array of mixed
  24. */
  25. private $axisOptions = [
  26. 'minimum' => null,
  27. 'maximum' => null,
  28. 'major_unit' => null,
  29. 'minor_unit' => null,
  30. 'orientation' => self::ORIENTATION_NORMAL,
  31. 'minor_tick_mark' => self::TICK_MARK_NONE,
  32. 'major_tick_mark' => self::TICK_MARK_NONE,
  33. 'axis_labels' => self::AXIS_LABELS_NEXT_TO,
  34. 'horizontal_crosses' => self::HORIZONTAL_CROSSES_AUTOZERO,
  35. 'horizontal_crosses_value' => null,
  36. ];
  37. /**
  38. * Fill Properties.
  39. *
  40. * @var array of mixed
  41. */
  42. private $fillProperties = [
  43. 'type' => self::EXCEL_COLOR_TYPE_ARGB,
  44. 'value' => null,
  45. 'alpha' => 0,
  46. ];
  47. /**
  48. * Line Properties.
  49. *
  50. * @var array of mixed
  51. */
  52. private $lineProperties = [
  53. 'type' => self::EXCEL_COLOR_TYPE_ARGB,
  54. 'value' => null,
  55. 'alpha' => 0,
  56. ];
  57. /**
  58. * Line Style Properties.
  59. *
  60. * @var array of mixed
  61. */
  62. private $lineStyleProperties = [
  63. 'width' => '9525',
  64. 'compound' => self::LINE_STYLE_COMPOUND_SIMPLE,
  65. 'dash' => self::LINE_STYLE_DASH_SOLID,
  66. 'cap' => self::LINE_STYLE_CAP_FLAT,
  67. 'join' => self::LINE_STYLE_JOIN_BEVEL,
  68. 'arrow' => [
  69. 'head' => [
  70. 'type' => self::LINE_STYLE_ARROW_TYPE_NOARROW,
  71. 'size' => self::LINE_STYLE_ARROW_SIZE_5,
  72. ],
  73. 'end' => [
  74. 'type' => self::LINE_STYLE_ARROW_TYPE_NOARROW,
  75. 'size' => self::LINE_STYLE_ARROW_SIZE_8,
  76. ],
  77. ],
  78. ];
  79. /**
  80. * Shadow Properties.
  81. *
  82. * @var array of mixed
  83. */
  84. private $shadowProperties = [
  85. 'presets' => self::SHADOW_PRESETS_NOSHADOW,
  86. 'effect' => null,
  87. 'color' => [
  88. 'type' => self::EXCEL_COLOR_TYPE_STANDARD,
  89. 'value' => 'black',
  90. 'alpha' => 40,
  91. ],
  92. 'size' => [
  93. 'sx' => null,
  94. 'sy' => null,
  95. 'kx' => null,
  96. ],
  97. 'blur' => null,
  98. 'direction' => null,
  99. 'distance' => null,
  100. 'algn' => null,
  101. 'rotWithShape' => null,
  102. ];
  103. /**
  104. * Glow Properties.
  105. *
  106. * @var array of mixed
  107. */
  108. private $glowProperties = [
  109. 'size' => null,
  110. 'color' => [
  111. 'type' => self::EXCEL_COLOR_TYPE_STANDARD,
  112. 'value' => 'black',
  113. 'alpha' => 40,
  114. ],
  115. ];
  116. /**
  117. * Soft Edge Properties.
  118. *
  119. * @var array of mixed
  120. */
  121. private $softEdges = [
  122. 'size' => null,
  123. ];
  124. /**
  125. * Get Series Data Type.
  126. *
  127. * @param mixed $format_code
  128. *
  129. * @return string
  130. */
  131. public function setAxisNumberProperties($format_code)
  132. {
  133. $this->axisNumber['format'] = (string) $format_code;
  134. $this->axisNumber['source_linked'] = 0;
  135. }
  136. /**
  137. * Get Axis Number Format Data Type.
  138. *
  139. * @return string
  140. */
  141. public function getAxisNumberFormat()
  142. {
  143. return $this->axisNumber['format'];
  144. }
  145. /**
  146. * Get Axis Number Source Linked.
  147. *
  148. * @return string
  149. */
  150. public function getAxisNumberSourceLinked()
  151. {
  152. return (string) $this->axisNumber['source_linked'];
  153. }
  154. /**
  155. * Set Axis Options Properties.
  156. *
  157. * @param string $axis_labels
  158. * @param string $horizontal_crosses_value
  159. * @param string $horizontal_crosses
  160. * @param string $axis_orientation
  161. * @param string $major_tmt
  162. * @param string $minor_tmt
  163. * @param string $minimum
  164. * @param string $maximum
  165. * @param string $major_unit
  166. * @param string $minor_unit
  167. */
  168. public function setAxisOptionsProperties($axis_labels, $horizontal_crosses_value = null, $horizontal_crosses = null, $axis_orientation = null, $major_tmt = null, $minor_tmt = null, $minimum = null, $maximum = null, $major_unit = null, $minor_unit = null)
  169. {
  170. $this->axisOptions['axis_labels'] = (string) $axis_labels;
  171. ($horizontal_crosses_value !== null) ? $this->axisOptions['horizontal_crosses_value'] = (string) $horizontal_crosses_value : null;
  172. ($horizontal_crosses !== null) ? $this->axisOptions['horizontal_crosses'] = (string) $horizontal_crosses : null;
  173. ($axis_orientation !== null) ? $this->axisOptions['orientation'] = (string) $axis_orientation : null;
  174. ($major_tmt !== null) ? $this->axisOptions['major_tick_mark'] = (string) $major_tmt : null;
  175. ($minor_tmt !== null) ? $this->axisOptions['minor_tick_mark'] = (string) $minor_tmt : null;
  176. ($minor_tmt !== null) ? $this->axisOptions['minor_tick_mark'] = (string) $minor_tmt : null;
  177. ($minimum !== null) ? $this->axisOptions['minimum'] = (string) $minimum : null;
  178. ($maximum !== null) ? $this->axisOptions['maximum'] = (string) $maximum : null;
  179. ($major_unit !== null) ? $this->axisOptions['major_unit'] = (string) $major_unit : null;
  180. ($minor_unit !== null) ? $this->axisOptions['minor_unit'] = (string) $minor_unit : null;
  181. }
  182. /**
  183. * Get Axis Options Property.
  184. *
  185. * @param string $property
  186. *
  187. * @return string
  188. */
  189. public function getAxisOptionsProperty($property)
  190. {
  191. return $this->axisOptions[$property];
  192. }
  193. /**
  194. * Set Axis Orientation Property.
  195. *
  196. * @param string $orientation
  197. */
  198. public function setAxisOrientation($orientation)
  199. {
  200. $this->axisOptions['orientation'] = (string) $orientation;
  201. }
  202. /**
  203. * Set Fill Property.
  204. *
  205. * @param string $color
  206. * @param int $alpha
  207. * @param string $type
  208. */
  209. public function setFillParameters($color, $alpha = 0, $type = self::EXCEL_COLOR_TYPE_ARGB)
  210. {
  211. $this->fillProperties = $this->setColorProperties($color, $alpha, $type);
  212. }
  213. /**
  214. * Set Line Property.
  215. *
  216. * @param string $color
  217. * @param int $alpha
  218. * @param string $type
  219. */
  220. public function setLineParameters($color, $alpha = 0, $type = self::EXCEL_COLOR_TYPE_ARGB)
  221. {
  222. $this->lineProperties = $this->setColorProperties($color, $alpha, $type);
  223. }
  224. /**
  225. * Get Fill Property.
  226. *
  227. * @param string $property
  228. *
  229. * @return string
  230. */
  231. public function getFillProperty($property)
  232. {
  233. return $this->fillProperties[$property];
  234. }
  235. /**
  236. * Get Line Property.
  237. *
  238. * @param string $property
  239. *
  240. * @return string
  241. */
  242. public function getLineProperty($property)
  243. {
  244. return $this->lineProperties[$property];
  245. }
  246. /**
  247. * Set Line Style Properties.
  248. *
  249. * @param float $line_width
  250. * @param string $compound_type
  251. * @param string $dash_type
  252. * @param string $cap_type
  253. * @param string $join_type
  254. * @param string $head_arrow_type
  255. * @param string $head_arrow_size
  256. * @param string $end_arrow_type
  257. * @param string $end_arrow_size
  258. */
  259. 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)
  260. {
  261. ($line_width !== null) ? $this->lineStyleProperties['width'] = $this->getExcelPointsWidth((float) $line_width) : null;
  262. ($compound_type !== null) ? $this->lineStyleProperties['compound'] = (string) $compound_type : null;
  263. ($dash_type !== null) ? $this->lineStyleProperties['dash'] = (string) $dash_type : null;
  264. ($cap_type !== null) ? $this->lineStyleProperties['cap'] = (string) $cap_type : null;
  265. ($join_type !== null) ? $this->lineStyleProperties['join'] = (string) $join_type : null;
  266. ($head_arrow_type !== null) ? $this->lineStyleProperties['arrow']['head']['type'] = (string) $head_arrow_type : null;
  267. ($head_arrow_size !== null) ? $this->lineStyleProperties['arrow']['head']['size'] = (string) $head_arrow_size : null;
  268. ($end_arrow_type !== null) ? $this->lineStyleProperties['arrow']['end']['type'] = (string) $end_arrow_type : null;
  269. ($end_arrow_size !== null) ? $this->lineStyleProperties['arrow']['end']['size'] = (string) $end_arrow_size : null;
  270. }
  271. /**
  272. * Get Line Style Property.
  273. *
  274. * @param array|string $elements
  275. *
  276. * @return string
  277. */
  278. public function getLineStyleProperty($elements)
  279. {
  280. return $this->getArrayElementsValue($this->lineStyleProperties, $elements);
  281. }
  282. /**
  283. * Get Line Style Arrow Excel Width.
  284. *
  285. * @param string $arrow
  286. *
  287. * @return string
  288. */
  289. public function getLineStyleArrowWidth($arrow)
  290. {
  291. return $this->getLineStyleArrowSize($this->lineStyleProperties['arrow'][$arrow]['size'], 'w');
  292. }
  293. /**
  294. * Get Line Style Arrow Excel Length.
  295. *
  296. * @param string $arrow
  297. *
  298. * @return string
  299. */
  300. public function getLineStyleArrowLength($arrow)
  301. {
  302. return $this->getLineStyleArrowSize($this->lineStyleProperties['arrow'][$arrow]['size'], 'len');
  303. }
  304. /**
  305. * Set Shadow Properties.
  306. *
  307. * @param int $sh_presets
  308. * @param string $sh_color_value
  309. * @param string $sh_color_type
  310. * @param string $sh_color_alpha
  311. * @param float $sh_blur
  312. * @param int $sh_angle
  313. * @param float $sh_distance
  314. */
  315. 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)
  316. {
  317. $this->setShadowPresetsProperties((int) $sh_presets)
  318. ->setShadowColor(
  319. $sh_color_value === null ? $this->shadowProperties['color']['value'] : $sh_color_value,
  320. $sh_color_alpha === null ? (int) $this->shadowProperties['color']['alpha'] : $sh_color_alpha,
  321. $sh_color_type === null ? $this->shadowProperties['color']['type'] : $sh_color_type
  322. )
  323. ->setShadowBlur($sh_blur)
  324. ->setShadowAngle($sh_angle)
  325. ->setShadowDistance($sh_distance);
  326. }
  327. /**
  328. * Set Shadow Color.
  329. *
  330. * @param int $shadow_presets
  331. *
  332. * @return Axis
  333. */
  334. private function setShadowPresetsProperties($shadow_presets)
  335. {
  336. $this->shadowProperties['presets'] = $shadow_presets;
  337. $this->setShadowProperiesMapValues($this->getShadowPresetsMap($shadow_presets));
  338. return $this;
  339. }
  340. /**
  341. * Set Shadow Properties from Maped Values.
  342. *
  343. * @param array $properties_map
  344. * @param * $reference
  345. *
  346. * @return Axis
  347. */
  348. private function setShadowProperiesMapValues(array $properties_map, &$reference = null)
  349. {
  350. $base_reference = $reference;
  351. foreach ($properties_map as $property_key => $property_val) {
  352. if (is_array($property_val)) {
  353. if ($reference === null) {
  354. $reference = &$this->shadowProperties[$property_key];
  355. } else {
  356. $reference = &$reference[$property_key];
  357. }
  358. $this->setShadowProperiesMapValues($property_val, $reference);
  359. } else {
  360. if ($base_reference === null) {
  361. $this->shadowProperties[$property_key] = $property_val;
  362. } else {
  363. $reference[$property_key] = $property_val;
  364. }
  365. }
  366. }
  367. return $this;
  368. }
  369. /**
  370. * Set Shadow Color.
  371. *
  372. * @param string $color
  373. * @param int $alpha
  374. * @param string $type
  375. *
  376. * @return Axis
  377. */
  378. private function setShadowColor($color, $alpha, $type)
  379. {
  380. $this->shadowProperties['color'] = $this->setColorProperties($color, $alpha, $type);
  381. return $this;
  382. }
  383. /**
  384. * Set Shadow Blur.
  385. *
  386. * @param float $blur
  387. *
  388. * @return Axis
  389. */
  390. private function setShadowBlur($blur)
  391. {
  392. if ($blur !== null) {
  393. $this->shadowProperties['blur'] = (string) $this->getExcelPointsWidth($blur);
  394. }
  395. return $this;
  396. }
  397. /**
  398. * Set Shadow Angle.
  399. *
  400. * @param int $angle
  401. *
  402. * @return Axis
  403. */
  404. private function setShadowAngle($angle)
  405. {
  406. if ($angle !== null) {
  407. $this->shadowProperties['direction'] = (string) $this->getExcelPointsAngle($angle);
  408. }
  409. return $this;
  410. }
  411. /**
  412. * Set Shadow Distance.
  413. *
  414. * @param float $distance
  415. *
  416. * @return Axis
  417. */
  418. private function setShadowDistance($distance)
  419. {
  420. if ($distance !== null) {
  421. $this->shadowProperties['distance'] = (string) $this->getExcelPointsWidth($distance);
  422. }
  423. return $this;
  424. }
  425. /**
  426. * Get Shadow Property.
  427. *
  428. * @param string|string[] $elements
  429. *
  430. * @return null|array|int|string
  431. */
  432. public function getShadowProperty($elements)
  433. {
  434. return $this->getArrayElementsValue($this->shadowProperties, $elements);
  435. }
  436. /**
  437. * Set Glow Properties.
  438. *
  439. * @param float $size
  440. * @param string $color_value
  441. * @param int $color_alpha
  442. * @param string $color_type
  443. */
  444. public function setGlowProperties($size, $color_value = null, $color_alpha = null, $color_type = null)
  445. {
  446. $this->setGlowSize($size)
  447. ->setGlowColor(
  448. $color_value === null ? $this->glowProperties['color']['value'] : $color_value,
  449. $color_alpha === null ? (int) $this->glowProperties['color']['alpha'] : $color_alpha,
  450. $color_type === null ? $this->glowProperties['color']['type'] : $color_type
  451. );
  452. }
  453. /**
  454. * Get Glow Property.
  455. *
  456. * @param array|string $property
  457. *
  458. * @return string
  459. */
  460. public function getGlowProperty($property)
  461. {
  462. return $this->getArrayElementsValue($this->glowProperties, $property);
  463. }
  464. /**
  465. * Set Glow Color.
  466. *
  467. * @param float $size
  468. *
  469. * @return Axis
  470. */
  471. private function setGlowSize($size)
  472. {
  473. if ($size !== null) {
  474. $this->glowProperties['size'] = $this->getExcelPointsWidth($size);
  475. }
  476. return $this;
  477. }
  478. /**
  479. * Set Glow Color.
  480. *
  481. * @param string $color
  482. * @param int $alpha
  483. * @param string $type
  484. *
  485. * @return Axis
  486. */
  487. private function setGlowColor($color, $alpha, $type)
  488. {
  489. $this->glowProperties['color'] = $this->setColorProperties($color, $alpha, $type);
  490. return $this;
  491. }
  492. /**
  493. * Set Soft Edges Size.
  494. *
  495. * @param float $size
  496. */
  497. public function setSoftEdges($size)
  498. {
  499. if ($size !== null) {
  500. $softEdges['size'] = (string) $this->getExcelPointsWidth($size);
  501. }
  502. }
  503. /**
  504. * Get Soft Edges Size.
  505. *
  506. * @return string
  507. */
  508. public function getSoftEdgesSize()
  509. {
  510. return $this->softEdges['size'];
  511. }
  512. }