PageRenderTime 55ms CodeModel.GetById 10ms app.highlight 38ms RepoModel.GetById 2ms app.codeStats 0ms

/PHPExcel/Reader/Excel2007/Chart.php

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