PageRenderTime 70ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/PHPExcel/Reader/Excel2007/Chart.php

https://github.com/yuweijun/blog
PHP | 524 lines | 452 code | 39 blank | 33 comment | 60 complexity | 213c70ebfe7e8ee01ee78578f0fd1552 MD5 | raw file
  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. if (empty($seriesVal)) {
  333. $seriesVal = null;
  334. }
  335. return array(
  336. 'formatCode' => $formatCode,
  337. 'pointCount' => $pointCount,
  338. 'dataValues' => $seriesVal
  339. );
  340. }
  341. private static function chartDataSeriesValuesMultiLevel($seriesValueSet, $dataType = 'n')
  342. {
  343. $seriesVal = array();
  344. $formatCode = '';
  345. $pointCount = 0;
  346. foreach ($seriesValueSet->lvl as $seriesLevelIdx => $seriesLevel) {
  347. foreach ($seriesLevel as $seriesValueIdx => $seriesValue) {
  348. switch ($seriesValueIdx) {
  349. case 'ptCount':
  350. $pointCount = self::getAttribute($seriesValue, 'val', 'integer');
  351. break;
  352. case 'formatCode':
  353. $formatCode = (string) $seriesValue;
  354. break;
  355. case 'pt':
  356. $pointVal = self::getAttribute($seriesValue, 'idx', 'integer');
  357. if ($dataType == 's') {
  358. $seriesVal[$pointVal][] = (string) $seriesValue->v;
  359. } else {
  360. $seriesVal[$pointVal][] = (float) $seriesValue->v;
  361. }
  362. break;
  363. }
  364. }
  365. }
  366. return array(
  367. 'formatCode' => $formatCode,
  368. 'pointCount' => $pointCount,
  369. 'dataValues' => $seriesVal
  370. );
  371. }
  372. private static function parseRichText($titleDetailPart = null)
  373. {
  374. $value = new PHPExcel_RichText();
  375. foreach ($titleDetailPart as $titleDetailElementKey => $titleDetailElement) {
  376. if (isset($titleDetailElement->t)) {
  377. $objText = $value->createTextRun((string) $titleDetailElement->t);
  378. }
  379. if (isset($titleDetailElement->rPr)) {
  380. if (isset($titleDetailElement->rPr->rFont["val"])) {
  381. $objText->getFont()->setName((string) $titleDetailElement->rPr->rFont["val"]);
  382. }
  383. $fontSize = (self::getAttribute($titleDetailElement->rPr, 'sz', 'integer'));
  384. if (!is_null($fontSize)) {
  385. $objText->getFont()->setSize(floor($fontSize / 100));
  386. }
  387. $fontColor = (self::getAttribute($titleDetailElement->rPr, 'color', 'string'));
  388. if (!is_null($fontColor)) {
  389. $objText->getFont()->setColor(new PHPExcel_Style_Color(self::readColor($fontColor)));
  390. }
  391. $bold = self::getAttribute($titleDetailElement->rPr, 'b', 'boolean');
  392. if (!is_null($bold)) {
  393. $objText->getFont()->setBold($bold);
  394. }
  395. $italic = self::getAttribute($titleDetailElement->rPr, 'i', 'boolean');
  396. if (!is_null($italic)) {
  397. $objText->getFont()->setItalic($italic);
  398. }
  399. $baseline = self::getAttribute($titleDetailElement->rPr, 'baseline', 'integer');
  400. if (!is_null($baseline)) {
  401. if ($baseline > 0) {
  402. $objText->getFont()->setSuperScript(true);
  403. } elseif ($baseline < 0) {
  404. $objText->getFont()->setSubScript(true);
  405. }
  406. }
  407. $underscore = (self::getAttribute($titleDetailElement->rPr, 'u', 'string'));
  408. if (!is_null($underscore)) {
  409. if ($underscore == 'sng') {
  410. $objText->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  411. } elseif ($underscore == 'dbl') {
  412. $objText->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLE);
  413. } else {
  414. $objText->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_NONE);
  415. }
  416. }
  417. $strikethrough = (self::getAttribute($titleDetailElement->rPr, 's', 'string'));
  418. if (!is_null($strikethrough)) {
  419. if ($strikethrough == 'noStrike') {
  420. $objText->getFont()->setStrikethrough(false);
  421. } else {
  422. $objText->getFont()->setStrikethrough(true);
  423. }
  424. }
  425. }
  426. }
  427. return $value;
  428. }
  429. private static function readChartAttributes($chartDetail)
  430. {
  431. $plotAttributes = array();
  432. if (isset($chartDetail->dLbls)) {
  433. if (isset($chartDetail->dLbls->howLegendKey)) {
  434. $plotAttributes['showLegendKey'] = self::getAttribute($chartDetail->dLbls->showLegendKey, 'val', 'string');
  435. }
  436. if (isset($chartDetail->dLbls->showVal)) {
  437. $plotAttributes['showVal'] = self::getAttribute($chartDetail->dLbls->showVal, 'val', 'string');
  438. }
  439. if (isset($chartDetail->dLbls->showCatName)) {
  440. $plotAttributes['showCatName'] = self::getAttribute($chartDetail->dLbls->showCatName, 'val', 'string');
  441. }
  442. if (isset($chartDetail->dLbls->showSerName)) {
  443. $plotAttributes['showSerName'] = self::getAttribute($chartDetail->dLbls->showSerName, 'val', 'string');
  444. }
  445. if (isset($chartDetail->dLbls->showPercent)) {
  446. $plotAttributes['showPercent'] = self::getAttribute($chartDetail->dLbls->showPercent, 'val', 'string');
  447. }
  448. if (isset($chartDetail->dLbls->showBubbleSize)) {
  449. $plotAttributes['showBubbleSize'] = self::getAttribute($chartDetail->dLbls->showBubbleSize, 'val', 'string');
  450. }
  451. if (isset($chartDetail->dLbls->showLeaderLines)) {
  452. $plotAttributes['showLeaderLines'] = self::getAttribute($chartDetail->dLbls->showLeaderLines, 'val', 'string');
  453. }
  454. }
  455. return $plotAttributes;
  456. }
  457. private static function setChartAttributes($plotArea, $plotAttributes)
  458. {
  459. foreach ($plotAttributes as $plotAttributeKey => $plotAttributeValue) {
  460. switch ($plotAttributeKey) {
  461. case 'showLegendKey':
  462. $plotArea->setShowLegendKey($plotAttributeValue);
  463. break;
  464. case 'showVal':
  465. $plotArea->setShowVal($plotAttributeValue);
  466. break;
  467. case 'showCatName':
  468. $plotArea->setShowCatName($plotAttributeValue);
  469. break;
  470. case 'showSerName':
  471. $plotArea->setShowSerName($plotAttributeValue);
  472. break;
  473. case 'showPercent':
  474. $plotArea->setShowPercent($plotAttributeValue);
  475. break;
  476. case 'showBubbleSize':
  477. $plotArea->setShowBubbleSize($plotAttributeValue);
  478. break;
  479. case 'showLeaderLines':
  480. $plotArea->setShowLeaderLines($plotAttributeValue);
  481. break;
  482. }
  483. }
  484. }
  485. }