Chart.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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 = DataSeries::EMPTY_AS_GAP;
  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 mixed $plotVisibleOnly
  126. * @param string $displayBlanksAs
  127. */
  128. public function __construct($name, ?Title $title = null, ?Legend $legend = null, ?PlotArea $plotArea = null, $plotVisibleOnly = true, $displayBlanksAs = DataSeries::EMPTY_AS_GAP, ?Title $xAxisLabel = null, ?Title $yAxisLabel = null, ?Axis $xAxis = null, ?Axis $yAxis = null, ?GridLines $majorGridlines = null, ?GridLines $minorGridlines = null)
  129. {
  130. $this->name = $name;
  131. $this->title = $title;
  132. $this->legend = $legend;
  133. $this->xAxisLabel = $xAxisLabel;
  134. $this->yAxisLabel = $yAxisLabel;
  135. $this->plotArea = $plotArea;
  136. $this->plotVisibleOnly = $plotVisibleOnly;
  137. $this->displayBlanksAs = $displayBlanksAs;
  138. $this->xAxis = $xAxis;
  139. $this->yAxis = $yAxis;
  140. $this->majorGridlines = $majorGridlines;
  141. $this->minorGridlines = $minorGridlines;
  142. }
  143. /**
  144. * Get Name.
  145. *
  146. * @return string
  147. */
  148. public function getName()
  149. {
  150. return $this->name;
  151. }
  152. /**
  153. * Get Worksheet.
  154. *
  155. * @return Worksheet
  156. */
  157. public function getWorksheet()
  158. {
  159. return $this->worksheet;
  160. }
  161. /**
  162. * Set Worksheet.
  163. *
  164. * @param Worksheet $pValue
  165. *
  166. * @return $this
  167. */
  168. public function setWorksheet(?Worksheet $pValue = null)
  169. {
  170. $this->worksheet = $pValue;
  171. return $this;
  172. }
  173. /**
  174. * Get Title.
  175. *
  176. * @return Title
  177. */
  178. public function getTitle()
  179. {
  180. return $this->title;
  181. }
  182. /**
  183. * Set Title.
  184. *
  185. * @return $this
  186. */
  187. public function setTitle(Title $title)
  188. {
  189. $this->title = $title;
  190. return $this;
  191. }
  192. /**
  193. * Get Legend.
  194. *
  195. * @return Legend
  196. */
  197. public function getLegend()
  198. {
  199. return $this->legend;
  200. }
  201. /**
  202. * Set Legend.
  203. *
  204. * @return $this
  205. */
  206. public function setLegend(Legend $legend)
  207. {
  208. $this->legend = $legend;
  209. return $this;
  210. }
  211. /**
  212. * Get X-Axis Label.
  213. *
  214. * @return Title
  215. */
  216. public function getXAxisLabel()
  217. {
  218. return $this->xAxisLabel;
  219. }
  220. /**
  221. * Set X-Axis Label.
  222. *
  223. * @return $this
  224. */
  225. public function setXAxisLabel(Title $label)
  226. {
  227. $this->xAxisLabel = $label;
  228. return $this;
  229. }
  230. /**
  231. * Get Y-Axis Label.
  232. *
  233. * @return Title
  234. */
  235. public function getYAxisLabel()
  236. {
  237. return $this->yAxisLabel;
  238. }
  239. /**
  240. * Set Y-Axis Label.
  241. *
  242. * @return $this
  243. */
  244. public function setYAxisLabel(Title $label)
  245. {
  246. $this->yAxisLabel = $label;
  247. return $this;
  248. }
  249. /**
  250. * Get Plot Area.
  251. *
  252. * @return PlotArea
  253. */
  254. public function getPlotArea()
  255. {
  256. return $this->plotArea;
  257. }
  258. /**
  259. * Get Plot Visible Only.
  260. *
  261. * @return bool
  262. */
  263. public function getPlotVisibleOnly()
  264. {
  265. return $this->plotVisibleOnly;
  266. }
  267. /**
  268. * Set Plot Visible Only.
  269. *
  270. * @param bool $plotVisibleOnly
  271. *
  272. * @return $this
  273. */
  274. public function setPlotVisibleOnly($plotVisibleOnly)
  275. {
  276. $this->plotVisibleOnly = $plotVisibleOnly;
  277. return $this;
  278. }
  279. /**
  280. * Get Display Blanks as.
  281. *
  282. * @return string
  283. */
  284. public function getDisplayBlanksAs()
  285. {
  286. return $this->displayBlanksAs;
  287. }
  288. /**
  289. * Set Display Blanks as.
  290. *
  291. * @param string $displayBlanksAs
  292. *
  293. * @return $this
  294. */
  295. public function setDisplayBlanksAs($displayBlanksAs)
  296. {
  297. $this->displayBlanksAs = $displayBlanksAs;
  298. return $this;
  299. }
  300. /**
  301. * Get yAxis.
  302. *
  303. * @return Axis
  304. */
  305. public function getChartAxisY()
  306. {
  307. if ($this->yAxis !== null) {
  308. return $this->yAxis;
  309. }
  310. return new Axis();
  311. }
  312. /**
  313. * Get xAxis.
  314. *
  315. * @return Axis
  316. */
  317. public function getChartAxisX()
  318. {
  319. if ($this->xAxis !== null) {
  320. return $this->xAxis;
  321. }
  322. return new Axis();
  323. }
  324. /**
  325. * Get Major Gridlines.
  326. *
  327. * @return GridLines
  328. */
  329. public function getMajorGridlines()
  330. {
  331. if ($this->majorGridlines !== null) {
  332. return $this->majorGridlines;
  333. }
  334. return new GridLines();
  335. }
  336. /**
  337. * Get Minor Gridlines.
  338. *
  339. * @return GridLines
  340. */
  341. public function getMinorGridlines()
  342. {
  343. if ($this->minorGridlines !== null) {
  344. return $this->minorGridlines;
  345. }
  346. return new GridLines();
  347. }
  348. /**
  349. * Set the Top Left position for the chart.
  350. *
  351. * @param string $cell
  352. * @param int $xOffset
  353. * @param int $yOffset
  354. *
  355. * @return $this
  356. */
  357. public function setTopLeftPosition($cell, $xOffset = null, $yOffset = null)
  358. {
  359. $this->topLeftCellRef = $cell;
  360. if ($xOffset !== null) {
  361. $this->setTopLeftXOffset($xOffset);
  362. }
  363. if ($yOffset !== null) {
  364. $this->setTopLeftYOffset($yOffset);
  365. }
  366. return $this;
  367. }
  368. /**
  369. * Get the top left position of the chart.
  370. *
  371. * @return array{cell: string, xOffset: int, yOffset: int} an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell
  372. */
  373. public function getTopLeftPosition()
  374. {
  375. return [
  376. 'cell' => $this->topLeftCellRef,
  377. 'xOffset' => $this->topLeftXOffset,
  378. 'yOffset' => $this->topLeftYOffset,
  379. ];
  380. }
  381. /**
  382. * Get the cell address where the top left of the chart is fixed.
  383. *
  384. * @return string
  385. */
  386. public function getTopLeftCell()
  387. {
  388. return $this->topLeftCellRef;
  389. }
  390. /**
  391. * Set the Top Left cell position for the chart.
  392. *
  393. * @param string $cell
  394. *
  395. * @return $this
  396. */
  397. public function setTopLeftCell($cell)
  398. {
  399. $this->topLeftCellRef = $cell;
  400. return $this;
  401. }
  402. /**
  403. * Set the offset position within the Top Left cell for the chart.
  404. *
  405. * @param int $xOffset
  406. * @param int $yOffset
  407. *
  408. * @return $this
  409. */
  410. public function setTopLeftOffset($xOffset, $yOffset)
  411. {
  412. if ($xOffset !== null) {
  413. $this->setTopLeftXOffset($xOffset);
  414. }
  415. if ($yOffset !== null) {
  416. $this->setTopLeftYOffset($yOffset);
  417. }
  418. return $this;
  419. }
  420. /**
  421. * Get the offset position within the Top Left cell for the chart.
  422. *
  423. * @return int[]
  424. */
  425. public function getTopLeftOffset()
  426. {
  427. return [
  428. 'X' => $this->topLeftXOffset,
  429. 'Y' => $this->topLeftYOffset,
  430. ];
  431. }
  432. public function setTopLeftXOffset($xOffset)
  433. {
  434. $this->topLeftXOffset = $xOffset;
  435. return $this;
  436. }
  437. public function getTopLeftXOffset()
  438. {
  439. return $this->topLeftXOffset;
  440. }
  441. public function setTopLeftYOffset($yOffset)
  442. {
  443. $this->topLeftYOffset = $yOffset;
  444. return $this;
  445. }
  446. public function getTopLeftYOffset()
  447. {
  448. return $this->topLeftYOffset;
  449. }
  450. /**
  451. * Set the Bottom Right position of the chart.
  452. *
  453. * @param string $cell
  454. * @param int $xOffset
  455. * @param int $yOffset
  456. *
  457. * @return $this
  458. */
  459. public function setBottomRightPosition($cell, $xOffset = null, $yOffset = null)
  460. {
  461. $this->bottomRightCellRef = $cell;
  462. if ($xOffset !== null) {
  463. $this->setBottomRightXOffset($xOffset);
  464. }
  465. if ($yOffset !== null) {
  466. $this->setBottomRightYOffset($yOffset);
  467. }
  468. return $this;
  469. }
  470. /**
  471. * Get the bottom right position of the chart.
  472. *
  473. * @return array an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell
  474. */
  475. public function getBottomRightPosition()
  476. {
  477. return [
  478. 'cell' => $this->bottomRightCellRef,
  479. 'xOffset' => $this->bottomRightXOffset,
  480. 'yOffset' => $this->bottomRightYOffset,
  481. ];
  482. }
  483. public function setBottomRightCell($cell)
  484. {
  485. $this->bottomRightCellRef = $cell;
  486. return $this;
  487. }
  488. /**
  489. * Get the cell address where the bottom right of the chart is fixed.
  490. *
  491. * @return string
  492. */
  493. public function getBottomRightCell()
  494. {
  495. return $this->bottomRightCellRef;
  496. }
  497. /**
  498. * Set the offset position within the Bottom Right cell for the chart.
  499. *
  500. * @param int $xOffset
  501. * @param int $yOffset
  502. *
  503. * @return $this
  504. */
  505. public function setBottomRightOffset($xOffset, $yOffset)
  506. {
  507. if ($xOffset !== null) {
  508. $this->setBottomRightXOffset($xOffset);
  509. }
  510. if ($yOffset !== null) {
  511. $this->setBottomRightYOffset($yOffset);
  512. }
  513. return $this;
  514. }
  515. /**
  516. * Get the offset position within the Bottom Right cell for the chart.
  517. *
  518. * @return int[]
  519. */
  520. public function getBottomRightOffset()
  521. {
  522. return [
  523. 'X' => $this->bottomRightXOffset,
  524. 'Y' => $this->bottomRightYOffset,
  525. ];
  526. }
  527. public function setBottomRightXOffset($xOffset)
  528. {
  529. $this->bottomRightXOffset = $xOffset;
  530. return $this;
  531. }
  532. public function getBottomRightXOffset()
  533. {
  534. return $this->bottomRightXOffset;
  535. }
  536. public function setBottomRightYOffset($yOffset)
  537. {
  538. $this->bottomRightYOffset = $yOffset;
  539. return $this;
  540. }
  541. public function getBottomRightYOffset()
  542. {
  543. return $this->bottomRightYOffset;
  544. }
  545. public function refresh(): void
  546. {
  547. if ($this->worksheet !== null) {
  548. $this->plotArea->refresh($this->worksheet);
  549. }
  550. }
  551. /**
  552. * Render the chart to given file (or stream).
  553. *
  554. * @param string $outputDestination Name of the file render to
  555. *
  556. * @return bool true on success
  557. */
  558. public function render($outputDestination = null)
  559. {
  560. if ($outputDestination == 'php://output') {
  561. $outputDestination = null;
  562. }
  563. $libraryName = Settings::getChartRenderer();
  564. if ($libraryName === null) {
  565. return false;
  566. }
  567. // Ensure that data series values are up-to-date before we render
  568. $this->refresh();
  569. $renderer = new $libraryName($this);
  570. return $renderer->render($outputDestination);
  571. }
  572. }