PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/include/PHPExcel/Reader/Excel2007/Chart.php

https://bitbucket.org/sleininger/stock_online
PHP | 441 lines | 367 code | 41 blank | 33 comment | 50 complexity | de07096d02a27ced686a5768772bf714 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.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 1.7.7, 2012-05-19
  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. foreach($chartElementsC as $chartElementKey => $chartElement) {
  63. switch ($chartElementKey) {
  64. case "chart":
  65. foreach($chartElement as $chartDetailsKey => $chartDetails) {
  66. $chartDetailsC = $chartDetails->children($namespacesChartMeta['c']);
  67. switch ($chartDetailsKey) {
  68. case "plotArea":
  69. $plotAreaLayout = $XaxisLable = $YaxisLable = null;
  70. $plotSeries = array();
  71. foreach($chartDetails as $chartDetailKey => $chartDetail) {
  72. switch ($chartDetailKey) {
  73. case "layout":
  74. $plotAreaLayout = self::_chartLayoutDetails($chartDetail,$namespacesChartMeta,'plotArea');
  75. break;
  76. case "catAx":
  77. if (isset($chartDetail->title)) {
  78. $XaxisLabel = self::_chartTitle($chartDetail->title->children($namespacesChartMeta['c']),$namespacesChartMeta,'cat');
  79. }
  80. break;
  81. case "dateAx":
  82. if (isset($chartDetail->title)) {
  83. $XaxisLabel = self::_chartTitle($chartDetail->title->children($namespacesChartMeta['c']),$namespacesChartMeta,'cat');
  84. }
  85. break;
  86. case "valAx":
  87. if (isset($chartDetail->title)) {
  88. $YaxisLabel = self::_chartTitle($chartDetail->title->children($namespacesChartMeta['c']),$namespacesChartMeta,'cat');
  89. }
  90. break;
  91. case "barChart":
  92. case "bar3DChart":
  93. $barDirection = self::_getAttribute($chartDetail->barDir, 'val', 'string');
  94. $plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  95. $plotSer->setPlotDirection($barDirection);
  96. $plotSeries[] = $plotSer;
  97. break;
  98. case "lineChart":
  99. case "line3DChart":
  100. $plotSeries[] = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  101. break;
  102. case "areaChart":
  103. case "area3DChart":
  104. $plotSeries[] = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  105. break;
  106. case "doughnutChart":
  107. case "pieChart":
  108. case "pie3DChart":
  109. $explosion = isset($chartDetail->ser->explosion);
  110. $plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  111. $plotSer->setPlotStyle($explosion);
  112. $plotSeries[] = $plotSer;
  113. break;
  114. case "scatterChart":
  115. $scatterStyle = self::_getAttribute($chartDetail->scatterStyle, 'val', 'string');
  116. $plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  117. $plotSer->setPlotStyle($scatterStyle);
  118. $plotSeries[] = $plotSer;
  119. break;
  120. case "bubbleChart":
  121. $bubbleScale = self::_getAttribute($chartDetail->bubbleScale, 'val', 'integer');
  122. $plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  123. $plotSer->setPlotStyle($bubbleScale);
  124. $plotSeries[] = $plotSer;
  125. break;
  126. case "radarChart":
  127. $radarStyle = self::_getAttribute($chartDetail->radarStyle, 'val', 'string');
  128. $plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  129. $plotSer->setPlotStyle($radarStyle);
  130. $plotSeries[] = $plotSer;
  131. break;
  132. case "surfaceChart":
  133. case "surface3DChart":
  134. $wireFrame = self::_getAttribute($chartDetail->wireframe, 'val', 'boolean');
  135. $plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  136. $plotSer->setPlotStyle($wireFrame);
  137. $plotSeries[] = $plotSer;
  138. break;
  139. case "stockChart":
  140. $plotSeries[] = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
  141. break;
  142. }
  143. }
  144. $plotArea = new PHPExcel_Chart_PlotArea($plotAreaLayout,$plotSeries);
  145. break;
  146. case "plotVisOnly":
  147. $plotVisOnly = self::_getAttribute($chartDetails, 'val', 'string');
  148. break;
  149. case "dispBlanksAs":
  150. $dispBlanksAs = self::_getAttribute($chartDetails, 'val', 'string');
  151. break;
  152. case "title":
  153. $title = self::_chartTitle($chartDetails,$namespacesChartMeta,'title');
  154. break;
  155. case "legend":
  156. $legendPos = 'r';
  157. $legendLayout = null;
  158. $legendOverlay = false;
  159. foreach($chartDetails as $chartDetailKey => $chartDetail) {
  160. switch ($chartDetailKey) {
  161. case "legendPos":
  162. $legendPos = self::_getAttribute($chartDetail, 'val', 'string');
  163. break;
  164. case "overlay":
  165. $legendOverlay = self::_getAttribute($chartDetail, 'val', 'boolean');
  166. break;
  167. case "layout":
  168. $legendLayout = self::_chartLayoutDetails($chartDetail,$namespacesChartMeta,'legend');
  169. break;
  170. }
  171. }
  172. $legend = new PHPExcel_Chart_Legend($legendPos, $legendLayout, $legendOverlay);
  173. break;
  174. }
  175. }
  176. }
  177. }
  178. $chart = new PHPExcel_Chart($chartName,$title,$legend,$plotArea,$plotVisOnly,$dispBlanksAs,$XaxisLabel,$YaxisLabel);
  179. return $chart;
  180. } // function readChart()
  181. private static function _chartTitle($titleDetails,$namespacesChartMeta,$type) {
  182. $caption = array();
  183. $titleLayout = null;
  184. foreach($titleDetails as $titleDetailKey => $chartDetail) {
  185. switch ($titleDetailKey) {
  186. case "tx":
  187. $titleDetails = $chartDetail->rich->children($namespacesChartMeta['a']);
  188. foreach($titleDetails as $titleKey => $titleDetail) {
  189. switch ($titleKey) {
  190. case "p":
  191. $titleDetailPart = $titleDetail->children($namespacesChartMeta['a']);
  192. $caption[] = self::_parseRichText($titleDetailPart);
  193. }
  194. }
  195. break;
  196. case "layout":
  197. $titleLayout = self::_chartLayoutDetails($chartDetail,$namespacesChartMeta);
  198. break;
  199. }
  200. }
  201. return new PHPExcel_Chart_Title($caption, $titleLayout);
  202. } // function _chartTitle()
  203. private static function _chartLayoutDetails($chartDetail,$namespacesChartMeta) {
  204. if (!isset($chartDetail->manualLayout)) {
  205. return null;
  206. }
  207. $details = $chartDetail->manualLayout->children($namespacesChartMeta['c']);
  208. if (is_null($details)) {
  209. return null;
  210. }
  211. $layout = array();
  212. foreach($details as $detailKey => $detail) {
  213. // echo $detailKey,' => ',self::_getAttribute($detail, 'val', 'string'),'<br />';
  214. $layout[$detailKey] = self::_getAttribute($detail, 'val', 'string');
  215. }
  216. return new PHPExcel_Chart_Layout($layout);
  217. } // function _chartLayoutDetails()
  218. private static function _chartDataSeries($chartDetail,$namespacesChartMeta,$plotType) {
  219. $multiSeriesType = NULL;
  220. $smoothLine = false;
  221. $seriesLabel = $seriesCategory = $seriesValues = $plotOrder = array();
  222. $seriesDetailSet = $chartDetail->children($namespacesChartMeta['c']);
  223. foreach($seriesDetailSet as $seriesDetailKey => $seriesDetails) {
  224. switch ($seriesDetailKey) {
  225. case "grouping":
  226. $multiSeriesType = self::_getAttribute($chartDetail->grouping, 'val', 'string');
  227. break;
  228. case "ser":
  229. $marker = NULL;
  230. foreach($seriesDetails as $seriesKey => $seriesDetail) {
  231. switch ($seriesKey) {
  232. case "idx":
  233. $seriesIndex = self::_getAttribute($seriesDetail, 'val', 'integer');
  234. break;
  235. case "order":
  236. $seriesOrder = self::_getAttribute($seriesDetail, 'val', 'integer');
  237. $plotOrder[$seriesIndex] = $seriesOrder;
  238. break;
  239. case "tx":
  240. $seriesLabel[$seriesIndex] = self::_chartDataSeriesValueSet($seriesDetail,$namespacesChartMeta);
  241. break;
  242. case "marker":
  243. $marker = self::_getAttribute($seriesDetail->symbol, 'val', 'string');
  244. break;
  245. case "smooth":
  246. $smoothLine = self::_getAttribute($seriesDetail, 'val', 'boolean');
  247. break;
  248. case "cat":
  249. $seriesCategory[$seriesIndex] = self::_chartDataSeriesValueSet($seriesDetail,$namespacesChartMeta);
  250. break;
  251. case "val":
  252. $seriesValues[$seriesIndex] = self::_chartDataSeriesValueSet($seriesDetail,$namespacesChartMeta,$marker);
  253. break;
  254. case "xVal":
  255. $seriesCategory[$seriesIndex] = self::_chartDataSeriesValueSet($seriesDetail,$namespacesChartMeta,$marker);
  256. break;
  257. case "yVal":
  258. $seriesValues[$seriesIndex] = self::_chartDataSeriesValueSet($seriesDetail,$namespacesChartMeta,$marker);
  259. break;
  260. }
  261. }
  262. }
  263. }
  264. return new PHPExcel_Chart_DataSeries($plotType,$multiSeriesType,$plotOrder,$seriesLabel,$seriesCategory,$seriesValues,$smoothLine);
  265. } // function _chartDataSeries()
  266. private static function _chartDataSeriesValueSet($seriesDetail, $namespacesChartMeta, $marker = null, $smoothLine = false) {
  267. if (isset($seriesDetail->strRef)) {
  268. $seriesSource = (string) $seriesDetail->strRef->f;
  269. $seriesData = self::_chartDataSeriesValues($seriesDetail->strRef->strCache->children($namespacesChartMeta['c']),'s');
  270. return new PHPExcel_Chart_DataSeriesValues('String',$seriesSource,$seriesData['formatCode'],$seriesData['pointCount'],$seriesData['dataValues'],$marker,$smoothLine);
  271. } elseif (isset($seriesDetail->numRef)) {
  272. $seriesSource = (string) $seriesDetail->numRef->f;
  273. $seriesData = self::_chartDataSeriesValues($seriesDetail->numRef->numCache->children($namespacesChartMeta['c']));
  274. return new PHPExcel_Chart_DataSeriesValues('Number',$seriesSource,$seriesData['formatCode'],$seriesData['pointCount'],$seriesData['dataValues'],$marker,$smoothLine);
  275. } elseif (isset($seriesDetail->multiLvlStrRef)) {
  276. $seriesSource = (string) $seriesDetail->multiLvlStrRef->f;
  277. $seriesData = self::_chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlStrRef->multiLvlStrCache->children($namespacesChartMeta['c']),'s');
  278. $seriesData['pointCount'] = count($seriesData['dataValues']);
  279. return new PHPExcel_Chart_DataSeriesValues('String',$seriesSource,$seriesData['formatCode'],$seriesData['pointCount'],$seriesData['dataValues'],$marker,$smoothLine);
  280. } elseif (isset($seriesDetail->multiLvlNumRef)) {
  281. $seriesSource = (string) $seriesDetail->multiLvlNumRef->f;
  282. $seriesData = self::_chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlNumRef->multiLvlNumCache->children($namespacesChartMeta['c']),'s');
  283. $seriesData['pointCount'] = count($seriesData['dataValues']);
  284. return new PHPExcel_Chart_DataSeriesValues('String',$seriesSource,$seriesData['formatCode'],$seriesData['pointCount'],$seriesData['dataValues'],$marker,$smoothLine);
  285. }
  286. return null;
  287. } // function _chartDataSeriesValueSet()
  288. private static function _chartDataSeriesValues($seriesValueSet,$dataType='n') {
  289. $seriesVal = array();
  290. $formatCode = '';
  291. $pointCount = 0;
  292. foreach($seriesValueSet as $seriesValueIdx => $seriesValue) {
  293. switch ($seriesValueIdx) {
  294. case 'ptCount':
  295. $pointCount = self::_getAttribute($seriesValue, 'val', 'integer');
  296. break;
  297. case 'formatCode':
  298. $formatCode = (string) $seriesValue;
  299. break;
  300. case 'pt':
  301. $pointVal = self::_getAttribute($seriesValue, 'idx', 'integer');
  302. if ($dataType == 's') {
  303. $seriesVal[$pointVal] = (string) $seriesValue->v;
  304. } else {
  305. $seriesVal[$pointVal] = (float) $seriesValue->v;
  306. }
  307. break;
  308. }
  309. }
  310. return array( 'formatCode' => $formatCode,
  311. 'pointCount' => $pointCount,
  312. 'dataValues' => $seriesVal
  313. );
  314. } // function _chartDataSeriesValues()
  315. private static function _chartDataSeriesValuesMultiLevel($seriesValueSet,$dataType='n') {
  316. $seriesVal = array();
  317. $formatCode = '';
  318. $pointCount = 0;
  319. foreach($seriesValueSet->lvl as $seriesLevelIdx => $seriesLevel) {
  320. foreach($seriesLevel as $seriesValueIdx => $seriesValue) {
  321. switch ($seriesValueIdx) {
  322. case 'ptCount':
  323. $pointCount = self::_getAttribute($seriesValue, 'val', 'integer');
  324. break;
  325. case 'formatCode':
  326. $formatCode = (string) $seriesValue;
  327. break;
  328. case 'pt':
  329. $pointVal = self::_getAttribute($seriesValue, 'idx', 'integer');
  330. if ($dataType == 's') {
  331. $seriesVal[$pointVal][] = (string) $seriesValue->v;
  332. } else {
  333. $seriesVal[$pointVal][] = (float) $seriesValue->v;
  334. }
  335. break;
  336. }
  337. }
  338. }
  339. return array( 'formatCode' => $formatCode,
  340. 'pointCount' => $pointCount,
  341. 'dataValues' => $seriesVal
  342. );
  343. } // function _chartDataSeriesValuesMultiLevel()
  344. private static function _parseRichText($titleDetailPart = null) {
  345. $value = new PHPExcel_RichText();
  346. foreach($titleDetailPart as $titleDetailElementKey => $titleDetailElement) {
  347. if (isset($titleDetailElement->t)) {
  348. $objText = $value->createTextRun( (string) $titleDetailElement->t );
  349. }
  350. if (isset($titleDetailElement->rPr)) {
  351. if (isset($titleDetailElement->rPr->rFont["val"])) {
  352. $objText->getFont()->setName((string) $titleDetailElement->rPr->rFont["val"]);
  353. }
  354. $fontSize = (self::_getAttribute($titleDetailElement->rPr, 'sz', 'integer'));
  355. if (!is_null($fontSize)) {
  356. $objText->getFont()->setSize(floor($fontSize / 100));
  357. }
  358. $fontColor = (self::_getAttribute($titleDetailElement->rPr, 'color', 'string'));
  359. if (!is_null($fontColor)) {
  360. $objText->getFont()->setColor( new PHPExcel_Style_Color( self::_readColor($fontColor) ) );
  361. }
  362. $bold = self::_getAttribute($titleDetailElement->rPr, 'b', 'boolean');
  363. if (!is_null($bold)) {
  364. $objText->getFont()->setBold($bold);
  365. }
  366. $italic = self::_getAttribute($titleDetailElement->rPr, 'i', 'boolean');
  367. if (!is_null($italic)) {
  368. $objText->getFont()->setItalic($italic);
  369. }
  370. $baseline = self::_getAttribute($titleDetailElement->rPr, 'baseline', 'integer');
  371. if (!is_null($baseline)) {
  372. if ($baseline > 0) {
  373. $objText->getFont()->setSuperScript(true);
  374. } elseif($baseline < 0) {
  375. $objText->getFont()->setSubScript(true);
  376. }
  377. }
  378. $underscore = (self::_getAttribute($titleDetailElement->rPr, 'u', 'string'));
  379. if (!is_null($underscore)) {
  380. if ($underscore == 'sng') {
  381. $objText->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  382. } elseif($underscore == 'dbl') {
  383. $objText->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLE);
  384. } else {
  385. $objText->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_NONE);
  386. }
  387. }
  388. $strikethrough = (self::_getAttribute($titleDetailElement->rPr, 's', 'string'));
  389. if (!is_null($strikethrough)) {
  390. if ($strikethrough == 'noStrike') {
  391. $objText->getFont()->setStrikethrough(false);
  392. } else {
  393. $objText->getFont()->setStrikethrough(true);
  394. }
  395. }
  396. }
  397. }
  398. return $value;
  399. }
  400. }