PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/application/third_party/PHPExcel/Reader/Excel2007/Chart.php

https://bitbucket.org/matyhaty/senses-designertravelv3
PHP | 513 lines | 436 code | 44 blank | 33 comment | 60 complexity | 542c699d801cdda595f05b5421799b89 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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_Reader_Excel5
  23. * @copyright Copyright (c) 2006 - 2012 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. /**
  28. * PHPExcel_Reader_Excel2007_Chart
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Reader_Excel2007
  32. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Reader_Excel2007_Chart
  35. {
  36. private static function _getAttribute($component, $name, $format) {
  37. $attributes = $component->attributes();
  38. if (isset($attributes[$name])) {
  39. if ($format == 'string') {
  40. return (string) $attributes[$name];
  41. } elseif ($format == 'integer') {
  42. return (integer) $attributes[$name];
  43. } elseif ($format == 'boolean') {
  44. return (boolean) ($attributes[$name] == '0') ? false : true;
  45. } else {
  46. return (float) $attributes[$name];
  47. }
  48. }
  49. return null;
  50. } // function _getAttribute()
  51. private static function _readColor($color,$background=false) {
  52. if (isset($color["rgb"])) {
  53. return (string)$color["rgb"];
  54. } else if (isset($color["indexed"])) {
  55. return PHPExcel_Style_Color::indexedColor($color["indexed"]-7,$background)->getARGB();
  56. }
  57. }
  58. public static function readChart($chartElements,$chartName) {
  59. $namespacesChartMeta = $chartElements->getNamespaces(true);
  60. $chartElementsC = $chartElements->children($namespacesChartMeta['c']);
  61. $XaxisLabel = $YaxisLabel = $legend = $title = NULL;
  62. $dispBlanksAs = $plotVisOnly = NULL;
  63. foreach($chartElementsC as $chartElementKey => $chartElement) {
  64. switch ($chartElementKey) {
  65. case "chart":
  66. foreach($chartElement as $chartDetailsKey => $chartDetails) {
  67. $chartDetailsC = $chartDetails->children($namespacesChartMeta['c']);
  68. switch ($chartDetailsKey) {
  69. case "plotArea":
  70. $plotAreaLayout = $XaxisLable = $YaxisLable = null;
  71. $plotSeries = $plotAttributes = array();
  72. foreach($chartDetails as $chartDetailKey => $chartDetail) {
  73. switch ($chartDetailKey) {
  74. case "layout":
  75. $plotAreaLayout = self::_chartLayoutDetails($chartDetail,$namespacesChartMeta,'plotArea');
  76. break;
  77. case "catAx":
  78. if (isset($chartDetail->title)) {
  79. $XaxisLabel = self::_chartTitle($chartDetail->title->children($namespacesChartMeta['c']),$namespacesChartMeta,'cat');
  80. }
  81. break;
  82. case "dateAx":
  83. if (isset($chartDetail->title)) {
  84. $XaxisLabel = self::_chartTitle($chartDetail->title->children($namespacesChartMeta['c']),$namespacesChartMeta,'cat');
  85. }
  86. break;
  87. case "valAx":
  88. if (isset($chartDetail->title)) {
  89. $YaxisLabel = self::_chartTitle($chartDetail->title->children($namespacesChartMeta['c']),$namespacesChartMeta,'cat');
  90. }
  91. break;
  92. case "barChart":
  93. case "bar3DChart":
  94. $barDirection = self::_getAttribute($chartDetail->barDir, 'val', 'string');
  95. $plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  96. $plotSer->setPlotDirection($barDirection);
  97. $plotSeries[] = $plotSer;
  98. $plotAttributes = self::_readChartAttributes($chartDetail);
  99. break;
  100. case "lineChart":
  101. case "line3DChart":
  102. $plotSeries[] = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  103. $plotAttributes = self::_readChartAttributes($chartDetail);
  104. break;
  105. case "areaChart":
  106. case "area3DChart":
  107. $plotSeries[] = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  108. $plotAttributes = self::_readChartAttributes($chartDetail);
  109. break;
  110. case "doughnutChart":
  111. case "pieChart":
  112. case "pie3DChart":
  113. $explosion = isset($chartDetail->ser->explosion);
  114. $plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  115. $plotSer->setPlotStyle($explosion);
  116. $plotSeries[] = $plotSer;
  117. $plotAttributes = self::_readChartAttributes($chartDetail);
  118. break;
  119. case "scatterChart":
  120. $scatterStyle = self::_getAttribute($chartDetail->scatterStyle, 'val', 'string');
  121. $plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  122. $plotSer->setPlotStyle($scatterStyle);
  123. $plotSeries[] = $plotSer;
  124. $plotAttributes = self::_readChartAttributes($chartDetail);
  125. break;
  126. case "bubbleChart":
  127. $bubbleScale = self::_getAttribute($chartDetail->bubbleScale, 'val', 'integer');
  128. $plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  129. $plotSer->setPlotStyle($bubbleScale);
  130. $plotSeries[] = $plotSer;
  131. $plotAttributes = self::_readChartAttributes($chartDetail);
  132. break;
  133. case "radarChart":
  134. $radarStyle = self::_getAttribute($chartDetail->radarStyle, 'val', 'string');
  135. $plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  136. $plotSer->setPlotStyle($radarStyle);
  137. $plotSeries[] = $plotSer;
  138. $plotAttributes = self::_readChartAttributes($chartDetail);
  139. break;
  140. case "surfaceChart":
  141. case "surface3DChart":
  142. $wireFrame = self::_getAttribute($chartDetail->wireframe, 'val', 'boolean');
  143. $plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  144. $plotSer->setPlotStyle($wireFrame);
  145. $plotSeries[] = $plotSer;
  146. $plotAttributes = self::_readChartAttributes($chartDetail);
  147. break;
  148. case "stockChart":
  149. $plotSeries[] = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  150. $plotAttributes = self::_readChartAttributes($plotAreaLayout);
  151. break;
  152. }
  153. }
  154. if ($plotAreaLayout == NULL) {
  155. $plotAreaLayout = new PHPExcel_Chart_Layout();
  156. }
  157. $plotArea = new PHPExcel_Chart_PlotArea($plotAreaLayout,$plotSeries);
  158. self::_setChartAttributes($plotAreaLayout,$plotAttributes);
  159. break;
  160. case "plotVisOnly":
  161. $plotVisOnly = self::_getAttribute($chartDetails, 'val', 'string');
  162. break;
  163. case "dispBlanksAs":
  164. $dispBlanksAs = self::_getAttribute($chartDetails, 'val', 'string');
  165. break;
  166. case "title":
  167. $title = self::_chartTitle($chartDetails,$namespacesChartMeta,'title');
  168. break;
  169. case "legend":
  170. $legendPos = 'r';
  171. $legendLayout = null;
  172. $legendOverlay = false;
  173. foreach($chartDetails as $chartDetailKey => $chartDetail) {
  174. switch ($chartDetailKey) {
  175. case "legendPos":
  176. $legendPos = self::_getAttribute($chartDetail, 'val', 'string');
  177. break;
  178. case "overlay":
  179. $legendOverlay = self::_getAttribute($chartDetail, 'val', 'boolean');
  180. break;
  181. case "layout":
  182. $legendLayout = self::_chartLayoutDetails($chartDetail,$namespacesChartMeta,'legend');
  183. break;
  184. }
  185. }
  186. $legend = new PHPExcel_Chart_Legend($legendPos, $legendLayout, $legendOverlay);
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. $chart = new PHPExcel_Chart($chartName,$title,$legend,$plotArea,$plotVisOnly,$dispBlanksAs,$XaxisLabel,$YaxisLabel);
  193. return $chart;
  194. } // function readChart()
  195. private static function _chartTitle($titleDetails,$namespacesChartMeta,$type) {
  196. $caption = array();
  197. $titleLayout = null;
  198. foreach($titleDetails as $titleDetailKey => $chartDetail) {
  199. switch ($titleDetailKey) {
  200. case "tx":
  201. $titleDetails = $chartDetail->rich->children($namespacesChartMeta['a']);
  202. foreach($titleDetails as $titleKey => $titleDetail) {
  203. switch ($titleKey) {
  204. case "p":
  205. $titleDetailPart = $titleDetail->children($namespacesChartMeta['a']);
  206. $caption[] = self::_parseRichText($titleDetailPart);
  207. }
  208. }
  209. break;
  210. case "layout":
  211. $titleLayout = self::_chartLayoutDetails($chartDetail,$namespacesChartMeta);
  212. break;
  213. }
  214. }
  215. return new PHPExcel_Chart_Title($caption, $titleLayout);
  216. } // function _chartTitle()
  217. private static function _chartLayoutDetails($chartDetail,$namespacesChartMeta) {
  218. if (!isset($chartDetail->manualLayout)) {
  219. return null;
  220. }
  221. $details = $chartDetail->manualLayout->children($namespacesChartMeta['c']);
  222. if (is_null($details)) {
  223. return null;
  224. }
  225. $layout = array();
  226. foreach($details as $detailKey => $detail) {
  227. // echo $detailKey,' => ',self::_getAttribute($detail, 'val', 'string'),PHP_EOL;
  228. $layout[$detailKey] = self::_getAttribute($detail, 'val', 'string');
  229. }
  230. return new PHPExcel_Chart_Layout($layout);
  231. } // function _chartLayoutDetails()
  232. private static function _chartDataSeries($chartDetail,$namespacesChartMeta,$plotType) {
  233. $multiSeriesType = NULL;
  234. $smoothLine = false;
  235. $seriesLabel = $seriesCategory = $seriesValues = $plotOrder = array();
  236. $seriesDetailSet = $chartDetail->children($namespacesChartMeta['c']);
  237. foreach($seriesDetailSet as $seriesDetailKey => $seriesDetails) {
  238. switch ($seriesDetailKey) {
  239. case "grouping":
  240. $multiSeriesType = self::_getAttribute($chartDetail->grouping, 'val', 'string');
  241. break;
  242. case "ser":
  243. $marker = NULL;
  244. foreach($seriesDetails as $seriesKey => $seriesDetail) {
  245. switch ($seriesKey) {
  246. case "idx":
  247. $seriesIndex = self::_getAttribute($seriesDetail, 'val', 'integer');
  248. break;
  249. case "order":
  250. $seriesOrder = self::_getAttribute($seriesDetail, 'val', 'integer');
  251. $plotOrder[$seriesIndex] = $seriesOrder;
  252. break;
  253. case "tx":
  254. $seriesLabel[$seriesIndex] = self::_chartDataSeriesValueSet($seriesDetail,$namespacesChartMeta);
  255. break;
  256. case "marker":
  257. $marker = self::_getAttribute($seriesDetail->symbol, 'val', 'string');
  258. break;
  259. case "smooth":
  260. $smoothLine = self::_getAttribute($seriesDetail, 'val', 'boolean');
  261. break;
  262. case "cat":
  263. $seriesCategory[$seriesIndex] = self::_chartDataSeriesValueSet($seriesDetail,$namespacesChartMeta);
  264. break;
  265. case "val":
  266. $seriesValues[$seriesIndex] = self::_chartDataSeriesValueSet($seriesDetail,$namespacesChartMeta,$marker);
  267. break;
  268. case "xVal":
  269. $seriesCategory[$seriesIndex] = self::_chartDataSeriesValueSet($seriesDetail,$namespacesChartMeta,$marker);
  270. break;
  271. case "yVal":
  272. $seriesValues[$seriesIndex] = self::_chartDataSeriesValueSet($seriesDetail,$namespacesChartMeta,$marker);
  273. break;
  274. }
  275. }
  276. }
  277. }
  278. return new PHPExcel_Chart_DataSeries($plotType,$multiSeriesType,$plotOrder,$seriesLabel,$seriesCategory,$seriesValues,$smoothLine);
  279. } // function _chartDataSeries()
  280. private static function _chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker = null, $smoothLine = false) {
  281. if (isset($seriesDetail->strRef)) {
  282. $seriesSource = (string) $seriesDetail->strRef->f;
  283. $seriesData = self::_chartDataSeriesValues($seriesDetail->strRef->strCache->children($namespacesChartMeta['c']),'s');
  284. return new PHPExcel_Chart_DataSeriesValues('String',$seriesSource,$seriesData['formatCode'],$seriesData['pointCount'],$seriesData['dataValues'],$marker,$smoothLine);
  285. } elseif (isset($seriesDetail->numRef)) {
  286. $seriesSource = (string) $seriesDetail->numRef->f;
  287. $seriesData = self::_chartDataSeriesValues($seriesDetail->numRef->numCache->children($namespacesChartMeta['c']));
  288. return new PHPExcel_Chart_DataSeriesValues('Number',$seriesSource,$seriesData['formatCode'],$seriesData['pointCount'],$seriesData['dataValues'],$marker,$smoothLine);
  289. } elseif (isset($seriesDetail->multiLvlStrRef)) {
  290. $seriesSource = (string) $seriesDetail->multiLvlStrRef->f;
  291. $seriesData = self::_chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlStrRef->multiLvlStrCache->children($namespacesChartMeta['c']),'s');
  292. $seriesData['pointCount'] = count($seriesData['dataValues']);
  293. return new PHPExcel_Chart_DataSeriesValues('String',$seriesSource,$seriesData['formatCode'],$seriesData['pointCount'],$seriesData['dataValues'],$marker,$smoothLine);
  294. } elseif (isset($seriesDetail->multiLvlNumRef)) {
  295. $seriesSource = (string) $seriesDetail->multiLvlNumRef->f;
  296. $seriesData = self::_chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlNumRef->multiLvlNumCache->children($namespacesChartMeta['c']),'s');
  297. $seriesData['pointCount'] = count($seriesData['dataValues']);
  298. return new PHPExcel_Chart_DataSeriesValues('String',$seriesSource,$seriesData['formatCode'],$seriesData['pointCount'],$seriesData['dataValues'],$marker,$smoothLine);
  299. }
  300. return null;
  301. } // function _chartDataSeriesValueSet()
  302. private static function _chartDataSeriesValues($seriesValueSet,$dataType='n') {
  303. $seriesVal = array();
  304. $formatCode = '';
  305. $pointCount = 0;
  306. foreach($seriesValueSet as $seriesValueIdx => $seriesValue) {
  307. switch ($seriesValueIdx) {
  308. case 'ptCount':
  309. $pointCount = self::_getAttribute($seriesValue, 'val', 'integer');
  310. break;
  311. case 'formatCode':
  312. $formatCode = (string) $seriesValue;
  313. break;
  314. case 'pt':
  315. $pointVal = self::_getAttribute($seriesValue, 'idx', 'integer');
  316. if ($dataType == 's') {
  317. $seriesVal[$pointVal] = (string) $seriesValue->v;
  318. } else {
  319. $seriesVal[$pointVal] = (float) $seriesValue->v;
  320. }
  321. break;
  322. }
  323. }
  324. return array( 'formatCode' => $formatCode,
  325. 'pointCount' => $pointCount,
  326. 'dataValues' => $seriesVal
  327. );
  328. } // function _chartDataSeriesValues()
  329. private static function _chartDataSeriesValuesMultiLevel($seriesValueSet,$dataType='n') {
  330. $seriesVal = array();
  331. $formatCode = '';
  332. $pointCount = 0;
  333. foreach($seriesValueSet->lvl as $seriesLevelIdx => $seriesLevel) {
  334. foreach($seriesLevel as $seriesValueIdx => $seriesValue) {
  335. switch ($seriesValueIdx) {
  336. case 'ptCount':
  337. $pointCount = self::_getAttribute($seriesValue, 'val', 'integer');
  338. break;
  339. case 'formatCode':
  340. $formatCode = (string) $seriesValue;
  341. break;
  342. case 'pt':
  343. $pointVal = self::_getAttribute($seriesValue, 'idx', 'integer');
  344. if ($dataType == 's') {
  345. $seriesVal[$pointVal][] = (string) $seriesValue->v;
  346. } else {
  347. $seriesVal[$pointVal][] = (float) $seriesValue->v;
  348. }
  349. break;
  350. }
  351. }
  352. }
  353. return array( 'formatCode' => $formatCode,
  354. 'pointCount' => $pointCount,
  355. 'dataValues' => $seriesVal
  356. );
  357. } // function _chartDataSeriesValuesMultiLevel()
  358. private static function _parseRichText($titleDetailPart = null) {
  359. $value = new PHPExcel_RichText();
  360. foreach($titleDetailPart as $titleDetailElementKey => $titleDetailElement) {
  361. if (isset($titleDetailElement->t)) {
  362. $objText = $value->createTextRun( (string) $titleDetailElement->t );
  363. }
  364. if (isset($titleDetailElement->rPr)) {
  365. if (isset($titleDetailElement->rPr->rFont["val"])) {
  366. $objText->getFont()->setName((string) $titleDetailElement->rPr->rFont["val"]);
  367. }
  368. $fontSize = (self::_getAttribute($titleDetailElement->rPr, 'sz', 'integer'));
  369. if (!is_null($fontSize)) {
  370. $objText->getFont()->setSize(floor($fontSize / 100));
  371. }
  372. $fontColor = (self::_getAttribute($titleDetailElement->rPr, 'color', 'string'));
  373. if (!is_null($fontColor)) {
  374. $objText->getFont()->setColor( new PHPExcel_Style_Color( self::_readColor($fontColor) ) );
  375. }
  376. $bold = self::_getAttribute($titleDetailElement->rPr, 'b', 'boolean');
  377. if (!is_null($bold)) {
  378. $objText->getFont()->setBold($bold);
  379. }
  380. $italic = self::_getAttribute($titleDetailElement->rPr, 'i', 'boolean');
  381. if (!is_null($italic)) {
  382. $objText->getFont()->setItalic($italic);
  383. }
  384. $baseline = self::_getAttribute($titleDetailElement->rPr, 'baseline', 'integer');
  385. if (!is_null($baseline)) {
  386. if ($baseline > 0) {
  387. $objText->getFont()->setSuperScript(true);
  388. } elseif($baseline < 0) {
  389. $objText->getFont()->setSubScript(true);
  390. }
  391. }
  392. $underscore = (self::_getAttribute($titleDetailElement->rPr, 'u', 'string'));
  393. if (!is_null($underscore)) {
  394. if ($underscore == 'sng') {
  395. $objText->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  396. } elseif($underscore == 'dbl') {
  397. $objText->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLE);
  398. } else {
  399. $objText->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_NONE);
  400. }
  401. }
  402. $strikethrough = (self::_getAttribute($titleDetailElement->rPr, 's', 'string'));
  403. if (!is_null($strikethrough)) {
  404. if ($strikethrough == 'noStrike') {
  405. $objText->getFont()->setStrikethrough(false);
  406. } else {
  407. $objText->getFont()->setStrikethrough(true);
  408. }
  409. }
  410. }
  411. }
  412. return $value;
  413. }
  414. private static function _readChartAttributes($chartDetail) {
  415. $plotAttributes = array();
  416. if (isset($chartDetail->dLbls)) {
  417. if (isset($chartDetail->dLbls->howLegendKey)) {
  418. $plotAttributes['showLegendKey'] = self::_getAttribute($chartDetail->dLbls->showLegendKey, 'val', 'string');
  419. }
  420. if (isset($chartDetail->dLbls->showVal)) {
  421. $plotAttributes['showVal'] = self::_getAttribute($chartDetail->dLbls->showVal, 'val', 'string');
  422. }
  423. if (isset($chartDetail->dLbls->showCatName)) {
  424. $plotAttributes['showCatName'] = self::_getAttribute($chartDetail->dLbls->showCatName, 'val', 'string');
  425. }
  426. if (isset($chartDetail->dLbls->showSerName)) {
  427. $plotAttributes['showSerName'] = self::_getAttribute($chartDetail->dLbls->showSerName, 'val', 'string');
  428. }
  429. if (isset($chartDetail->dLbls->showPercent)) {
  430. $plotAttributes['showPercent'] = self::_getAttribute($chartDetail->dLbls->showPercent, 'val', 'string');
  431. }
  432. if (isset($chartDetail->dLbls->showBubbleSize)) {
  433. $plotAttributes['showBubbleSize'] = self::_getAttribute($chartDetail->dLbls->showBubbleSize, 'val', 'string');
  434. }
  435. if (isset($chartDetail->dLbls->showLeaderLines)) {
  436. $plotAttributes['showLeaderLines'] = self::_getAttribute($chartDetail->dLbls->showLeaderLines, 'val', 'string');
  437. }
  438. }
  439. return $plotAttributes;
  440. }
  441. private static function _setChartAttributes($plotArea,$plotAttributes)
  442. {
  443. foreach($plotAttributes as $plotAttributeKey => $plotAttributeValue) {
  444. switch($plotAttributeKey) {
  445. case 'showLegendKey' :
  446. $plotArea->setShowLegendKey($plotAttributeValue);
  447. break;
  448. case 'showVal' :
  449. $plotArea->setShowVal($plotAttributeValue);
  450. break;
  451. case 'showCatName' :
  452. $plotArea->setShowCatName($plotAttributeValue);
  453. break;
  454. case 'showSerName' :
  455. $plotArea->setShowSerName($plotAttributeValue);
  456. break;
  457. case 'showPercent' :
  458. $plotArea->setShowPercent($plotAttributeValue);
  459. break;
  460. case 'showBubbleSize' :
  461. $plotArea->setShowBubbleSize($plotAttributeValue);
  462. break;
  463. case 'showLeaderLines' :
  464. $plotArea->setShowLeaderLines($plotAttributeValue);
  465. break;
  466. }
  467. }
  468. }
  469. }