PageRenderTime 83ms CodeModel.GetById 26ms app.highlight 50ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/PHPExcel/Chart/Renderer/jpgraph.php

https://github.com/iGroup/PHPExcel
PHP | 837 lines | 620 code | 154 blank | 63 comment | 123 complexity | e8239b626488dec01e5f3bba4f67277e MD5 | raw file
  1<?php
  2
  3/**
  4 * PHPExcel
  5 *
  6 * Copyright (c) 2006 - 2012 PHPExcel
  7 *
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation; either
 11 * version 2.1 of the License, or (at your option) any later version.
 12 *
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16 * Lesser General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 21 *
 22 * @category	PHPExcel
 23 * @package		PHPExcel_Chart
 24 * @copyright	Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
 25 * @license		http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 26 * @version		##VERSION##, ##DATE##
 27 */
 28
 29
 30require_once(PHPExcel_Settings::getChartRendererPath().'/jpgraph.php');
 31
 32
 33/**
 34 * PHPExcel_Chart_Renderer_jpgraph
 35 *
 36 * @category	PHPExcel
 37 * @package		PHPExcel_Chart_Renderer
 38 * @copyright	Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
 39 */
 40class PHPExcel_Chart_Renderer_jpgraph
 41{
 42	private static $_width	= 640;
 43
 44	private static $_height	= 480;
 45
 46	private static $_colourSet = array( 'mediumpurple1',	'palegreen3',	'gold1',		'cadetblue1',
 47										'darkmagenta',		'coral',		'dodgerblue3',	'eggplant',
 48										'mediumblue',		'magenta',		'sandybrown',	'cyan',
 49										'firebrick1',		'forestgreen',	'deeppink4',	'darkolivegreen',
 50										'goldenrod2'
 51									  );
 52
 53	private static $_markSet = array(	'diamond'	=> MARK_DIAMOND,
 54										'square'	=> MARK_SQUARE,
 55										'triangle'	=> MARK_UTRIANGLE,
 56										'x'			=> MARK_X,
 57										'star'		=> MARK_STAR,
 58										'dot'		=> MARK_FILLEDCIRCLE,
 59										'dash'		=> MARK_DTRIANGLE,
 60										'circle'	=> MARK_CIRCLE,
 61										'plus'		=> MARK_CROSS
 62									);
 63
 64
 65	private $_chart	= null;
 66
 67	private $_graph	= null;
 68
 69	private static $_plotColour	= 0;
 70
 71	private static $_plotMark	= 0;
 72
 73
 74	private function _formatPointMarker($seriesPlot,$markerID) {
 75		$plotMarkKeys = array_keys(self::$_markSet);
 76		if (is_null($markerID)) {
 77			//	Use default plot marker (next marker in the series)
 78			self::$_plotMark %= count(self::$_markSet);
 79			$seriesPlot->mark->SetType(self::$_markSet[$plotMarkKeys[self::$_plotMark++]]);
 80		} elseif ($markerID !== 'none') {
 81			//	Use specified plot marker (if it exists)
 82			if (isset(self::$_markSet[$markerID])) {
 83				$seriesPlot->mark->SetType(self::$_markSet[$markerID]);
 84			} else {
 85				//	If the specified plot marker doesn't exist, use default plot marker (next marker in the series)
 86				self::$_plotMark %= count(self::$_markSet);
 87				$seriesPlot->mark->SetType(self::$_markSet[$plotMarkKeys[self::$_plotMark++]]);
 88			}
 89		} else {
 90			//	Hide plot marker
 91			$seriesPlot->mark->Hide();
 92		}
 93		$seriesPlot->mark->SetColor(self::$_colourSet[self::$_plotColour]);
 94		$seriesPlot->mark->SetFillColor(self::$_colourSet[self::$_plotColour]);
 95		$seriesPlot->SetColor(self::$_colourSet[self::$_plotColour++]);
 96
 97		return $seriesPlot;
 98	}	//	function _formatPointMarker()
 99
100
101	private function _formatDataSetLabels($groupID, $datasetLabels, $labelCount, $rotation = '') {
102		$datasetLabelFormatCode = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotCategoryByIndex(0)->getFormatCode();
103		if (!is_null($datasetLabelFormatCode)) {
104			//	Retrieve any label formatting code
105			$datasetLabelFormatCode = stripslashes($datasetLabelFormatCode);
106		}
107
108		$testCurrentIndex = 0;
109		foreach($datasetLabels as $i => $datasetLabel) {
110			if (is_array($datasetLabel)) {
111				if ($rotation == 'bar') {
112					$datasetLabels[$i] = implode(" ",$datasetLabel);
113				} else {
114					$datasetLabel = array_reverse($datasetLabel);
115					$datasetLabels[$i] = implode("\n",$datasetLabel);
116				}
117			} else {
118				//	Format labels according to any formatting code
119				if (!is_null($datasetLabelFormatCode)) {
120					$datasetLabels[$i] = PHPExcel_Style_NumberFormat::toFormattedString($datasetLabel,$datasetLabelFormatCode);
121				}
122			}
123			++$testCurrentIndex;
124		}
125
126		return $datasetLabels;
127	}	//	function _formatDataSetLabels()
128
129
130	private function _percentageSumCalculation($groupID,$seriesCount) {
131		//	Adjust our values to a percentage value across all series in the group
132		for($i = 0; $i < $seriesCount; ++$i) {
133			if ($i == 0) {
134				$sumValues = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getDataValues();
135			} else {
136				$nextValues = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getDataValues();
137				foreach($nextValues as $k => $value) {
138					if (isset($sumValues[$k])) {
139						$sumValues[$k] += $value;
140					} else {
141						$sumValues[$k] = $value;
142					}
143				}
144			}
145		}
146
147		return $sumValues;
148	}	//	function _percentageSumCalculation()
149
150
151	private function _percentageAdjustValues($dataValues,$sumValues) {
152		foreach($dataValues as $k => $dataValue) {
153			$dataValues[$k] = $dataValue / $sumValues[$k] * 100;
154		}
155
156		return $dataValues;
157	}	//	function _percentageAdjustValues()
158
159
160	private function _getCaption($captionElement) {
161		//	Read any caption
162		$caption = (!is_null($captionElement)) ? $captionElement->getCaption() : NULL;
163		//	Test if we have a title caption to display
164		if (!is_null($caption)) {
165			//	If we do, it could be a plain string or an array
166			if (is_array($caption)) {
167				//	Implode an array to a plain string
168				$caption = implode('',$caption);
169			}
170		}
171		return $caption;
172	}	//	function _getCaption()
173
174
175	private function _renderTitle() {
176		$title = $this->_getCaption($this->_chart->getTitle());
177		if (!is_null($title)) {
178			$this->_graph->title->Set($title);
179		}
180	}	//	function _renderTitle()
181
182
183	private function _renderLegend() {
184		$legend = $this->_chart->getLegend();
185		if (!is_null($legend)) {
186			$legendPosition = $legend->getPosition();
187			$legendOverlay = $legend->getOverlay();
188			switch ($legendPosition) {
189				case 'r'	:
190					$this->_graph->legend->SetPos(0.01,0.5,'right','center');	//	right
191					$this->_graph->legend->SetColumns(1);
192					break;
193				case 'l'	:
194					$this->_graph->legend->SetPos(0.01,0.5,'left','center');	//	left
195					$this->_graph->legend->SetColumns(1);
196					break;
197				case 't'	:
198					$this->_graph->legend->SetPos(0.5,0.01,'center','top');	//	top
199					break;
200				case 'b'	:
201					$this->_graph->legend->SetPos(0.5,0.99,'center','bottom');	//	bottom
202					break;
203				default		:
204					$this->_graph->legend->SetPos(0.01,0.01,'right','top');	//	top-right
205					$this->_graph->legend->SetColumns(1);
206					break;
207			}
208		} else {
209			$this->_graph->legend->Hide();
210		}
211	}	//	function _renderLegend()
212
213
214	private function _renderCartesianPlotArea($type='textlin') {
215		$this->_graph = new Graph(self::$_width,self::$_height);
216		$this->_graph->SetScale($type);
217
218		$this->_renderTitle();
219
220		//	Rotate for bar rather than column chart
221		$rotation = $this->_chart->getPlotArea()->getPlotGroupByIndex(0)->getPlotDirection();
222		$reverse = ($rotation == 'bar') ? true : false;
223
224		$xAxisLabel = $this->_chart->getXAxisLabel();
225		if (!is_null($xAxisLabel)) {
226			$title = $this->_getCaption($xAxisLabel);
227			if (!is_null($title)) {
228				$this->_graph->xaxis->SetTitle($title,'center');
229				$this->_graph->xaxis->title->SetMargin(35);
230				if ($reverse) {
231					$this->_graph->xaxis->title->SetAngle(90);
232					$this->_graph->xaxis->title->SetMargin(90);
233				}
234			}
235		}
236
237		$yAxisLabel = $this->_chart->getYAxisLabel();
238		if (!is_null($yAxisLabel)) {
239			$title = $this->_getCaption($yAxisLabel);
240			if (!is_null($title)) {
241				$this->_graph->yaxis->SetTitle($title,'center');
242				if ($reverse) {
243					$this->_graph->yaxis->title->SetAngle(0);
244					$this->_graph->yaxis->title->SetMargin(-55);
245				}
246			}
247		}
248	}	//	function _renderCartesianPlotArea()
249
250
251	private function _renderPiePlotArea($doughnut = False) {
252		$this->_graph = new PieGraph(self::$_width,self::$_height);
253
254		$this->_renderTitle();
255	}	//	function _renderPiePlotArea()
256
257
258	private function _renderRadarPlotArea() {
259		$this->_graph = new RadarGraph(self::$_width,self::$_height);
260		$this->_graph->SetScale('lin');
261
262		$this->_renderTitle();
263	}	//	function _renderRadarPlotArea()
264
265
266	private function _renderPlotLine($groupID, $filled = false, $combination = false, $dimensions = '2d') {
267		$grouping = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotGrouping();
268
269        $labelCount = count($this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex(0)->getPointCount());
270		if ($labelCount > 0) {
271			$datasetLabels = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotCategoryByIndex(0)->getDataValues();
272			$datasetLabels = $this->_formatDataSetLabels($groupID, $datasetLabels, $labelCount);
273			$this->_graph->xaxis->SetTickLabels($datasetLabels);
274		}
275
276		$seriesCount = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotSeriesCount();
277		$seriesPlots = array();
278		if ($grouping == 'percentStacked') {
279			$sumValues = $this->_percentageSumCalculation($groupID,$seriesCount);
280		}
281
282		//	Loop through each data series in turn
283		for($i = 0; $i < $seriesCount; ++$i) {
284			$dataValues = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getDataValues();
285			$marker = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getPointMarker();
286
287			if ($grouping == 'percentStacked') {
288				$dataValues = $this->_percentageAdjustValues($dataValues,$sumValues);
289			}
290
291			//	Fill in any missing values in the $dataValues array
292			$testCurrentIndex = 0;
293			foreach($dataValues as $k => $dataValue) {
294				while($k != $testCurrentIndex) {
295					$dataValues[$testCurrentIndex] = null;
296					++$testCurrentIndex;
297				}
298				++$testCurrentIndex;
299			}
300
301			$seriesPlot = new LinePlot($dataValues);
302			if ($combination) {
303				$seriesPlot->SetBarCenter();
304			}
305
306			if ($filled) {
307				$seriesPlot->SetFilled(true);
308				$seriesPlot->SetColor('black');
309				$seriesPlot->SetFillColor(self::$_colourSet[self::$_plotColour++]);
310			} else {
311				//	Set the appropriate plot marker
312				$this->_formatPointMarker($seriesPlot,$marker);
313			}
314			$dataLabel = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotLabelByIndex($i)->getDataValue();
315			$seriesPlot->SetLegend($dataLabel);
316
317			$seriesPlots[] = $seriesPlot;
318		}
319
320		if ($grouping == 'standard') {
321			$groupPlot = $seriesPlots;
322		} else {
323			$groupPlot = new AccLinePlot($seriesPlots);
324		}
325		$this->_graph->Add($groupPlot);
326	}	//	function _renderPlotLine()
327
328
329	private function _renderPlotBar($groupID, $dimensions = '2d') {
330		$rotation = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotDirection();
331		//	Rotate for bar rather than column chart
332		if (($groupID == 0) && ($rotation == 'bar')) {
333			$this->_graph->Set90AndMargin();
334		}
335		$grouping = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotGrouping();
336
337        $labelCount = count($this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex(0)->getPointCount());
338		if ($labelCount > 0) {
339			$datasetLabels = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotCategoryByIndex(0)->getDataValues();
340			$datasetLabels = $this->_formatDataSetLabels($groupID, $datasetLabels, $labelCount, $rotation);
341			//	Rotate for bar rather than column chart
342			if ($rotation == 'bar') {
343				$datasetLabels = array_reverse($datasetLabels);
344				$this->_graph->yaxis->SetPos('max');
345				$this->_graph->yaxis->SetLabelAlign('center','top');
346				$this->_graph->yaxis->SetLabelSide(SIDE_RIGHT);
347			}
348			$this->_graph->xaxis->SetTickLabels($datasetLabels);
349		}
350
351
352		$seriesCount = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotSeriesCount();
353		$seriesPlots = array();
354		if ($grouping == 'percentStacked') {
355			$sumValues = $this->_percentageSumCalculation($groupID,$seriesCount);
356		}
357
358		//	Loop through each data series in turn
359		for($j = 0; $j < $seriesCount; ++$j) {
360			$dataValues = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($j)->getDataValues();
361			if ($grouping == 'percentStacked') {
362				$dataValues = $this->_percentageAdjustValues($dataValues,$sumValues);
363			}
364
365			//	Fill in any missing values in the $dataValues array
366			$testCurrentIndex = 0;
367			foreach($dataValues as $k => $dataValue) {
368				while($k != $testCurrentIndex) {
369					$dataValues[$testCurrentIndex] = null;
370					++$testCurrentIndex;
371				}
372				++$testCurrentIndex;
373			}
374
375			//	Reverse the $dataValues order for bar rather than column chart
376			if ($rotation == 'bar') {
377				$dataValues = array_reverse($dataValues);
378			}
379			$seriesPlot = new BarPlot($dataValues);
380			$seriesPlot->SetColor('black');
381			$seriesPlot->SetFillColor(self::$_colourSet[self::$_plotColour++]);
382			if ($dimensions == '3d') {
383				$seriesPlot->SetShadow();
384			}
385			if (!$this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotLabelByIndex($j)) {
386				$dataLabel = '';
387			} else {
388				$dataLabel = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotLabelByIndex($j)->getDataValue();
389			}
390			$seriesPlot->SetLegend($dataLabel);
391
392			$seriesPlots[] = $seriesPlot;
393		}
394		//	Reverse the plot order for bar rather than column chart
395		if (($rotation == 'bar') && (!($grouping == 'percentStacked'))) {
396			$seriesPlots = array_reverse($seriesPlots);
397		}
398
399		if ($grouping == 'clustered') {
400			$groupPlot = new GroupBarPlot($seriesPlots);
401		} elseif ($grouping == 'standard') {
402			$groupPlot = new GroupBarPlot($seriesPlots);
403		} else {
404			$groupPlot = new AccBarPlot($seriesPlots);
405			if ($dimensions == '3d') {
406				$groupPlot->SetShadow();
407			}
408		}
409
410		$this->_graph->Add($groupPlot);
411	}	//	function _renderPlotBar()
412
413
414	private function _renderPlotScatter($groupID,$bubble) {
415		$grouping = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotGrouping();
416		$scatterStyle = $bubbleSize = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotStyle();
417
418		$seriesCount = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotSeriesCount();
419		$seriesPlots = array();
420
421		//	Loop through each data series in turn
422		for($i = 0; $i < $seriesCount; ++$i) {
423			$dataValuesY = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotCategoryByIndex($i)->getDataValues();
424			$dataValuesX = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getDataValues();
425
426			foreach($dataValuesY as $k => $dataValueY) {
427				$dataValuesY[$k] = $k;
428			}
429
430			$seriesPlot = new ScatterPlot($dataValuesX,$dataValuesY);
431			if ($scatterStyle == 'lineMarker') {
432				$seriesPlot->SetLinkPoints();
433				$seriesPlot->link->SetColor(self::$_colourSet[self::$_plotColour]);
434			} elseif ($scatterStyle == 'smoothMarker') {
435				$spline = new Spline($dataValuesY,$dataValuesX);
436				list($splineDataY,$splineDataX) = $spline->Get(count($dataValuesX) * self::$_width / 20);
437				$lplot = new LinePlot($splineDataX,$splineDataY);
438				$lplot->SetColor(self::$_colourSet[self::$_plotColour]);
439
440				$this->_graph->Add($lplot);
441			}
442
443			if ($bubble) {
444				$this->_formatPointMarker($seriesPlot,'dot');
445				$seriesPlot->mark->SetColor('black');
446				$seriesPlot->mark->SetSize($bubbleSize);
447			} else {
448				$marker = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getPointMarker();
449				$this->_formatPointMarker($seriesPlot,$marker);
450			}
451			$dataLabel = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotLabelByIndex($i)->getDataValue();
452			$seriesPlot->SetLegend($dataLabel);
453
454			$this->_graph->Add($seriesPlot);
455		}
456	}	//	function _renderPlotScatter()
457
458
459	private function _renderPlotRadar($groupID) {
460		$radarStyle = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotStyle();
461
462		$seriesCount = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotSeriesCount();
463		$seriesPlots = array();
464
465		//	Loop through each data series in turn
466		for($i = 0; $i < $seriesCount; ++$i) {
467			$dataValuesY = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotCategoryByIndex($i)->getDataValues();
468			$dataValuesX = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getDataValues();
469			$marker = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getPointMarker();
470
471			$dataValues = array();
472			foreach($dataValuesY as $k => $dataValueY) {
473				$dataValues[$k] = implode(' ',array_reverse($dataValueY));
474			}
475			$tmp = array_shift($dataValues);
476			$dataValues[] = $tmp;
477			$tmp = array_shift($dataValuesX);
478			$dataValuesX[] = $tmp;
479
480			$this->_graph->SetTitles(array_reverse($dataValues));
481
482			$seriesPlot = new RadarPlot(array_reverse($dataValuesX));
483
484			$dataLabel = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotLabelByIndex($i)->getDataValue();
485			$seriesPlot->SetColor(self::$_colourSet[self::$_plotColour++]);
486			if ($radarStyle == 'filled') {
487				$seriesPlot->SetFillColor(self::$_colourSet[self::$_plotColour]);
488			}
489			$this->_formatPointMarker($seriesPlot,$marker);
490			$seriesPlot->SetLegend($dataLabel);
491
492			$this->_graph->Add($seriesPlot);
493		}
494	}	//	function _renderPlotRadar()
495
496
497	private function _renderPlotContour($groupID) {
498		$contourStyle = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotStyle();
499
500		$seriesCount = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotSeriesCount();
501		$seriesPlots = array();
502
503		$dataValues = array();
504		//	Loop through each data series in turn
505		for($i = 0; $i < $seriesCount; ++$i) {
506			$dataValuesY = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotCategoryByIndex($i)->getDataValues();
507			$dataValuesX = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getDataValues();
508
509			$dataValues[$i] = $dataValuesX;
510		}
511		$seriesPlot = new ContourPlot($dataValues);
512
513		$this->_graph->Add($seriesPlot);
514	}	//	function _renderPlotContour()
515
516
517	private function _renderPlotStock($groupID) {
518		$seriesCount = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotSeriesCount();
519		$plotOrder = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotOrder();
520		$seriesPlots = array();
521
522		$dataValues = array();
523		//	Loop through each data series in turn
524		for($i = 0; $i < $seriesCount; ++$i) {
525			$dataValuesY = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotCategoryByIndex($i)->getDataValues();
526			$dataValuesX = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getDataValues();
527
528			foreach($dataValuesX as $j => $dataValueX)
529			$dataValues[$j][$plotOrder[$i]] = $dataValueX;
530		}
531
532		$seriesPlot = new StockPlot($dataValues);
533
534		$this->_graph->Add($seriesPlot);
535	}	//	function _renderPlotStock()
536
537
538	private function _renderAreaChart($groupCount, $dimensions = '2d') {
539		require_once('jpgraph_line.php');
540
541		$this->_renderCartesianPlotArea();
542
543		for($i = 0; $i < $groupCount; ++$i) {
544			$this->_renderPlotLine($i,True,False,$dimensions);
545		}
546	}	//	function _renderAreaChart()
547
548
549	private function _renderLineChart($groupCount, $dimensions = '2d') {
550		require_once('jpgraph_line.php');
551
552		$this->_renderCartesianPlotArea();
553
554		for($i = 0; $i < $groupCount; ++$i) {
555			$this->_renderPlotLine($i,False,False,$dimensions);
556		}
557	}	//	function _renderLineChart()
558
559
560	private function _renderBarChart($groupCount, $dimensions = '2d') {
561		require_once('jpgraph_bar.php');
562
563		$this->_renderCartesianPlotArea();
564
565		for($i = 0; $i < $groupCount; ++$i) {
566			$this->_renderPlotBar($i,$dimensions);
567		}
568	}	//	function _renderBarChart()
569
570
571	private function _renderScatterChart($groupCount) {
572		require_once('jpgraph_scatter.php');
573		require_once('jpgraph_regstat.php');
574		require_once('jpgraph_line.php');
575
576		$this->_renderCartesianPlotArea('linlin');
577
578		for($i = 0; $i < $groupCount; ++$i) {
579			$this->_renderPlotScatter($i,false);
580		}
581	}	//	function _renderScatterChart()
582
583
584	private function _renderBubbleChart($groupCount) {
585		require_once('jpgraph_scatter.php');
586
587		$this->_renderCartesianPlotArea('linlin');
588
589		for($i = 0; $i < $groupCount; ++$i) {
590			$this->_renderPlotScatter($i,true);
591		}
592	}	//	function _renderBubbleChart()
593
594
595	private function _renderPieChart($groupCount, $dimensions = '2d', $doughnut = False, $multiplePlots = False) {
596		require_once('jpgraph_pie.php');
597		if ($dimensions == '3d') {
598			require_once('jpgraph_pie3d.php');
599		}
600
601		$this->_renderPiePlotArea($doughnut);
602
603		$iLimit = ($multiplePlots) ? $groupCount : 1;
604		for($groupID = 0; $groupID < $iLimit; ++$groupID) {
605			$grouping = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotGrouping();
606			$exploded = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotStyle();
607			if ($groupID == 0) {
608		        $labelCount = count($this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex(0)->getPointCount());
609				if ($labelCount > 0) {
610					$datasetLabels = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotCategoryByIndex(0)->getDataValues();
611					$datasetLabels = $this->_formatDataSetLabels($groupID, $datasetLabels, $labelCount);
612				}
613			}
614
615			$seriesCount = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotSeriesCount();
616			$seriesPlots = array();
617			//	For pie charts, we only display the first series: doughnut charts generally display all series
618			$jLimit = ($multiplePlots) ? $seriesCount : 1;
619			//	Loop through each data series in turn
620			for($j = 0; $j < $jLimit; ++$j) {
621				$dataValues = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($j)->getDataValues();
622
623				//	Fill in any missing values in the $dataValues array
624				$testCurrentIndex = 0;
625				foreach($dataValues as $k => $dataValue) {
626					while($k != $testCurrentIndex) {
627						$dataValues[$testCurrentIndex] = null;
628						++$testCurrentIndex;
629					}
630					++$testCurrentIndex;
631				}
632
633				if ($dimensions == '3d') {
634					$seriesPlot = new PiePlot3D($dataValues);
635				} else {
636					if ($doughnut) {
637						$seriesPlot = new PiePlotC($dataValues);
638					} else {
639						$seriesPlot = new PiePlot($dataValues);
640					}
641				}
642
643				if ($multiplePlots) {
644					$seriesPlot->SetSize(($jLimit-$j) / ($jLimit * 4));
645				}
646
647				if ($doughnut) {
648					$seriesPlot->SetMidColor('white');
649				}
650
651				$seriesPlot->SetColor(self::$_colourSet[self::$_plotColour++]);
652				if (count($datasetLabels) > 0)
653					$seriesPlot->SetLabels(array_fill(0,count($datasetLabels),''));
654				if ($dimensions != '3d') {
655					$seriesPlot->SetGuideLines(false);
656				}
657				if ($j == 0) {
658					if ($exploded) {
659						$seriesPlot->ExplodeAll();
660					}
661					$seriesPlot->SetLegends($datasetLabels);
662				}
663
664				$this->_graph->Add($seriesPlot);
665			}
666		}
667	}	//	function _renderPieChart()
668
669
670	private function _renderRadarChart($groupCount) {
671		require_once('jpgraph_radar.php');
672
673		$this->_renderRadarPlotArea();
674
675		for($groupID = 0; $groupID < $groupCount; ++$groupID) {
676			$this->_renderPlotRadar($groupID);
677		}
678	}	//	function _renderRadarChart()
679
680
681	private function _renderStockChart($groupCount) {
682		require_once('jpgraph_stock.php');
683
684		$this->_renderCartesianPlotArea();
685
686		for($groupID = 0; $groupID < $groupCount; ++$i) {
687			$this->_renderPlotStock($groupID);
688		}
689	}	//	function _renderStockChart()
690
691
692	private function _renderContourChart($groupCount,$dimensions) {
693		require_once('jpgraph_contour.php');
694
695		$this->_renderCartesianPlotArea('intint');
696
697		for($i = 0; $i < $groupCount; ++$i) {
698			$this->_renderPlotContour($i);
699		}
700	}	//	function _renderContourChart()
701
702
703	private function _renderCombinationChart($groupCount,$dimensions,$outputDestination) {
704		require_once('jpgraph_line.php');
705		require_once('jpgraph_bar.php');
706		require_once('jpgraph_scatter.php');
707		require_once('jpgraph_regstat.php');
708		require_once('jpgraph_line.php');
709
710		$this->_renderCartesianPlotArea();
711
712		for($i = 0; $i < $groupCount; ++$i) {
713			$dimensions = null;
714			$chartType = $this->_chart->getPlotArea()->getPlotGroupByIndex($i)->getPlotType();
715			switch ($chartType) {
716				case 'area3DChart' :
717					$dimensions = '3d';
718				case 'areaChart' :
719					$this->_renderPlotLine($i,True,True,$dimensions);
720					break;
721				case 'bar3DChart' :
722					$dimensions = '3d';
723				case 'barChart' :
724					$this->_renderPlotBar($i,$dimensions);
725					break;
726				case 'line3DChart' :
727					$dimensions = '3d';
728				case 'lineChart' :
729					$this->_renderPlotLine($i,False,True,$dimensions);
730					break;
731				case 'scatterChart' :
732					$this->_renderPlotScatter($i,false);
733					break;
734				case 'bubbleChart' :
735					$this->_renderPlotScatter($i,true);
736					break;
737				default	:
738					$this->_graph = null;
739					return false;
740			}
741		}
742
743		$this->_renderLegend();
744
745		$this->_graph->Stroke($outputDestination);
746		return true;
747	}	//	function _renderCombinationChart()
748
749
750	public function render($outputDestination) {
751        self::$_plotColour = 0;
752
753		$groupCount = $this->_chart->getPlotArea()->getPlotGroupCount();
754
755		$dimensions = null;
756		if ($groupCount == 1) {
757			$chartType = $this->_chart->getPlotArea()->getPlotGroupByIndex(0)->getPlotType();
758		} else {
759			$chartTypes = array();
760			for($i = 0; $i < $groupCount; ++$i) {
761				$chartTypes[] = $this->_chart->getPlotArea()->getPlotGroupByIndex($i)->getPlotType();
762			}
763			$chartTypes = array_unique($chartTypes);
764			if (count($chartTypes) == 1) {
765				$chartType = array_pop($chartTypes);
766			} elseif (count($chartTypes) == 0) {
767				echo 'Chart is not yet implemented<br />';
768				return false;
769			} else {
770				return $this->_renderCombinationChart($groupCount,$dimensions,$outputDestination);
771			}
772		}
773
774		switch ($chartType) {
775			case 'area3DChart' :
776				$dimensions = '3d';
777			case 'areaChart' :
778				$this->_renderAreaChart($groupCount,$dimensions);
779				break;
780			case 'bar3DChart' :
781				$dimensions = '3d';
782			case 'barChart' :
783				$this->_renderBarChart($groupCount,$dimensions);
784				break;
785			case 'line3DChart' :
786				$dimensions = '3d';
787			case 'lineChart' :
788				$this->_renderLineChart($groupCount,$dimensions);
789				break;
790			case 'pie3DChart' :
791				$dimensions = '3d';
792			case 'pieChart' :
793				$this->_renderPieChart($groupCount,$dimensions,False,False);
794				break;
795			case 'doughnut3DChart' :
796				$dimensions = '3d';
797			case 'doughnutChart' :
798				$this->_renderPieChart($groupCount,$dimensions,True,True);
799				break;
800			case 'scatterChart' :
801				$this->_renderScatterChart($groupCount);
802				break;
803			case 'bubbleChart' :
804				$this->_renderBubbleChart($groupCount);
805				break;
806			case 'radarChart' :
807				$this->_renderRadarChart($groupCount);
808				break;
809			case 'surface3DChart' :
810				$dimensions = '3d';
811			case 'surfaceChart' :
812				$this->_renderContourChart($groupCount,$dimensions);
813				break;
814			case 'stockChart' :
815				$this->_renderStockChart($groupCount,$dimensions);
816				break;
817			default	:
818				echo $chartType.' is not yet implemented<br />';
819				return false;
820		}
821		$this->_renderLegend();
822
823		$this->_graph->Stroke($outputDestination);
824		return true;
825	}	//	function render()
826
827
828	/**
829	 * Create a new PHPExcel_Chart_Renderer_jpgraph
830	 */
831	public function __construct(PHPExcel_Chart $chart)
832	{
833		$this->_graph	= null;
834		$this->_chart	= $chart;
835	}	//	function __construct()
836
837}	//	PHPExcel_Chart_Renderer_jpgraph