Chart.php 15 KB

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