Chart.php 14 KB

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