Chart.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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_Writer_Excel2007
  23. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel2007_Chart
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPart
  35. {
  36. /**
  37. * Write charts to XML format
  38. *
  39. * @param PHPExcel_Chart $pChart
  40. * @return string XML Output
  41. * @throws PHPExcel_Writer_Exception
  42. */
  43. public function writeChart(PHPExcel_Chart $pChart = null)
  44. {
  45. // Create XML writer
  46. $objWriter = null;
  47. if ($this->getParentWriter()->getUseDiskCaching()) {
  48. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  49. } else {
  50. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  51. }
  52. // Ensure that data series values are up-to-date before we save
  53. $pChart->refresh();
  54. // XML header
  55. $objWriter->startDocument('1.0','UTF-8','yes');
  56. // c:chartSpace
  57. $objWriter->startElement('c:chartSpace');
  58. $objWriter->writeAttribute('xmlns:c', 'http://schemas.openxmlformats.org/drawingml/2006/chart');
  59. $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
  60. $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  61. $objWriter->startElement('c:date1904');
  62. $objWriter->writeAttribute('val', 0);
  63. $objWriter->endElement();
  64. $objWriter->startElement('c:lang');
  65. $objWriter->writeAttribute('val', "en-GB");
  66. $objWriter->endElement();
  67. $objWriter->startElement('c:roundedCorners');
  68. $objWriter->writeAttribute('val', 0);
  69. $objWriter->endElement();
  70. $this->_writeAlternateContent($objWriter);
  71. $objWriter->startElement('c:chart');
  72. $this->_writeTitle($pChart->getTitle(), $objWriter);
  73. $objWriter->startElement('c:autoTitleDeleted');
  74. $objWriter->writeAttribute('val', 0);
  75. $objWriter->endElement();
  76. $this->_writePlotArea($pChart->getPlotArea(),
  77. $pChart->getXAxisLabel(),
  78. $pChart->getYAxisLabel(),
  79. $objWriter,
  80. $pChart->getWorksheet()
  81. );
  82. $this->_writeLegend($pChart->getLegend(), $objWriter);
  83. $objWriter->startElement('c:plotVisOnly');
  84. $objWriter->writeAttribute('val', 1);
  85. $objWriter->endElement();
  86. $objWriter->startElement('c:dispBlanksAs');
  87. $objWriter->writeAttribute('val', "gap");
  88. $objWriter->endElement();
  89. $objWriter->startElement('c:showDLblsOverMax');
  90. $objWriter->writeAttribute('val', 0);
  91. $objWriter->endElement();
  92. $objWriter->endElement();
  93. $this->_writePrintSettings($objWriter);
  94. $objWriter->endElement();
  95. // Return
  96. return $objWriter->getData();
  97. }
  98. /**
  99. * Write Chart Title
  100. *
  101. * @param PHPExcel_Chart_Title $title
  102. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  103. * @throws PHPExcel_Writer_Exception
  104. */
  105. private function _writeTitle(PHPExcel_Chart_Title $title = null, $objWriter)
  106. {
  107. if (is_null($title)) {
  108. return;
  109. }
  110. $objWriter->startElement('c:title');
  111. $objWriter->startElement('c:tx');
  112. $objWriter->startElement('c:rich');
  113. $objWriter->startElement('a:bodyPr');
  114. $objWriter->endElement();
  115. $objWriter->startElement('a:lstStyle');
  116. $objWriter->endElement();
  117. $objWriter->startElement('a:p');
  118. $caption = $title->getCaption();
  119. if ((is_array($caption)) && (count($caption) > 0))
  120. $caption = $caption[0];
  121. $this->getParentWriter()->getWriterPart('stringtable')->writeRichTextForCharts($objWriter, $caption, 'a');
  122. $objWriter->endElement();
  123. $objWriter->endElement();
  124. $objWriter->endElement();
  125. $layout = $title->getLayout();
  126. $this->_writeLayout($layout, $objWriter);
  127. $objWriter->startElement('c:overlay');
  128. $objWriter->writeAttribute('val', 0);
  129. $objWriter->endElement();
  130. $objWriter->endElement();
  131. }
  132. /**
  133. * Write Chart Legend
  134. *
  135. * @param PHPExcel_Chart_Legend $legend
  136. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  137. * @throws PHPExcel_Writer_Exception
  138. */
  139. private function _writeLegend(PHPExcel_Chart_Legend $legend = null, $objWriter)
  140. {
  141. if (is_null($legend)) {
  142. return;
  143. }
  144. $objWriter->startElement('c:legend');
  145. $objWriter->startElement('c:legendPos');
  146. $objWriter->writeAttribute('val', $legend->getPosition());
  147. $objWriter->endElement();
  148. $layout = $legend->getLayout();
  149. $this->_writeLayout($layout, $objWriter);
  150. $objWriter->startElement('c:overlay');
  151. $objWriter->writeAttribute('val', ($legend->getOverlay()) ? '1' : '0');
  152. $objWriter->endElement();
  153. $objWriter->startElement('c:txPr');
  154. $objWriter->startElement('a:bodyPr');
  155. $objWriter->endElement();
  156. $objWriter->startElement('a:lstStyle');
  157. $objWriter->endElement();
  158. $objWriter->startElement('a:p');
  159. $objWriter->startElement('a:pPr');
  160. $objWriter->writeAttribute('rtl', 0);
  161. $objWriter->startElement('a:defRPr');
  162. $objWriter->endElement();
  163. $objWriter->endElement();
  164. $objWriter->startElement('a:endParaRPr');
  165. $objWriter->writeAttribute('lang', "en-US");
  166. $objWriter->endElement();
  167. $objWriter->endElement();
  168. $objWriter->endElement();
  169. $objWriter->endElement();
  170. }
  171. /**
  172. * Write Chart Plot Area
  173. *
  174. * @param PHPExcel_Chart_PlotArea $plotArea
  175. * @param PHPExcel_Chart_Title $xAxisLabel
  176. * @param PHPExcel_Chart_Title $yAxisLabel
  177. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  178. * @throws PHPExcel_Writer_Exception
  179. */
  180. private function _writePlotArea(PHPExcel_Chart_PlotArea $plotArea,
  181. PHPExcel_Chart_Title $xAxisLabel = NULL,
  182. PHPExcel_Chart_Title $yAxisLabel = NULL,
  183. $objWriter,
  184. PHPExcel_Worksheet $pSheet)
  185. {
  186. if (is_null($plotArea)) {
  187. return;
  188. }
  189. $id1 = $id2 = 0;
  190. $this->_seriesIndex = 0;
  191. $objWriter->startElement('c:plotArea');
  192. $layout = $plotArea->getLayout();
  193. $this->_writeLayout($layout, $objWriter);
  194. $chartTypes = self::_getChartType($plotArea);
  195. $catIsMultiLevelSeries = $valIsMultiLevelSeries = FALSE;
  196. $plotGroupingType = '';
  197. foreach($chartTypes as $chartType) {
  198. $objWriter->startElement('c:'.$chartType);
  199. $groupCount = $plotArea->getPlotGroupCount();
  200. for($i = 0; $i < $groupCount; ++$i) {
  201. $plotGroup = $plotArea->getPlotGroupByIndex($i);
  202. $groupType = $plotGroup->getPlotType();
  203. if ($groupType == $chartType) {
  204. $plotStyle = $plotGroup->getPlotStyle();
  205. if ($groupType === PHPExcel_Chart_DataSeries::TYPE_RADARCHART) {
  206. $objWriter->startElement('c:radarStyle');
  207. $objWriter->writeAttribute('val', $plotStyle );
  208. $objWriter->endElement();
  209. } elseif ($groupType === PHPExcel_Chart_DataSeries::TYPE_SCATTERCHART) {
  210. $objWriter->startElement('c:scatterStyle');
  211. $objWriter->writeAttribute('val', $plotStyle );
  212. $objWriter->endElement();
  213. }
  214. $this->_writePlotGroup($plotGroup, $chartType, $objWriter, $catIsMultiLevelSeries, $valIsMultiLevelSeries, $plotGroupingType, $pSheet);
  215. }
  216. }
  217. $this->_writeDataLbls($objWriter, $layout);
  218. if ($chartType === PHPExcel_Chart_DataSeries::TYPE_LINECHART) {
  219. // Line only, Line3D can't be smoothed
  220. $objWriter->startElement('c:smooth');
  221. $objWriter->writeAttribute('val', (integer) $plotGroup->getSmoothLine() );
  222. $objWriter->endElement();
  223. } elseif (($chartType === PHPExcel_Chart_DataSeries::TYPE_BARCHART) ||
  224. ($chartType === PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D)) {
  225. $objWriter->startElement('c:gapWidth');
  226. $objWriter->writeAttribute('val', 150 );
  227. $objWriter->endElement();
  228. if ($plotGroupingType == 'percentStacked' ||
  229. $plotGroupingType == 'stacked') {
  230. $objWriter->startElement('c:overlap');
  231. $objWriter->writeAttribute('val', 100 );
  232. $objWriter->endElement();
  233. }
  234. } elseif ($chartType === PHPExcel_Chart_DataSeries::TYPE_BUBBLECHART) {
  235. $objWriter->startElement('c:bubbleScale');
  236. $objWriter->writeAttribute('val', 25 );
  237. $objWriter->endElement();
  238. $objWriter->startElement('c:showNegBubbles');
  239. $objWriter->writeAttribute('val', 0 );
  240. $objWriter->endElement();
  241. } elseif ($chartType === PHPExcel_Chart_DataSeries::TYPE_STOCKCHART) {
  242. $objWriter->startElement('c:hiLowLines');
  243. $objWriter->endElement();
  244. }
  245. // Generate 2 unique numbers to use for axId values
  246. // $id1 = $id2 = rand(10000000,99999999);
  247. // do {
  248. // $id2 = rand(10000000,99999999);
  249. // } while ($id1 == $id2);
  250. $id1 = '75091328';
  251. $id2 = '75089408';
  252. if (($chartType !== PHPExcel_Chart_DataSeries::TYPE_PIECHART) &&
  253. ($chartType !== PHPExcel_Chart_DataSeries::TYPE_PIECHART_3D) &&
  254. ($chartType !== PHPExcel_Chart_DataSeries::TYPE_DONUTCHART)) {
  255. $objWriter->startElement('c:axId');
  256. $objWriter->writeAttribute('val', $id1 );
  257. $objWriter->endElement();
  258. $objWriter->startElement('c:axId');
  259. $objWriter->writeAttribute('val', $id2 );
  260. $objWriter->endElement();
  261. } else {
  262. $objWriter->startElement('c:firstSliceAng');
  263. $objWriter->writeAttribute('val', 0);
  264. $objWriter->endElement();
  265. if ($chartType === PHPExcel_Chart_DataSeries::TYPE_DONUTCHART) {
  266. $objWriter->startElement('c:holeSize');
  267. $objWriter->writeAttribute('val', 50);
  268. $objWriter->endElement();
  269. }
  270. }
  271. $objWriter->endElement();
  272. }
  273. if (($chartType !== PHPExcel_Chart_DataSeries::TYPE_PIECHART) &&
  274. ($chartType !== PHPExcel_Chart_DataSeries::TYPE_PIECHART_3D) &&
  275. ($chartType !== PHPExcel_Chart_DataSeries::TYPE_DONUTCHART)) {
  276. if ($chartType === PHPExcel_Chart_DataSeries::TYPE_BUBBLECHART) {
  277. $this->_writeValAx($objWriter,$plotArea,$xAxisLabel,$chartType,$id1,$id2,$catIsMultiLevelSeries);
  278. } else {
  279. $this->_writeCatAx($objWriter,$plotArea,$xAxisLabel,$chartType,$id1,$id2,$catIsMultiLevelSeries);
  280. }
  281. $this->_writeValAx($objWriter,$plotArea,$yAxisLabel,$chartType,$id1,$id2,$valIsMultiLevelSeries);
  282. }
  283. $objWriter->endElement();
  284. }
  285. /**
  286. * Write Data Labels
  287. *
  288. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  289. * @param PHPExcel_Chart_Layout $chartLayout Chart layout
  290. * @throws PHPExcel_Writer_Exception
  291. */
  292. private function _writeDataLbls($objWriter, $chartLayout)
  293. {
  294. $objWriter->startElement('c:dLbls');
  295. $objWriter->startElement('c:showLegendKey');
  296. $showLegendKey = (empty($chartLayout)) ? 0 : $chartLayout->getShowLegendKey();
  297. $objWriter->writeAttribute('val', ((empty($showLegendKey)) ? 0 : 1) );
  298. $objWriter->endElement();
  299. $objWriter->startElement('c:showVal');
  300. $showVal = (empty($chartLayout)) ? 0 : $chartLayout->getShowVal();
  301. $objWriter->writeAttribute('val', ((empty($showVal)) ? 0 : 1) );
  302. $objWriter->endElement();
  303. $objWriter->startElement('c:showCatName');
  304. $showCatName = (empty($chartLayout)) ? 0 : $chartLayout->getShowCatName();
  305. $objWriter->writeAttribute('val', ((empty($showCatName)) ? 0 : 1) );
  306. $objWriter->endElement();
  307. $objWriter->startElement('c:showSerName');
  308. $showSerName = (empty($chartLayout)) ? 0 : $chartLayout->getShowSerName();
  309. $objWriter->writeAttribute('val', ((empty($showSerName)) ? 0 : 1) );
  310. $objWriter->endElement();
  311. $objWriter->startElement('c:showPercent');
  312. $showPercent = (empty($chartLayout)) ? 0 : $chartLayout->getShowPercent();
  313. $objWriter->writeAttribute('val', ((empty($showPercent)) ? 0 : 1) );
  314. $objWriter->endElement();
  315. $objWriter->startElement('c:showBubbleSize');
  316. $showBubbleSize = (empty($chartLayout)) ? 0 : $chartLayout->getShowBubbleSize();
  317. $objWriter->writeAttribute('val', ((empty($showBubbleSize)) ? 0 : 1) );
  318. $objWriter->endElement();
  319. $objWriter->startElement('c:showLeaderLines');
  320. $showLeaderLines = (empty($chartLayout)) ? 1 : $chartLayout->getShowLeaderLines();
  321. $objWriter->writeAttribute('val', ((empty($showLeaderLines)) ? 0 : 1) );
  322. $objWriter->endElement();
  323. $objWriter->endElement();
  324. }
  325. /**
  326. * Write Category Axis
  327. *
  328. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  329. * @param PHPExcel_Chart_PlotArea $plotArea
  330. * @param PHPExcel_Chart_Title $xAxisLabel
  331. * @param string $groupType Chart type
  332. * @param string $id1
  333. * @param string $id2
  334. * @param boolean $isMultiLevelSeries
  335. * @throws PHPExcel_Writer_Exception
  336. */
  337. private function _writeCatAx($objWriter, PHPExcel_Chart_PlotArea $plotArea, $xAxisLabel, $groupType, $id1, $id2, $isMultiLevelSeries)
  338. {
  339. $objWriter->startElement('c:catAx');
  340. if ($id1 > 0) {
  341. $objWriter->startElement('c:axId');
  342. $objWriter->writeAttribute('val', $id1);
  343. $objWriter->endElement();
  344. }
  345. $objWriter->startElement('c:scaling');
  346. $objWriter->startElement('c:orientation');
  347. $objWriter->writeAttribute('val', "minMax");
  348. $objWriter->endElement();
  349. $objWriter->endElement();
  350. $objWriter->startElement('c:delete');
  351. $objWriter->writeAttribute('val', 0);
  352. $objWriter->endElement();
  353. $objWriter->startElement('c:axPos');
  354. $objWriter->writeAttribute('val', "b");
  355. $objWriter->endElement();
  356. if (!is_null($xAxisLabel)) {
  357. $objWriter->startElement('c:title');
  358. $objWriter->startElement('c:tx');
  359. $objWriter->startElement('c:rich');
  360. $objWriter->startElement('a:bodyPr');
  361. $objWriter->endElement();
  362. $objWriter->startElement('a:lstStyle');
  363. $objWriter->endElement();
  364. $objWriter->startElement('a:p');
  365. $objWriter->startElement('a:r');
  366. $caption = $xAxisLabel->getCaption();
  367. if (is_array($caption))
  368. $caption = $caption[0];
  369. $objWriter->startElement('a:t');
  370. // $objWriter->writeAttribute('xml:space', 'preserve');
  371. $objWriter->writeRawData(PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $caption ));
  372. $objWriter->endElement();
  373. $objWriter->endElement();
  374. $objWriter->endElement();
  375. $objWriter->endElement();
  376. $objWriter->endElement();
  377. $layout = $xAxisLabel->getLayout();
  378. $this->_writeLayout($layout, $objWriter);
  379. $objWriter->startElement('c:overlay');
  380. $objWriter->writeAttribute('val', 0);
  381. $objWriter->endElement();
  382. $objWriter->endElement();
  383. }
  384. $objWriter->startElement('c:numFmt');
  385. $objWriter->writeAttribute('formatCode', "General");
  386. $objWriter->writeAttribute('sourceLinked', 1);
  387. $objWriter->endElement();
  388. $objWriter->startElement('c:majorTickMark');
  389. $objWriter->writeAttribute('val', "out");
  390. $objWriter->endElement();
  391. $objWriter->startElement('c:minorTickMark');
  392. $objWriter->writeAttribute('val', "none");
  393. $objWriter->endElement();
  394. $objWriter->startElement('c:tickLblPos');
  395. $objWriter->writeAttribute('val', "nextTo");
  396. $objWriter->endElement();
  397. if ($id2 > 0) {
  398. $objWriter->startElement('c:crossAx');
  399. $objWriter->writeAttribute('val', $id2);
  400. $objWriter->endElement();
  401. $objWriter->startElement('c:crosses');
  402. $objWriter->writeAttribute('val', "autoZero");
  403. $objWriter->endElement();
  404. }
  405. $objWriter->startElement('c:auto');
  406. $objWriter->writeAttribute('val', 1);
  407. $objWriter->endElement();
  408. $objWriter->startElement('c:lblAlgn');
  409. $objWriter->writeAttribute('val', "ctr");
  410. $objWriter->endElement();
  411. $objWriter->startElement('c:lblOffset');
  412. $objWriter->writeAttribute('val', 100);
  413. $objWriter->endElement();
  414. if ($isMultiLevelSeries) {
  415. $objWriter->startElement('c:noMultiLvlLbl');
  416. $objWriter->writeAttribute('val', 0);
  417. $objWriter->endElement();
  418. }
  419. $objWriter->endElement();
  420. }
  421. /**
  422. * Write Value Axis
  423. *
  424. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  425. * @param PHPExcel_Chart_PlotArea $plotArea
  426. * @param PHPExcel_Chart_Title $yAxisLabel
  427. * @param string $groupType Chart type
  428. * @param string $id1
  429. * @param string $id2
  430. * @param boolean $isMultiLevelSeries
  431. * @throws PHPExcel_Writer_Exception
  432. */
  433. private function _writeValAx($objWriter, PHPExcel_Chart_PlotArea $plotArea, $yAxisLabel, $groupType, $id1, $id2, $isMultiLevelSeries)
  434. {
  435. $objWriter->startElement('c:valAx');
  436. if ($id2 > 0) {
  437. $objWriter->startElement('c:axId');
  438. $objWriter->writeAttribute('val', $id2);
  439. $objWriter->endElement();
  440. }
  441. $objWriter->startElement('c:scaling');
  442. $objWriter->startElement('c:orientation');
  443. $objWriter->writeAttribute('val', "minMax");
  444. $objWriter->endElement();
  445. $objWriter->endElement();
  446. $objWriter->startElement('c:delete');
  447. $objWriter->writeAttribute('val', 0);
  448. $objWriter->endElement();
  449. $objWriter->startElement('c:axPos');
  450. $objWriter->writeAttribute('val', "l");
  451. $objWriter->endElement();
  452. $objWriter->startElement('c:majorGridlines');
  453. $objWriter->endElement();
  454. if (!is_null($yAxisLabel)) {
  455. $objWriter->startElement('c:title');
  456. $objWriter->startElement('c:tx');
  457. $objWriter->startElement('c:rich');
  458. $objWriter->startElement('a:bodyPr');
  459. $objWriter->endElement();
  460. $objWriter->startElement('a:lstStyle');
  461. $objWriter->endElement();
  462. $objWriter->startElement('a:p');
  463. $objWriter->startElement('a:r');
  464. $caption = $yAxisLabel->getCaption();
  465. if (is_array($caption))
  466. $caption = $caption[0];
  467. $objWriter->startElement('a:t');
  468. // $objWriter->writeAttribute('xml:space', 'preserve');
  469. $objWriter->writeRawData(PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $caption ));
  470. $objWriter->endElement();
  471. $objWriter->endElement();
  472. $objWriter->endElement();
  473. $objWriter->endElement();
  474. $objWriter->endElement();
  475. if ($groupType !== PHPExcel_Chart_DataSeries::TYPE_BUBBLECHART) {
  476. $layout = $yAxisLabel->getLayout();
  477. $this->_writeLayout($layout, $objWriter);
  478. }
  479. $objWriter->startElement('c:overlay');
  480. $objWriter->writeAttribute('val', 0);
  481. $objWriter->endElement();
  482. $objWriter->endElement();
  483. }
  484. $objWriter->startElement('c:numFmt');
  485. $objWriter->writeAttribute('formatCode', "General");
  486. $objWriter->writeAttribute('sourceLinked', 1);
  487. $objWriter->endElement();
  488. $objWriter->startElement('c:majorTickMark');
  489. $objWriter->writeAttribute('val', "out");
  490. $objWriter->endElement();
  491. $objWriter->startElement('c:minorTickMark');
  492. $objWriter->writeAttribute('val', "none");
  493. $objWriter->endElement();
  494. $objWriter->startElement('c:tickLblPos');
  495. $objWriter->writeAttribute('val', "nextTo");
  496. $objWriter->endElement();
  497. if ($id1 > 0) {
  498. $objWriter->startElement('c:crossAx');
  499. $objWriter->writeAttribute('val', $id2);
  500. $objWriter->endElement();
  501. $objWriter->startElement('c:crosses');
  502. $objWriter->writeAttribute('val', "autoZero");
  503. $objWriter->endElement();
  504. $objWriter->startElement('c:crossBetween');
  505. $objWriter->writeAttribute('val', "midCat");
  506. $objWriter->endElement();
  507. }
  508. if ($isMultiLevelSeries) {
  509. if ($groupType !== PHPExcel_Chart_DataSeries::TYPE_BUBBLECHART) {
  510. $objWriter->startElement('c:noMultiLvlLbl');
  511. $objWriter->writeAttribute('val', 0);
  512. $objWriter->endElement();
  513. }
  514. }
  515. $objWriter->endElement();
  516. }
  517. /**
  518. * Get the data series type(s) for a chart plot series
  519. *
  520. * @param PHPExcel_Chart_PlotArea $plotArea
  521. * @return string|array
  522. * @throws PHPExcel_Writer_Exception
  523. */
  524. private static function _getChartType($plotArea)
  525. {
  526. $groupCount = $plotArea->getPlotGroupCount();
  527. if ($groupCount == 1) {
  528. $chartType = array($plotArea->getPlotGroupByIndex(0)->getPlotType());
  529. } else {
  530. $chartTypes = array();
  531. for($i = 0; $i < $groupCount; ++$i) {
  532. $chartTypes[] = $plotArea->getPlotGroupByIndex($i)->getPlotType();
  533. }
  534. $chartType = array_unique($chartTypes);
  535. if (count($chartTypes) == 0) {
  536. throw new PHPExcel_Writer_Exception('Chart is not yet implemented');
  537. }
  538. }
  539. return $chartType;
  540. }
  541. /**
  542. * Write Plot Group (series of related plots)
  543. *
  544. * @param PHPExcel_Chart_DataSeries $plotGroup
  545. * @param string $groupType Type of plot for dataseries
  546. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  547. * @param boolean &$catIsMultiLevelSeries Is category a multi-series category
  548. * @param boolean &$valIsMultiLevelSeries Is value set a multi-series set
  549. * @param string &$plotGroupingType Type of grouping for multi-series values
  550. * @param PHPExcel_Worksheet $pSheet
  551. * @throws PHPExcel_Writer_Exception
  552. */
  553. private function _writePlotGroup( $plotGroup,
  554. $groupType,
  555. $objWriter,
  556. &$catIsMultiLevelSeries,
  557. &$valIsMultiLevelSeries,
  558. &$plotGroupingType,
  559. PHPExcel_Worksheet $pSheet
  560. )
  561. {
  562. if (is_null($plotGroup)) {
  563. return;
  564. }
  565. if (($groupType == PHPExcel_Chart_DataSeries::TYPE_BARCHART) ||
  566. ($groupType == PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D)) {
  567. $objWriter->startElement('c:barDir');
  568. $objWriter->writeAttribute('val', $plotGroup->getPlotDirection());
  569. $objWriter->endElement();
  570. }
  571. if (!is_null($plotGroup->getPlotGrouping())) {
  572. $plotGroupingType = $plotGroup->getPlotGrouping();
  573. $objWriter->startElement('c:grouping');
  574. $objWriter->writeAttribute('val', $plotGroupingType);
  575. $objWriter->endElement();
  576. }
  577. // Get these details before the loop, because we can use the count to check for varyColors
  578. $plotSeriesOrder = $plotGroup->getPlotOrder();
  579. $plotSeriesCount = count($plotSeriesOrder);
  580. if (($groupType !== PHPExcel_Chart_DataSeries::TYPE_RADARCHART) &&
  581. ($groupType !== PHPExcel_Chart_DataSeries::TYPE_STOCKCHART)) {
  582. if ($groupType !== PHPExcel_Chart_DataSeries::TYPE_LINECHART) {
  583. if (($groupType == PHPExcel_Chart_DataSeries::TYPE_PIECHART) ||
  584. ($groupType == PHPExcel_Chart_DataSeries::TYPE_PIECHART_3D) ||
  585. ($groupType == PHPExcel_Chart_DataSeries::TYPE_DONUTCHART) ||
  586. ($plotSeriesCount > 1)) {
  587. $objWriter->startElement('c:varyColors');
  588. $objWriter->writeAttribute('val', 1);
  589. $objWriter->endElement();
  590. } else {
  591. $objWriter->startElement('c:varyColors');
  592. $objWriter->writeAttribute('val', 0);
  593. $objWriter->endElement();
  594. }
  595. }
  596. }
  597. foreach($plotSeriesOrder as $plotSeriesIdx => $plotSeriesRef) {
  598. $objWriter->startElement('c:ser');
  599. $objWriter->startElement('c:idx');
  600. $objWriter->writeAttribute('val', $this->_seriesIndex + $plotSeriesIdx);
  601. $objWriter->endElement();
  602. $objWriter->startElement('c:order');
  603. $objWriter->writeAttribute('val', $this->_seriesIndex + $plotSeriesRef);
  604. $objWriter->endElement();
  605. if (($groupType == PHPExcel_Chart_DataSeries::TYPE_PIECHART) ||
  606. ($groupType == PHPExcel_Chart_DataSeries::TYPE_PIECHART_3D) ||
  607. ($groupType == PHPExcel_Chart_DataSeries::TYPE_DONUTCHART)) {
  608. $objWriter->startElement('c:dPt');
  609. $objWriter->startElement('c:idx');
  610. $objWriter->writeAttribute('val', 3);
  611. $objWriter->endElement();
  612. $objWriter->startElement('c:bubble3D');
  613. $objWriter->writeAttribute('val', 0);
  614. $objWriter->endElement();
  615. $objWriter->startElement('c:spPr');
  616. $objWriter->startElement('a:solidFill');
  617. $objWriter->startElement('a:srgbClr');
  618. $objWriter->writeAttribute('val', 'FF9900');
  619. $objWriter->endElement();
  620. $objWriter->endElement();
  621. $objWriter->endElement();
  622. $objWriter->endElement();
  623. }
  624. // Labels
  625. $plotSeriesLabel = $plotGroup->getPlotLabelByIndex($plotSeriesRef);
  626. if ($plotSeriesLabel && ($plotSeriesLabel->getPointCount() > 0)) {
  627. $objWriter->startElement('c:tx');
  628. $objWriter->startElement('c:strRef');
  629. $this->_writePlotSeriesLabel($plotSeriesLabel, $objWriter);
  630. $objWriter->endElement();
  631. $objWriter->endElement();
  632. }
  633. // Formatting for the points
  634. if ($groupType == PHPExcel_Chart_DataSeries::TYPE_LINECHART) {
  635. $objWriter->startElement('c:spPr');
  636. $objWriter->startElement('a:ln');
  637. $objWriter->writeAttribute('w', 12700);
  638. $objWriter->endElement();
  639. $objWriter->endElement();
  640. }
  641. $plotSeriesValues = $plotGroup->getPlotValuesByIndex($plotSeriesRef);
  642. if ($plotSeriesValues) {
  643. $plotSeriesMarker = $plotSeriesValues->getPointMarker();
  644. if ($plotSeriesMarker) {
  645. $objWriter->startElement('c:marker');
  646. $objWriter->startElement('c:symbol');
  647. $objWriter->writeAttribute('val', $plotSeriesMarker);
  648. $objWriter->endElement();
  649. if ($plotSeriesMarker !== 'none') {
  650. $objWriter->startElement('c:size');
  651. $objWriter->writeAttribute('val', 3);
  652. $objWriter->endElement();
  653. }
  654. $objWriter->endElement();
  655. }
  656. }
  657. if (($groupType === PHPExcel_Chart_DataSeries::TYPE_BARCHART) ||
  658. ($groupType === PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D) ||
  659. ($groupType === PHPExcel_Chart_DataSeries::TYPE_BUBBLECHART)) {
  660. $objWriter->startElement('c:invertIfNegative');
  661. $objWriter->writeAttribute('val', 0);
  662. $objWriter->endElement();
  663. }
  664. // Category Labels
  665. $plotSeriesCategory = $plotGroup->getPlotCategoryByIndex($plotSeriesRef);
  666. if ($plotSeriesCategory && ($plotSeriesCategory->getPointCount() > 0)) {
  667. $catIsMultiLevelSeries = $catIsMultiLevelSeries || $plotSeriesCategory->isMultiLevelSeries();
  668. if (($groupType == PHPExcel_Chart_DataSeries::TYPE_PIECHART) ||
  669. ($groupType == PHPExcel_Chart_DataSeries::TYPE_PIECHART_3D) ||
  670. ($groupType == PHPExcel_Chart_DataSeries::TYPE_DONUTCHART)) {
  671. if (!is_null($plotGroup->getPlotStyle())) {
  672. $plotStyle = $plotGroup->getPlotStyle();
  673. if ($plotStyle) {
  674. $objWriter->startElement('c:explosion');
  675. $objWriter->writeAttribute('val', 25);
  676. $objWriter->endElement();
  677. }
  678. }
  679. }
  680. if (($groupType === PHPExcel_Chart_DataSeries::TYPE_BUBBLECHART) ||
  681. ($groupType === PHPExcel_Chart_DataSeries::TYPE_SCATTERCHART)) {
  682. $objWriter->startElement('c:xVal');
  683. } else {
  684. $objWriter->startElement('c:cat');
  685. }
  686. $this->_writePlotSeriesValues($plotSeriesCategory, $objWriter, $groupType, 'str', $pSheet);
  687. $objWriter->endElement();
  688. }
  689. // Values
  690. if ($plotSeriesValues) {
  691. $valIsMultiLevelSeries = $valIsMultiLevelSeries || $plotSeriesValues->isMultiLevelSeries();
  692. if (($groupType === PHPExcel_Chart_DataSeries::TYPE_BUBBLECHART) ||
  693. ($groupType === PHPExcel_Chart_DataSeries::TYPE_SCATTERCHART)) {
  694. $objWriter->startElement('c:yVal');
  695. } else {
  696. $objWriter->startElement('c:val');
  697. }
  698. $this->_writePlotSeriesValues($plotSeriesValues, $objWriter, $groupType, 'num', $pSheet);
  699. $objWriter->endElement();
  700. }
  701. if ($groupType === PHPExcel_Chart_DataSeries::TYPE_BUBBLECHART) {
  702. $this->_writeBubbles($plotSeriesValues, $objWriter, $pSheet);
  703. }
  704. $objWriter->endElement();
  705. }
  706. $this->_seriesIndex += $plotSeriesIdx + 1;
  707. }
  708. /**
  709. * Write Plot Series Label
  710. *
  711. * @param PHPExcel_Chart_DataSeriesValues $plotSeriesLabel
  712. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  713. * @throws PHPExcel_Writer_Exception
  714. */
  715. private function _writePlotSeriesLabel($plotSeriesLabel, $objWriter)
  716. {
  717. if (is_null($plotSeriesLabel)) {
  718. return;
  719. }
  720. $objWriter->startElement('c:f');
  721. $objWriter->writeRawData($plotSeriesLabel->getDataSource());
  722. $objWriter->endElement();
  723. $objWriter->startElement('c:strCache');
  724. $objWriter->startElement('c:ptCount');
  725. $objWriter->writeAttribute('val', $plotSeriesLabel->getPointCount() );
  726. $objWriter->endElement();
  727. foreach($plotSeriesLabel->getDataValues() as $plotLabelKey => $plotLabelValue) {
  728. $objWriter->startElement('c:pt');
  729. $objWriter->writeAttribute('idx', $plotLabelKey );
  730. $objWriter->startElement('c:v');
  731. $objWriter->writeRawData( $plotLabelValue );
  732. $objWriter->endElement();
  733. $objWriter->endElement();
  734. }
  735. $objWriter->endElement();
  736. }
  737. /**
  738. * Write Plot Series Values
  739. *
  740. * @param PHPExcel_Chart_DataSeriesValues $plotSeriesValues
  741. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  742. * @param string $groupType Type of plot for dataseries
  743. * @param string $dataType Datatype of series values
  744. * @param PHPExcel_Worksheet $pSheet
  745. * @throws PHPExcel_Writer_Exception
  746. */
  747. private function _writePlotSeriesValues( $plotSeriesValues,
  748. $objWriter,
  749. $groupType,
  750. $dataType='str',
  751. PHPExcel_Worksheet $pSheet
  752. )
  753. {
  754. if (is_null($plotSeriesValues)) {
  755. return;
  756. }
  757. if ($plotSeriesValues->isMultiLevelSeries()) {
  758. $levelCount = $plotSeriesValues->multiLevelCount();
  759. $objWriter->startElement('c:multiLvlStrRef');
  760. $objWriter->startElement('c:f');
  761. $objWriter->writeRawData( $plotSeriesValues->getDataSource() );
  762. $objWriter->endElement();
  763. $objWriter->startElement('c:multiLvlStrCache');
  764. $objWriter->startElement('c:ptCount');
  765. $objWriter->writeAttribute('val', $plotSeriesValues->getPointCount() );
  766. $objWriter->endElement();
  767. for ($level = 0; $level < $levelCount; ++$level) {
  768. $objWriter->startElement('c:lvl');
  769. foreach($plotSeriesValues->getDataValues() as $plotSeriesKey => $plotSeriesValue) {
  770. if (isset($plotSeriesValue[$level])) {
  771. $objWriter->startElement('c:pt');
  772. $objWriter->writeAttribute('idx', $plotSeriesKey );
  773. $objWriter->startElement('c:v');
  774. $objWriter->writeRawData( $plotSeriesValue[$level] );
  775. $objWriter->endElement();
  776. $objWriter->endElement();
  777. }
  778. }
  779. $objWriter->endElement();
  780. }
  781. $objWriter->endElement();
  782. $objWriter->endElement();
  783. } else {
  784. $objWriter->startElement('c:'.$dataType.'Ref');
  785. $objWriter->startElement('c:f');
  786. $objWriter->writeRawData( $plotSeriesValues->getDataSource() );
  787. $objWriter->endElement();
  788. $objWriter->startElement('c:'.$dataType.'Cache');
  789. if (($groupType != PHPExcel_Chart_DataSeries::TYPE_PIECHART) &&
  790. ($groupType != PHPExcel_Chart_DataSeries::TYPE_PIECHART_3D) &&
  791. ($groupType != PHPExcel_Chart_DataSeries::TYPE_DONUTCHART)) {
  792. if (($plotSeriesValues->getFormatCode() !== NULL) &&
  793. ($plotSeriesValues->getFormatCode() !== '')) {
  794. $objWriter->startElement('c:formatCode');
  795. $objWriter->writeRawData( $plotSeriesValues->getFormatCode() );
  796. $objWriter->endElement();
  797. }
  798. }
  799. $objWriter->startElement('c:ptCount');
  800. $objWriter->writeAttribute('val', $plotSeriesValues->getPointCount() );
  801. $objWriter->endElement();
  802. $dataValues = $plotSeriesValues->getDataValues();
  803. if (!empty($dataValues)) {
  804. if (is_array($dataValues)) {
  805. foreach($dataValues as $plotSeriesKey => $plotSeriesValue) {
  806. $objWriter->startElement('c:pt');
  807. $objWriter->writeAttribute('idx', $plotSeriesKey );
  808. $objWriter->startElement('c:v');
  809. $objWriter->writeRawData( $plotSeriesValue );
  810. $objWriter->endElement();
  811. $objWriter->endElement();
  812. }
  813. }
  814. }
  815. $objWriter->endElement();
  816. $objWriter->endElement();
  817. }
  818. }
  819. /**
  820. * Write Bubble Chart Details
  821. *
  822. * @param PHPExcel_Chart_DataSeriesValues $plotSeriesValues
  823. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  824. * @throws PHPExcel_Writer_Exception
  825. */
  826. private function _writeBubbles($plotSeriesValues, $objWriter, PHPExcel_Worksheet $pSheet)
  827. {
  828. if (is_null($plotSeriesValues)) {
  829. return;
  830. }
  831. $objWriter->startElement('c:bubbleSize');
  832. $objWriter->startElement('c:numLit');
  833. $objWriter->startElement('c:formatCode');
  834. $objWriter->writeRawData( 'General' );
  835. $objWriter->endElement();
  836. $objWriter->startElement('c:ptCount');
  837. $objWriter->writeAttribute('val', $plotSeriesValues->getPointCount() );
  838. $objWriter->endElement();
  839. $dataValues = $plotSeriesValues->getDataValues();
  840. if (!empty($dataValues)) {
  841. if (is_array($dataValues)) {
  842. foreach($dataValues as $plotSeriesKey => $plotSeriesValue) {
  843. $objWriter->startElement('c:pt');
  844. $objWriter->writeAttribute('idx', $plotSeriesKey );
  845. $objWriter->startElement('c:v');
  846. $objWriter->writeRawData( 1 );
  847. $objWriter->endElement();
  848. $objWriter->endElement();
  849. }
  850. }
  851. }
  852. $objWriter->endElement();
  853. $objWriter->endElement();
  854. $objWriter->startElement('c:bubble3D');
  855. $objWriter->writeAttribute('val', 0 );
  856. $objWriter->endElement();
  857. }
  858. /**
  859. * Write Layout
  860. *
  861. * @param PHPExcel_Chart_Layout $layout
  862. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  863. * @throws PHPExcel_Writer_Exception
  864. */
  865. private function _writeLayout(PHPExcel_Chart_Layout $layout = NULL, $objWriter)
  866. {
  867. $objWriter->startElement('c:layout');
  868. if (!is_null($layout)) {
  869. $objWriter->startElement('c:manualLayout');
  870. $layoutTarget = $layout->getLayoutTarget();
  871. if (!is_null($layoutTarget)) {
  872. $objWriter->startElement('c:layoutTarget');
  873. $objWriter->writeAttribute('val', $layoutTarget);
  874. $objWriter->endElement();
  875. }
  876. $xMode = $layout->getXMode();
  877. if (!is_null($xMode)) {
  878. $objWriter->startElement('c:xMode');
  879. $objWriter->writeAttribute('val', $xMode);
  880. $objWriter->endElement();
  881. }
  882. $yMode = $layout->getYMode();
  883. if (!is_null($yMode)) {
  884. $objWriter->startElement('c:yMode');
  885. $objWriter->writeAttribute('val', $yMode);
  886. $objWriter->endElement();
  887. }
  888. $x = $layout->getXPosition();
  889. if (!is_null($x)) {
  890. $objWriter->startElement('c:x');
  891. $objWriter->writeAttribute('val', $x);
  892. $objWriter->endElement();
  893. }
  894. $y = $layout->getYPosition();
  895. if (!is_null($y)) {
  896. $objWriter->startElement('c:y');
  897. $objWriter->writeAttribute('val', $y);
  898. $objWriter->endElement();
  899. }
  900. $w = $layout->getWidth();
  901. if (!is_null($w)) {
  902. $objWriter->startElement('c:w');
  903. $objWriter->writeAttribute('val', $w);
  904. $objWriter->endElement();
  905. }
  906. $h = $layout->getHeight();
  907. if (!is_null($h)) {
  908. $objWriter->startElement('c:h');
  909. $objWriter->writeAttribute('val', $h);
  910. $objWriter->endElement();
  911. }
  912. $objWriter->endElement();
  913. }
  914. $objWriter->endElement();
  915. }
  916. /**
  917. * Write Alternate Content block
  918. *
  919. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  920. * @throws PHPExcel_Writer_Exception
  921. */
  922. private function _writeAlternateContent($objWriter)
  923. {
  924. $objWriter->startElement('mc:AlternateContent');
  925. $objWriter->writeAttribute('xmlns:mc', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
  926. $objWriter->startElement('mc:Choice');
  927. $objWriter->writeAttribute('xmlns:c14', 'http://schemas.microsoft.com/office/drawing/2007/8/2/chart');
  928. $objWriter->writeAttribute('Requires', 'c14');
  929. $objWriter->startElement('c14:style');
  930. $objWriter->writeAttribute('val', '102');
  931. $objWriter->endElement();
  932. $objWriter->endElement();
  933. $objWriter->startElement('mc:Fallback');
  934. $objWriter->startElement('c:style');
  935. $objWriter->writeAttribute('val', '2');
  936. $objWriter->endElement();
  937. $objWriter->endElement();
  938. $objWriter->endElement();
  939. }
  940. /**
  941. * Write Printer Settings
  942. *
  943. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  944. * @throws PHPExcel_Writer_Exception
  945. */
  946. private function _writePrintSettings($objWriter)
  947. {
  948. $objWriter->startElement('c:printSettings');
  949. $objWriter->startElement('c:headerFooter');
  950. $objWriter->endElement();
  951. $objWriter->startElement('c:pageMargins');
  952. $objWriter->writeAttribute('footer', 0.3);
  953. $objWriter->writeAttribute('header', 0.3);
  954. $objWriter->writeAttribute('r', 0.7);
  955. $objWriter->writeAttribute('l', 0.7);
  956. $objWriter->writeAttribute('t', 0.75);
  957. $objWriter->writeAttribute('b', 0.75);
  958. $objWriter->endElement();
  959. $objWriter->startElement('c:pageSetup');
  960. $objWriter->writeAttribute('orientation', "portrait");
  961. $objWriter->endElement();
  962. $objWriter->endElement();
  963. }
  964. }