Chart.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Chart;
  3. use PhpOffice\PhpSpreadsheet\Settings;
  4. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  5. class Chart
  6. {
  7. /**
  8. * Chart Name.
  9. *
  10. * @var string
  11. */
  12. private $name = '';
  13. /**
  14. * Worksheet.
  15. *
  16. * @var Worksheet
  17. */
  18. private $worksheet;
  19. /**
  20. * Chart Title.
  21. *
  22. * @var Title
  23. */
  24. private $title;
  25. /**
  26. * Chart Legend.
  27. *
  28. * @var Legend
  29. */
  30. private $legend;
  31. /**
  32. * X-Axis Label.
  33. *
  34. * @var Title
  35. */
  36. private $xAxisLabel;
  37. /**
  38. * Y-Axis Label.
  39. *
  40. * @var Title
  41. */
  42. private $yAxisLabel;
  43. /**
  44. * Chart Plot Area.
  45. *
  46. * @var PlotArea
  47. */
  48. private $plotArea;
  49. /**
  50. * Plot Visible Only.
  51. *
  52. * @var bool
  53. */
  54. private $plotVisibleOnly = true;
  55. /**
  56. * Display Blanks as.
  57. *
  58. * @var string
  59. */
  60. private $displayBlanksAs = '0';
  61. /**
  62. * Chart Asix Y as.
  63. *
  64. * @var Axis
  65. */
  66. private $yAxis;
  67. /**
  68. * Chart Asix X as.
  69. *
  70. * @var Axis
  71. */
  72. private $xAxis;
  73. /**
  74. * Chart Major Gridlines as.
  75. *
  76. * @var GridLines
  77. */
  78. private $majorGridlines;
  79. /**
  80. * Chart Minor Gridlines as.
  81. *
  82. * @var GridLines
  83. */
  84. private $minorGridlines;
  85. /**
  86. * Top-Left Cell Position.
  87. *
  88. * @var string
  89. */
  90. private $topLeftCellRef = 'A1';
  91. /**
  92. * Top-Left X-Offset.
  93. *
  94. * @var int
  95. */
  96. private $topLeftXOffset = 0;
  97. /**
  98. * Top-Left Y-Offset.
  99. *
  100. * @var int
  101. */
  102. private $topLeftYOffset = 0;
  103. /**
  104. * Bottom-Right Cell Position.
  105. *
  106. * @var string
  107. */
  108. private $bottomRightCellRef = 'A1';
  109. /**
  110. * Bottom-Right X-Offset.
  111. *
  112. * @var int
  113. */
  114. private $bottomRightXOffset = 10;
  115. /**
  116. * Bottom-Right Y-Offset.
  117. *
  118. * @var int
  119. */
  120. private $bottomRightYOffset = 10;
  121. /**
  122. * Create a new Chart.
  123. *
  124. * @param mixed $name
  125. * @param null|Title $title
  126. * @param null|Legend $legend
  127. * @param null|PlotArea $plotArea
  128. * @param mixed $plotVisibleOnly
  129. * @param mixed $displayBlanksAs
  130. * @param null|Title $xAxisLabel
  131. * @param null|Title $yAxisLabel
  132. * @param null|Axis $xAxis
  133. * @param null|Axis $yAxis
  134. * @param null|GridLines $majorGridlines
  135. * @param null|GridLines $minorGridlines
  136. */
  137. public function __construct($name, Title $title = null, Legend $legend = null, PlotArea $plotArea = null, $plotVisibleOnly = true, $displayBlanksAs = '0', Title $xAxisLabel = null, Title $yAxisLabel = null, Axis $xAxis = null, Axis $yAxis = null, GridLines $majorGridlines = null, GridLines $minorGridlines = null)
  138. {
  139. $this->name = $name;
  140. $this->title = $title;
  141. $this->legend = $legend;
  142. $this->xAxisLabel = $xAxisLabel;
  143. $this->yAxisLabel = $yAxisLabel;
  144. $this->plotArea = $plotArea;
  145. $this->plotVisibleOnly = $plotVisibleOnly;
  146. $this->displayBlanksAs = $displayBlanksAs;
  147. $this->xAxis = $xAxis;
  148. $this->yAxis = $yAxis;
  149. $this->majorGridlines = $majorGridlines;
  150. $this->minorGridlines = $minorGridlines;
  151. }
  152. /**
  153. * Get Name.
  154. *
  155. * @return string
  156. */
  157. public function getName()
  158. {
  159. return $this->name;
  160. }
  161. /**
  162. * Get Worksheet.
  163. *
  164. * @return Worksheet
  165. */
  166. public function getWorksheet()
  167. {
  168. return $this->worksheet;
  169. }
  170. /**
  171. * Set Worksheet.
  172. *
  173. * @param Worksheet $pValue
  174. *
  175. * @return Chart
  176. */
  177. public function setWorksheet(Worksheet $pValue = null)
  178. {
  179. $this->worksheet = $pValue;
  180. return $this;
  181. }
  182. /**
  183. * Get Title.
  184. *
  185. * @return Title
  186. */
  187. public function getTitle()
  188. {
  189. return $this->title;
  190. }
  191. /**
  192. * Set Title.
  193. *
  194. * @param Title $title
  195. *
  196. * @return Chart
  197. */
  198. public function setTitle(Title $title)
  199. {
  200. $this->title = $title;
  201. return $this;
  202. }
  203. /**
  204. * Get Legend.
  205. *
  206. * @return Legend
  207. */
  208. public function getLegend()
  209. {
  210. return $this->legend;
  211. }
  212. /**
  213. * Set Legend.
  214. *
  215. * @param Legend $legend
  216. *
  217. * @return Chart
  218. */
  219. public function setLegend(Legend $legend)
  220. {
  221. $this->legend = $legend;
  222. return $this;
  223. }
  224. /**
  225. * Get X-Axis Label.
  226. *
  227. * @return Title
  228. */
  229. public function getXAxisLabel()
  230. {
  231. return $this->xAxisLabel;
  232. }
  233. /**
  234. * Set X-Axis Label.
  235. *
  236. * @param Title $label
  237. *
  238. * @return Chart
  239. */
  240. public function setXAxisLabel(Title $label)
  241. {
  242. $this->xAxisLabel = $label;
  243. return $this;
  244. }
  245. /**
  246. * Get Y-Axis Label.
  247. *
  248. * @return Title
  249. */
  250. public function getYAxisLabel()
  251. {
  252. return $this->yAxisLabel;
  253. }
  254. /**
  255. * Set Y-Axis Label.
  256. *
  257. * @param Title $label
  258. *
  259. * @return Chart
  260. */
  261. public function setYAxisLabel(Title $label)
  262. {
  263. $this->yAxisLabel = $label;
  264. return $this;
  265. }
  266. /**
  267. * Get Plot Area.
  268. *
  269. * @return PlotArea
  270. */
  271. public function getPlotArea()
  272. {
  273. return $this->plotArea;
  274. }
  275. /**
  276. * Get Plot Visible Only.
  277. *
  278. * @return bool
  279. */
  280. public function getPlotVisibleOnly()
  281. {
  282. return $this->plotVisibleOnly;
  283. }
  284. /**
  285. * Set Plot Visible Only.
  286. *
  287. * @param bool $plotVisibleOnly
  288. *
  289. * @return Chart
  290. */
  291. public function setPlotVisibleOnly($plotVisibleOnly)
  292. {
  293. $this->plotVisibleOnly = $plotVisibleOnly;
  294. return $this;
  295. }
  296. /**
  297. * Get Display Blanks as.
  298. *
  299. * @return string
  300. */
  301. public function getDisplayBlanksAs()
  302. {
  303. return $this->displayBlanksAs;
  304. }
  305. /**
  306. * Set Display Blanks as.
  307. *
  308. * @param string $displayBlanksAs
  309. *
  310. * @return Chart
  311. */
  312. public function setDisplayBlanksAs($displayBlanksAs)
  313. {
  314. $this->displayBlanksAs = $displayBlanksAs;
  315. return $this;
  316. }
  317. /**
  318. * Get yAxis.
  319. *
  320. * @return Axis
  321. */
  322. public function getChartAxisY()
  323. {
  324. if ($this->yAxis !== null) {
  325. return $this->yAxis;
  326. }
  327. return new Axis();
  328. }
  329. /**
  330. * Get xAxis.
  331. *
  332. * @return Axis
  333. */
  334. public function getChartAxisX()
  335. {
  336. if ($this->xAxis !== null) {
  337. return $this->xAxis;
  338. }
  339. return new Axis();
  340. }
  341. /**
  342. * Get Major Gridlines.
  343. *
  344. * @return GridLines
  345. */
  346. public function getMajorGridlines()
  347. {
  348. if ($this->majorGridlines !== null) {
  349. return $this->majorGridlines;
  350. }
  351. return new GridLines();
  352. }
  353. /**
  354. * Get Minor Gridlines.
  355. *
  356. * @return GridLines
  357. */
  358. public function getMinorGridlines()
  359. {
  360. if ($this->minorGridlines !== null) {
  361. return $this->minorGridlines;
  362. }
  363. return new GridLines();
  364. }
  365. /**
  366. * Set the Top Left position for the chart.
  367. *
  368. * @param string $cell
  369. * @param int $xOffset
  370. * @param int $yOffset
  371. *
  372. * @return Chart
  373. */
  374. public function setTopLeftPosition($cell, $xOffset = null, $yOffset = null)
  375. {
  376. $this->topLeftCellRef = $cell;
  377. if ($xOffset !== null) {
  378. $this->setTopLeftXOffset($xOffset);
  379. }
  380. if ($yOffset !== null) {
  381. $this->setTopLeftYOffset($yOffset);
  382. }
  383. return $this;
  384. }
  385. /**
  386. * Get the top left position of the chart.
  387. *
  388. * @return array an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell
  389. */
  390. public function getTopLeftPosition()
  391. {
  392. return [
  393. 'cell' => $this->topLeftCellRef,
  394. 'xOffset' => $this->topLeftXOffset,
  395. 'yOffset' => $this->topLeftYOffset,
  396. ];
  397. }
  398. /**
  399. * Get the cell address where the top left of the chart is fixed.
  400. *
  401. * @return string
  402. */
  403. public function getTopLeftCell()
  404. {
  405. return $this->topLeftCellRef;
  406. }
  407. /**
  408. * Set the Top Left cell position for the chart.
  409. *
  410. * @param string $cell
  411. *
  412. * @return Chart
  413. */
  414. public function setTopLeftCell($cell)
  415. {
  416. $this->topLeftCellRef = $cell;
  417. return $this;
  418. }
  419. /**
  420. * Set the offset position within the Top Left cell for the chart.
  421. *
  422. * @param int $xOffset
  423. * @param int $yOffset
  424. *
  425. * @return Chart
  426. */
  427. public function setTopLeftOffset($xOffset, $yOffset)
  428. {
  429. if ($xOffset !== null) {
  430. $this->setTopLeftXOffset($xOffset);
  431. }
  432. if ($yOffset !== null) {
  433. $this->setTopLeftYOffset($yOffset);
  434. }
  435. return $this;
  436. }
  437. /**
  438. * Get the offset position within the Top Left cell for the chart.
  439. *
  440. * @return int[]
  441. */
  442. public function getTopLeftOffset()
  443. {
  444. return [
  445. 'X' => $this->topLeftXOffset,
  446. 'Y' => $this->topLeftYOffset,
  447. ];
  448. }
  449. public function setTopLeftXOffset($xOffset)
  450. {
  451. $this->topLeftXOffset = $xOffset;
  452. return $this;
  453. }
  454. public function getTopLeftXOffset()
  455. {
  456. return $this->topLeftXOffset;
  457. }
  458. public function setTopLeftYOffset($yOffset)
  459. {
  460. $this->topLeftYOffset = $yOffset;
  461. return $this;
  462. }
  463. public function getTopLeftYOffset()
  464. {
  465. return $this->topLeftYOffset;
  466. }
  467. /**
  468. * Set the Bottom Right position of the chart.
  469. *
  470. * @param string $cell
  471. * @param int $xOffset
  472. * @param int $yOffset
  473. *
  474. * @return Chart
  475. */
  476. public function setBottomRightPosition($cell, $xOffset = null, $yOffset = null)
  477. {
  478. $this->bottomRightCellRef = $cell;
  479. if ($xOffset !== null) {
  480. $this->setBottomRightXOffset($xOffset);
  481. }
  482. if ($yOffset !== null) {
  483. $this->setBottomRightYOffset($yOffset);
  484. }
  485. return $this;
  486. }
  487. /**
  488. * Get the bottom right position of the chart.
  489. *
  490. * @return array an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell
  491. */
  492. public function getBottomRightPosition()
  493. {
  494. return [
  495. 'cell' => $this->bottomRightCellRef,
  496. 'xOffset' => $this->bottomRightXOffset,
  497. 'yOffset' => $this->bottomRightYOffset,
  498. ];
  499. }
  500. public function setBottomRightCell($cell)
  501. {
  502. $this->bottomRightCellRef = $cell;
  503. return $this;
  504. }
  505. /**
  506. * Get the cell address where the bottom right of the chart is fixed.
  507. *
  508. * @return string
  509. */
  510. public function getBottomRightCell()
  511. {
  512. return $this->bottomRightCellRef;
  513. }
  514. /**
  515. * Set the offset position within the Bottom Right cell for the chart.
  516. *
  517. * @param int $xOffset
  518. * @param int $yOffset
  519. *
  520. * @return Chart
  521. */
  522. public function setBottomRightOffset($xOffset, $yOffset)
  523. {
  524. if ($xOffset !== null) {
  525. $this->setBottomRightXOffset($xOffset);
  526. }
  527. if ($yOffset !== null) {
  528. $this->setBottomRightYOffset($yOffset);
  529. }
  530. return $this;
  531. }
  532. /**
  533. * Get the offset position within the Bottom Right cell for the chart.
  534. *
  535. * @return int[]
  536. */
  537. public function getBottomRightOffset()
  538. {
  539. return [
  540. 'X' => $this->bottomRightXOffset,
  541. 'Y' => $this->bottomRightYOffset,
  542. ];
  543. }
  544. public function setBottomRightXOffset($xOffset)
  545. {
  546. $this->bottomRightXOffset = $xOffset;
  547. return $this;
  548. }
  549. public function getBottomRightXOffset()
  550. {
  551. return $this->bottomRightXOffset;
  552. }
  553. public function setBottomRightYOffset($yOffset)
  554. {
  555. $this->bottomRightYOffset = $yOffset;
  556. return $this;
  557. }
  558. public function getBottomRightYOffset()
  559. {
  560. return $this->bottomRightYOffset;
  561. }
  562. public function refresh()
  563. {
  564. if ($this->worksheet !== null) {
  565. $this->plotArea->refresh($this->worksheet);
  566. }
  567. }
  568. /**
  569. * Render the chart to given file (or stream).
  570. *
  571. * @param string $outputDestination Name of the file render to
  572. *
  573. * @return bool true on success
  574. */
  575. public function render($outputDestination = null)
  576. {
  577. if ($outputDestination == 'php://output') {
  578. $outputDestination = null;
  579. }
  580. $libraryName = Settings::getChartRenderer();
  581. if ($libraryName === null) {
  582. return false;
  583. }
  584. // Ensure that data series values are up-to-date before we render
  585. $this->refresh();
  586. $renderer = new $libraryName($this);
  587. return $renderer->render($outputDestination);
  588. }
  589. }