Chart.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Style;
  18. /**
  19. * Chart style
  20. *
  21. * @since 0.12.0
  22. */
  23. class Chart extends AbstractStyle
  24. {
  25. /**
  26. * Width (in EMU)
  27. *
  28. * @var int
  29. */
  30. private $width = 1000000;
  31. /**
  32. * Height (in EMU)
  33. *
  34. * @var int
  35. */
  36. private $height = 1000000;
  37. /**
  38. * Is 3D; applies to pie, bar, line, area
  39. *
  40. * @var bool
  41. */
  42. private $is3d = false;
  43. /**
  44. * A list of colors to use in the chart
  45. *
  46. * @var array
  47. */
  48. private $colors = array();
  49. /**
  50. * Chart title
  51. *
  52. * @var string
  53. */
  54. private $title = null;
  55. /**
  56. * Chart legend visibility
  57. *
  58. * @var bool
  59. */
  60. private $showLegend = false;
  61. /**
  62. * Chart legend Position.
  63. * Possible values are 'r', 't', 'b', 'l', 'tr'
  64. *
  65. * @var string
  66. */
  67. private $legendPosition = 'r';
  68. /**
  69. * A list of display options for data labels
  70. *
  71. * @var array
  72. */
  73. private $dataLabelOptions = array(
  74. 'showVal' => true, // value
  75. 'showCatName' => true, // category name
  76. 'showLegendKey' => false, //show the cart legend
  77. 'showSerName' => false, // series name
  78. 'showPercent' => false,
  79. 'showLeaderLines' => false,
  80. 'showBubbleSize' => false,
  81. );
  82. /**
  83. * A string that tells the writer where to write chart labels or to skip
  84. * "nextTo" - sets labels next to the axis (bar graphs on the left) (default)
  85. * "low" - labels on the left side of the graph
  86. * "high" - labels on the right side of the graph
  87. *
  88. * @var string
  89. */
  90. private $categoryLabelPosition = 'nextTo';
  91. /**
  92. * A string that tells the writer where to write chart labels or to skip
  93. * "nextTo" - sets labels next to the axis (bar graphs on the bottom) (default)
  94. * "low" - labels are below the graph
  95. * "high" - labels above the graph
  96. *
  97. * @var string
  98. */
  99. private $valueLabelPosition = 'nextTo';
  100. /**
  101. * @var string
  102. */
  103. private $categoryAxisTitle;
  104. /**
  105. * @var string
  106. */
  107. private $valueAxisTitle;
  108. /**
  109. * The position for major tick marks
  110. * Possible values are 'in', 'out', 'cross', 'none'
  111. *
  112. * @var string
  113. */
  114. private $majorTickMarkPos = 'none';
  115. /**
  116. * Show labels for axis
  117. *
  118. * @var bool
  119. */
  120. private $showAxisLabels = false;
  121. /**
  122. * Show Gridlines for Y-Axis
  123. *
  124. * @var bool
  125. */
  126. private $gridY = false;
  127. /**
  128. * Show Gridlines for X-Axis
  129. *
  130. * @var bool
  131. */
  132. private $gridX = false;
  133. /**
  134. * Create a new instance
  135. *
  136. * @param array $style
  137. */
  138. public function __construct($style = array())
  139. {
  140. $this->setStyleByArray($style);
  141. }
  142. /**
  143. * Get width
  144. *
  145. * @return int
  146. */
  147. public function getWidth()
  148. {
  149. return $this->width;
  150. }
  151. /**
  152. * Set width
  153. *
  154. * @param int $value
  155. * @return self
  156. */
  157. public function setWidth($value = null)
  158. {
  159. $this->width = $this->setIntVal($value, $this->width);
  160. return $this;
  161. }
  162. /**
  163. * Get height
  164. *
  165. * @return int
  166. */
  167. public function getHeight()
  168. {
  169. return $this->height;
  170. }
  171. /**
  172. * Set height
  173. *
  174. * @param int $value
  175. * @return self
  176. */
  177. public function setHeight($value = null)
  178. {
  179. $this->height = $this->setIntVal($value, $this->height);
  180. return $this;
  181. }
  182. /**
  183. * Is 3D
  184. *
  185. * @return bool
  186. */
  187. public function is3d()
  188. {
  189. return $this->is3d;
  190. }
  191. /**
  192. * Set 3D
  193. *
  194. * @param bool $value
  195. * @return self
  196. */
  197. public function set3d($value = true)
  198. {
  199. $this->is3d = $this->setBoolVal($value, $this->is3d);
  200. return $this;
  201. }
  202. /**
  203. * Get the list of colors to use in a chart.
  204. *
  205. * @return array
  206. */
  207. public function getColors()
  208. {
  209. return $this->colors;
  210. }
  211. /**
  212. * Set the colors to use in a chart.
  213. *
  214. * @param array $value a list of colors to use in the chart
  215. * @return self
  216. */
  217. public function setColors($value = array())
  218. {
  219. $this->colors = $value;
  220. return $this;
  221. }
  222. /**
  223. * Get the chart title
  224. *
  225. * @return string
  226. */
  227. public function getTitle()
  228. {
  229. return $this->title;
  230. }
  231. /**
  232. * Set the chart title
  233. *
  234. * @param string $value
  235. * @return self
  236. */
  237. public function setTitle($value = null)
  238. {
  239. $this->title = $value;
  240. return $this;
  241. }
  242. /**
  243. * Get chart legend visibility
  244. *
  245. * @return bool
  246. */
  247. public function isShowLegend()
  248. {
  249. return $this->showLegend;
  250. }
  251. /**
  252. * Set chart legend visibility
  253. *
  254. * @param bool $value
  255. * @return self
  256. */
  257. public function setShowLegend($value = false)
  258. {
  259. $this->showLegend = $value;
  260. return $this;
  261. }
  262. /**
  263. * Get chart legend position
  264. *
  265. * @return string
  266. */
  267. public function getLegendPosition()
  268. {
  269. return $this->legendPosition;
  270. }
  271. /**
  272. * Set chart legend position. choices:
  273. * "r" - right of chart
  274. * "b" - bottom of chart
  275. * "t" - top of chart
  276. * "l" - left of chart
  277. * "tr" - top right of chart
  278. *
  279. * default: right
  280. *
  281. * @param string $legendPosition
  282. * @return self
  283. */
  284. public function setLegendPosition($legendPosition = 'r')
  285. {
  286. $enum = array('r', 'b', 't', 'l', 'tr');
  287. $this->legendPosition = $this->setEnumVal($legendPosition, $enum, $this->legendPosition);
  288. return $this;
  289. }
  290. /*
  291. * Show labels for axis
  292. *
  293. * @return bool
  294. */
  295. public function showAxisLabels()
  296. {
  297. return $this->showAxisLabels;
  298. }
  299. /**
  300. * Set show Gridlines for Y-Axis
  301. *
  302. * @param bool $value
  303. * @return self
  304. */
  305. public function setShowAxisLabels($value = true)
  306. {
  307. $this->showAxisLabels = $this->setBoolVal($value, $this->showAxisLabels);
  308. return $this;
  309. }
  310. /**
  311. * get the list of options for data labels
  312. *
  313. * @return array
  314. */
  315. public function getDataLabelOptions()
  316. {
  317. return $this->dataLabelOptions;
  318. }
  319. /**
  320. * Set values for data label options.
  321. * This will only change values for options defined in $this->dataLabelOptions, and cannot create new ones.
  322. *
  323. * @param array $values [description]
  324. */
  325. public function setDataLabelOptions($values = array())
  326. {
  327. foreach (array_keys($this->dataLabelOptions) as $option) {
  328. if (isset($values[$option])) {
  329. $this->dataLabelOptions[$option] = $this->setBoolVal(
  330. $values[$option],
  331. $this->dataLabelOptions[$option]
  332. );
  333. }
  334. }
  335. }
  336. /*
  337. * Show Gridlines for Y-Axis
  338. *
  339. * @return bool
  340. */
  341. public function showGridY()
  342. {
  343. return $this->gridY;
  344. }
  345. /**
  346. * Set show Gridlines for Y-Axis
  347. *
  348. * @param bool $value
  349. * @return self
  350. */
  351. public function setShowGridY($value = true)
  352. {
  353. $this->gridY = $this->setBoolVal($value, $this->gridY);
  354. return $this;
  355. }
  356. /**
  357. * Get the categoryLabelPosition setting
  358. *
  359. * @return string
  360. */
  361. public function getCategoryLabelPosition()
  362. {
  363. return $this->categoryLabelPosition;
  364. }
  365. /**
  366. * Set the categoryLabelPosition setting
  367. * "none" - skips writing labels
  368. * "nextTo" - sets labels next to the (bar graphs on the left)
  369. * "low" - labels on the left side of the graph
  370. * "high" - labels on the right side of the graph
  371. *
  372. * @param mixed $labelPosition
  373. * @return self
  374. */
  375. public function setCategoryLabelPosition($labelPosition)
  376. {
  377. $enum = array('nextTo', 'low', 'high');
  378. $this->categoryLabelPosition = $this->setEnumVal($labelPosition, $enum, $this->categoryLabelPosition);
  379. return $this;
  380. }
  381. /**
  382. * Get the valueAxisLabelPosition setting
  383. *
  384. * @return string
  385. */
  386. public function getValueLabelPosition()
  387. {
  388. return $this->valueLabelPosition;
  389. }
  390. /**
  391. * Set the valueLabelPosition setting
  392. * "none" - skips writing labels
  393. * "nextTo" - sets labels next to the value
  394. * "low" - sets labels are below the graph
  395. * "high" - sets labels above the graph
  396. *
  397. * @param string
  398. * @param mixed $labelPosition
  399. */
  400. public function setValueLabelPosition($labelPosition)
  401. {
  402. $enum = array('nextTo', 'low', 'high');
  403. $this->valueLabelPosition = $this->setEnumVal($labelPosition, $enum, $this->valueLabelPosition);
  404. return $this;
  405. }
  406. /**
  407. * Get the categoryAxisTitle
  408. * @return string
  409. */
  410. public function getCategoryAxisTitle()
  411. {
  412. return $this->categoryAxisTitle;
  413. }
  414. /**
  415. * Set the title that appears on the category side of the chart
  416. * @param string $axisTitle
  417. */
  418. public function setCategoryAxisTitle($axisTitle)
  419. {
  420. $this->categoryAxisTitle = $axisTitle;
  421. return $this;
  422. }
  423. /**
  424. * Get the valueAxisTitle
  425. * @return string
  426. */
  427. public function getValueAxisTitle()
  428. {
  429. return $this->valueAxisTitle;
  430. }
  431. /**
  432. * Set the title that appears on the value side of the chart
  433. * @param string $axisTitle
  434. */
  435. public function setValueAxisTitle($axisTitle)
  436. {
  437. $this->valueAxisTitle = $axisTitle;
  438. return $this;
  439. }
  440. public function getMajorTickPosition()
  441. {
  442. return $this->majorTickMarkPos;
  443. }
  444. /**
  445. * Set the position for major tick marks
  446. * @param string $position
  447. */
  448. public function setMajorTickPosition($position)
  449. {
  450. $enum = array('in', 'out', 'cross', 'none');
  451. $this->majorTickMarkPos = $this->setEnumVal($position, $enum, $this->majorTickMarkPos);
  452. }
  453. /**
  454. * Show Gridlines for X-Axis
  455. *
  456. * @return bool
  457. */
  458. public function showGridX()
  459. {
  460. return $this->gridX;
  461. }
  462. /**
  463. * Set show Gridlines for X-Axis
  464. *
  465. * @param bool $value
  466. * @return self
  467. */
  468. public function setShowGridX($value = true)
  469. {
  470. $this->gridX = $this->setBoolVal($value, $this->gridX);
  471. return $this;
  472. }
  473. }