Chart.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. /**
  28. * PHPExcel_Chart
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Chart
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Chart
  35. {
  36. /**
  37. * Chart Name
  38. *
  39. * @var string
  40. */
  41. private $_name = '';
  42. /**
  43. * Worksheet
  44. *
  45. * @var PHPExcel_Worksheet
  46. */
  47. private $_worksheet = null;
  48. /**
  49. * Chart Title
  50. *
  51. * @var PHPExcel_Chart_Title
  52. */
  53. private $_title = null;
  54. /**
  55. * Chart Legend
  56. *
  57. * @var PHPExcel_Chart_Legend
  58. */
  59. private $_legend = null;
  60. /**
  61. * X-Axis Label
  62. *
  63. * @var PHPExcel_Chart_Title
  64. */
  65. private $_xAxisLabel = null;
  66. /**
  67. * Y-Axis Label
  68. *
  69. * @var PHPExcel_Chart_Title
  70. */
  71. private $_yAxisLabel = null;
  72. /**
  73. * Chart Plot Area
  74. *
  75. * @var PHPExcel_Chart_PlotArea
  76. */
  77. private $_plotArea = null;
  78. /**
  79. * Plot Visible Only
  80. *
  81. * @var boolean
  82. */
  83. private $_plotVisibleOnly = true;
  84. /**
  85. * Display Blanks as
  86. *
  87. * @var string
  88. */
  89. private $_displayBlanksAs = '0';
  90. /**
  91. * Top-Left Cell Position
  92. *
  93. * @var string
  94. */
  95. private $_topLeftCellRef = 'A1';
  96. /**
  97. * Top-Left X-Offset
  98. *
  99. * @var integer
  100. */
  101. private $_topLeftXOffset = 0;
  102. /**
  103. * Top-Left Y-Offset
  104. *
  105. * @var integer
  106. */
  107. private $_topLeftYOffset = 0;
  108. /**
  109. * Bottom-Right Cell Position
  110. *
  111. * @var string
  112. */
  113. private $_bottomRightCellRef = 'A1';
  114. /**
  115. * Bottom-Right X-Offset
  116. *
  117. * @var integer
  118. */
  119. private $_bottomRightXOffset = 10;
  120. /**
  121. * Bottom-Right Y-Offset
  122. *
  123. * @var integer
  124. */
  125. private $_bottomRightYOffset = 10;
  126. /**
  127. * Create a new PHPExcel_Chart
  128. */
  129. 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)
  130. {
  131. $this->_name = $name;
  132. $this->_title = $title;
  133. $this->_legend = $legend;
  134. $this->_xAxisLabel = $xAxisLabel;
  135. $this->_yAxisLabel = $yAxisLabel;
  136. $this->_plotArea = $plotArea;
  137. $this->_plotVisibleOnly = $plotVisibleOnly;
  138. $this->_displayBlanksAs = $displayBlanksAs;
  139. }
  140. /**
  141. * Get Name
  142. *
  143. * @return string
  144. */
  145. public function getName() {
  146. return $this->_name;
  147. }
  148. /**
  149. * Get Worksheet
  150. *
  151. * @return PHPExcel_Worksheet
  152. */
  153. public function getWorksheet() {
  154. return $this->_worksheet;
  155. }
  156. /**
  157. * Set Worksheet
  158. *
  159. * @param PHPExcel_Worksheet $pValue
  160. * @throws PHPExcel_Chart_Exception
  161. * @return PHPExcel_Chart
  162. */
  163. public function setWorksheet(PHPExcel_Worksheet $pValue = null) {
  164. $this->_worksheet = $pValue;
  165. return $this;
  166. }
  167. /**
  168. * Get Title
  169. *
  170. * @return PHPExcel_Chart_Title
  171. */
  172. public function getTitle() {
  173. return $this->_title;
  174. }
  175. /**
  176. * Set Title
  177. *
  178. * @param PHPExcel_Chart_Title $title
  179. * @return PHPExcel_Chart
  180. */
  181. public function setTitle(PHPExcel_Chart_Title $title) {
  182. $this->_title = $title;
  183. return $this;
  184. }
  185. /**
  186. * Get Legend
  187. *
  188. * @return PHPExcel_Chart_Legend
  189. */
  190. public function getLegend() {
  191. return $this->_legend;
  192. }
  193. /**
  194. * Set Legend
  195. *
  196. * @param PHPExcel_Chart_Legend $legend
  197. * @return PHPExcel_Chart
  198. */
  199. public function setLegend(PHPExcel_Chart_Legend $legend) {
  200. $this->_legend = $legend;
  201. return $this;
  202. }
  203. /**
  204. * Get X-Axis Label
  205. *
  206. * @return PHPExcel_Chart_Title
  207. */
  208. public function getXAxisLabel() {
  209. return $this->_xAxisLabel;
  210. }
  211. /**
  212. * Set X-Axis Label
  213. *
  214. * @param PHPExcel_Chart_Title $label
  215. * @return PHPExcel_Chart
  216. */
  217. public function setXAxisLabel(PHPExcel_Chart_Title $label) {
  218. $this->_xAxisLabel = $label;
  219. return $this;
  220. }
  221. /**
  222. * Get Y-Axis Label
  223. *
  224. * @return PHPExcel_Chart_Title
  225. */
  226. public function getYAxisLabel() {
  227. return $this->_yAxisLabel;
  228. }
  229. /**
  230. * Set Y-Axis Label
  231. *
  232. * @param PHPExcel_Chart_Title $label
  233. * @return PHPExcel_Chart
  234. */
  235. public function setYAxisLabel(PHPExcel_Chart_Title $label) {
  236. $this->_yAxisLabel = $label;
  237. return $this;
  238. }
  239. /**
  240. * Get Plot Area
  241. *
  242. * @return PHPExcel_Chart_PlotArea
  243. */
  244. public function getPlotArea() {
  245. return $this->_plotArea;
  246. }
  247. /**
  248. * Get Plot Visible Only
  249. *
  250. * @return boolean
  251. */
  252. public function getPlotVisibleOnly() {
  253. return $this->_plotVisibleOnly;
  254. }
  255. /**
  256. * Set Plot Visible Only
  257. *
  258. * @param boolean $plotVisibleOnly
  259. * @return PHPExcel_Chart
  260. */
  261. public function setPlotVisibleOnly($plotVisibleOnly = true) {
  262. $this->_plotVisibleOnly = $plotVisibleOnly;
  263. return $this;
  264. }
  265. /**
  266. * Get Display Blanks as
  267. *
  268. * @return string
  269. */
  270. public function getDisplayBlanksAs() {
  271. return $this->_displayBlanksAs;
  272. }
  273. /**
  274. * Set Display Blanks as
  275. *
  276. * @param string $displayBlanksAs
  277. * @return PHPExcel_Chart
  278. */
  279. public function setDisplayBlanksAs($displayBlanksAs = '0') {
  280. $this->_displayBlanksAs = $displayBlanksAs;
  281. }
  282. /**
  283. * Set the Top Left position for the chart
  284. *
  285. * @param string $cell
  286. * @param integer $xOffset
  287. * @param integer $yOffset
  288. * @return PHPExcel_Chart
  289. */
  290. public function setTopLeftPosition($cell, $xOffset=null, $yOffset=null) {
  291. $this->_topLeftCellRef = $cell;
  292. if (!is_null($xOffset))
  293. $this->setTopLeftXOffset($xOffset);
  294. if (!is_null($yOffset))
  295. $this->setTopLeftYOffset($yOffset);
  296. return $this;
  297. }
  298. /**
  299. * Get the top left position of the chart
  300. *
  301. * @return array an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell
  302. */
  303. public function getTopLeftPosition() {
  304. return array( 'cell' => $this->_topLeftCellRef,
  305. 'xOffset' => $this->_topLeftXOffset,
  306. 'yOffset' => $this->_topLeftYOffset
  307. );
  308. }
  309. /**
  310. * Get the cell address where the top left of the chart is fixed
  311. *
  312. * @return string
  313. */
  314. public function getTopLeftCell() {
  315. return $this->_topLeftCellRef;
  316. }
  317. /**
  318. * Set the Top Left cell position for the chart
  319. *
  320. * @param string $cell
  321. * @return PHPExcel_Chart
  322. */
  323. public function setTopLeftCell($cell) {
  324. $this->_topLeftCellRef = $cell;
  325. return $this;
  326. }
  327. /**
  328. * Set the offset position within the Top Left cell for the chart
  329. *
  330. * @param integer $xOffset
  331. * @param integer $yOffset
  332. * @return PHPExcel_Chart
  333. */
  334. public function setTopLeftOffset($xOffset=null,$yOffset=null) {
  335. if (!is_null($xOffset))
  336. $this->setTopLeftXOffset($xOffset);
  337. if (!is_null($yOffset))
  338. $this->setTopLeftYOffset($yOffset);
  339. return $this;
  340. }
  341. /**
  342. * Get the offset position within the Top Left cell for the chart
  343. *
  344. * @return integer[]
  345. */
  346. public function getTopLeftOffset() {
  347. return array( 'X' => $this->_topLeftXOffset,
  348. 'Y' => $this->_topLeftYOffset
  349. );
  350. }
  351. public function setTopLeftXOffset($xOffset) {
  352. $this->_topLeftXOffset = $xOffset;
  353. return $this;
  354. }
  355. public function getTopLeftXOffset() {
  356. return $this->_topLeftXOffset;
  357. }
  358. public function setTopLeftYOffset($yOffset) {
  359. $this->_topLeftYOffset = $yOffset;
  360. return $this;
  361. }
  362. public function getTopLeftYOffset() {
  363. return $this->_topLeftYOffset;
  364. }
  365. /**
  366. * Set the Bottom Right position of the chart
  367. *
  368. * @param string $cell
  369. * @param integer $xOffset
  370. * @param integer $yOffset
  371. * @return PHPExcel_Chart
  372. */
  373. public function setBottomRightPosition($cell, $xOffset=null, $yOffset=null) {
  374. $this->_bottomRightCellRef = $cell;
  375. if (!is_null($xOffset))
  376. $this->setBottomRightXOffset($xOffset);
  377. if (!is_null($yOffset))
  378. $this->setBottomRightYOffset($yOffset);
  379. return $this;
  380. }
  381. /**
  382. * Get the bottom right position of the chart
  383. *
  384. * @return array an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell
  385. */
  386. public function getBottomRightPosition() {
  387. return array( 'cell' => $this->_bottomRightCellRef,
  388. 'xOffset' => $this->_bottomRightXOffset,
  389. 'yOffset' => $this->_bottomRightYOffset
  390. );
  391. }
  392. public function setBottomRightCell($cell) {
  393. $this->_bottomRightCellRef = $cell;
  394. return $this;
  395. }
  396. /**
  397. * Get the cell address where the bottom right of the chart is fixed
  398. *
  399. * @return string
  400. */
  401. public function getBottomRightCell() {
  402. return $this->_bottomRightCellRef;
  403. }
  404. /**
  405. * Set the offset position within the Bottom Right cell for the chart
  406. *
  407. * @param integer $xOffset
  408. * @param integer $yOffset
  409. * @return PHPExcel_Chart
  410. */
  411. public function setBottomRightOffset($xOffset=null,$yOffset=null) {
  412. if (!is_null($xOffset))
  413. $this->setBottomRightXOffset($xOffset);
  414. if (!is_null($yOffset))
  415. $this->setBottomRightYOffset($yOffset);
  416. return $this;
  417. }
  418. /**
  419. * Get the offset position within the Bottom Right cell for the chart
  420. *
  421. * @return integer[]
  422. */
  423. public function getBottomRightOffset() {
  424. return array( 'X' => $this->_bottomRightXOffset,
  425. 'Y' => $this->_bottomRightYOffset
  426. );
  427. }
  428. public function setBottomRightXOffset($xOffset) {
  429. $this->_bottomRightXOffset = $xOffset;
  430. return $this;
  431. }
  432. public function getBottomRightXOffset() {
  433. return $this->_bottomRightXOffset;
  434. }
  435. public function setBottomRightYOffset($yOffset) {
  436. $this->_bottomRightYOffset = $yOffset;
  437. return $this;
  438. }
  439. public function getBottomRightYOffset() {
  440. return $this->_bottomRightYOffset;
  441. }
  442. public function refresh() {
  443. if ($this->_worksheet !== NULL) {
  444. $this->_plotArea->refresh($this->_worksheet);
  445. }
  446. }
  447. public function render($outputDestination = null) {
  448. $libraryName = PHPExcel_Settings::getChartRendererName();
  449. if (is_null($libraryName)) {
  450. return false;
  451. }
  452. // Ensure that data series values are up-to-date before we render
  453. $this->refresh();
  454. $libraryPath = PHPExcel_Settings::getChartRendererPath();
  455. $includePath = str_replace('\\','/',get_include_path());
  456. $rendererPath = str_replace('\\','/',$libraryPath);
  457. if (strpos($rendererPath,$includePath) === false) {
  458. set_include_path(get_include_path() . PATH_SEPARATOR . $libraryPath);
  459. }
  460. $rendererName = 'PHPExcel_Chart_Renderer_'.$libraryName;
  461. $renderer = new $rendererName($this);
  462. if ($outputDestination == 'php://output') {
  463. $outputDestination = null;
  464. }
  465. return $renderer->render($outputDestination);
  466. }
  467. }