For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

520 lines
26 KiB

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