Data labels for charts

This commit is contained in:
Mark Baker 2012-09-17 23:23:55 +01:00
parent fb6ad92fd4
commit d7e4aaf80b
3 changed files with 288 additions and 26 deletions

View File

@ -40,49 +40,104 @@ class PHPExcel_Chart_Layout
*
* @var string
*/
private $_layoutTarget = null;
private $_layoutTarget = NULL;
/**
* X Mode
*
* @var string
*/
private $_xMode = null;
private $_xMode = NULL;
/**
* Y Mode
*
* @var string
*/
private $_yMode = null;
private $_yMode = NULL;
/**
* X-Position
*
* @var float
*/
private $_xPos = null;
private $_xPos = NULL;
/**
* Y-Position
*
* @var float
*/
private $_yPos = null;
private $_yPos = NULL;
/**
* width
*
* @var float
*/
private $_width = null;
private $_width = NULL;
/**
* height
*
* @var float
*/
private $_height = null;
private $_height = NULL;
/**
* show legend key
* Specifies that legend keys should be shown in data labels
*
* @var boolean
*/
private $_showLegendKey = NULL;
/**
* show value
* Specifies that the value should be shown in a data label.
*
* @var boolean
*/
private $_showVal = NULL;
/**
* show category name
* Specifies that the category name should be shown in the data label.
*
* @var boolean
*/
private $_showCatName = NULL;
/**
* show data series name
* Specifies that the series name should be shown in the data label.
*
* @var boolean
*/
private $_showSerName = NULL;
/**
* show percentage
* Specifies that the percentage should be shown in the data label.
*
* @var boolean
*/
private $_showPercent = NULL;
/**
* show bubble size
*
* @var boolean
*/
private $_showBubbleSize = NULL;
/**
* show leader lines
* Specifies that leader lines should be shown for the data label.
*
* @var boolean
*/
private $_showLeaderLines = NULL;
/**
@ -225,4 +280,138 @@ class PHPExcel_Chart_Layout
$this->_height = $value;
}
/**
* Get show legend key
*
* @return boolean
*/
public function getShowLegendKey() {
return $this->_showLegendKey;
}
/**
* Set show legend key
* Specifies that legend keys should be shown in data labels.
*
* @param boolean $value Show legend key
*/
public function setShowLegendKey($value) {
$this->_showLegendKey = $value;
}
/**
* Get show value
*
* @return boolean
*/
public function getShowVal() {
return $this->_showVal;
}
/**
* Set show val
* Specifies that the value should be shown in data labels.
*
* @param boolean $value Show val
*/
public function setShowVal($value) {
$this->_showVal = $value;
}
/**
* Get show category name
*
* @return boolean
*/
public function getShowCatName() {
return $this->_showCatName;
}
/**
* Set show cat name
* Specifies that the category name should be shown in data labels.
*
* @param boolean $value Show cat name
*/
public function setShowCatName($value) {
$this->_showCatName = $value;
}
/**
* Get show data series name
*
* @return boolean
*/
public function getShowSerName() {
return $this->_showSerName;
}
/**
* Set show ser name
* Specifies that the series name should be shown in data labels.
*
* @param boolean $value Show ser name
*/
public function setShowSerName($value) {
$this->_showSerName = $value;
}
/**
* Get show percentage
*
* @return boolean
*/
public function getShowPercent() {
return $this->_showPercent;
}
/**
* Set show percentage
* Specifies that the percentage should be shown in data labels.
*
* @param boolean $value Show percentage
*/
public function setShowPercent($value) {
$this->_showPercent = $value;
}
/**
* Get show bubble size
*
* @return boolean
*/
public function getShowBubbleSize() {
return $this->_showBubbleSize;
}
/**
* Set show bubble size
* Specifies that the bubble size should be shown in data labels.
*
* @param boolean $value Show bubble size
*/
public function setShowBubbleSize($value) {
$this->_showBubbleSize = $value;
}
/**
* Get show leader lines
*
* @return boolean
*/
public function getShowLeaderLines() {
return $this->_showLeaderLines;
}
/**
* Set show leader lines
* Specifies that leader lines should be shown in data labels.
*
* @param boolean $value Show leader lines
*/
public function setShowLeaderLines($value) {
$this->_showLeaderLines = $value;
}
}

View File

@ -75,7 +75,7 @@ class PHPExcel_Reader_Excel2007_Chart
switch ($chartDetailsKey) {
case "plotArea":
$plotAreaLayout = $XaxisLable = $YaxisLable = null;
$plotSeries = array();
$plotSeries = $plotAttributes = array();
foreach($chartDetails as $chartDetailKey => $chartDetail) {
switch ($chartDetailKey) {
case "layout":
@ -102,14 +102,17 @@ class PHPExcel_Reader_Excel2007_Chart
$plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
$plotSer->setPlotDirection($barDirection);
$plotSeries[] = $plotSer;
$plotAttributes = self::_readChartAttributes($chartDetail);
break;
case "lineChart":
case "line3DChart":
$plotSeries[] = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
$plotAttributes = self::_readChartAttributes($chartDetail);
break;
case "areaChart":
case "area3DChart":
$plotSeries[] = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
$plotAttributes = self::_readChartAttributes($chartDetail);
break;
case "doughnutChart":
case "pieChart":
@ -118,24 +121,28 @@ class PHPExcel_Reader_Excel2007_Chart
$plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
$plotSer->setPlotStyle($explosion);
$plotSeries[] = $plotSer;
$plotAttributes = self::_readChartAttributes($chartDetail);
break;
case "scatterChart":
$scatterStyle = self::_getAttribute($chartDetail->scatterStyle, 'val', 'string');
$plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
$plotSer->setPlotStyle($scatterStyle);
$plotSeries[] = $plotSer;
$plotAttributes = self::_readChartAttributes($chartDetail);
break;
case "bubbleChart":
$bubbleScale = self::_getAttribute($chartDetail->bubbleScale, 'val', 'integer');
$plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
$plotSer->setPlotStyle($bubbleScale);
$plotSeries[] = $plotSer;
$plotAttributes = self::_readChartAttributes($chartDetail);
break;
case "radarChart":
$radarStyle = self::_getAttribute($chartDetail->radarStyle, 'val', 'string');
$plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
$plotSer->setPlotStyle($radarStyle);
$plotSeries[] = $plotSer;
$plotAttributes = self::_readChartAttributes($chartDetail);
break;
case "surfaceChart":
case "surface3DChart":
@ -143,13 +150,19 @@ class PHPExcel_Reader_Excel2007_Chart
$plotSer = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
$plotSer->setPlotStyle($wireFrame);
$plotSeries[] = $plotSer;
$plotAttributes = self::_readChartAttributes($chartDetail);
break;
case "stockChart":
$plotSeries[] = self::_chartDataSeries($chartDetail,$namespacesChartMeta,$chartDetailKey);
$plotAttributes = self::_readChartAttributes($plotAreaLayout);
break;
}
}
if ($plotAreaLayout == NULL) {
$plotAreaLayout = new PHPExcel_Chart_Layout();
}
$plotArea = new PHPExcel_Chart_PlotArea($plotAreaLayout,$plotSeries);
self::_setChartAttributes($plotAreaLayout,$plotAttributes);
break;
case "plotVisOnly":
$plotVisOnly = self::_getAttribute($chartDetails, 'val', 'string');
@ -183,7 +196,6 @@ class PHPExcel_Reader_Excel2007_Chart
}
}
}
$chart = new PHPExcel_Chart($chartName,$title,$legend,$plotArea,$plotVisOnly,$dispBlanksAs,$XaxisLabel,$YaxisLabel);
return $chart;
@ -225,7 +237,7 @@ class PHPExcel_Reader_Excel2007_Chart
}
$layout = array();
foreach($details as $detailKey => $detail) {
// echo $detailKey,' => ',self::_getAttribute($detail, 'val', 'string'),'<br />';
// echo $detailKey,' => ',self::_getAttribute($detail, 'val', 'string'),PHP_EOL;
$layout[$detailKey] = self::_getAttribute($detail, 'val', 'string');
}
return new PHPExcel_Chart_Layout($layout);
@ -306,13 +318,6 @@ class PHPExcel_Reader_Excel2007_Chart
$seriesData['pointCount'] = count($seriesData['dataValues']);
return new PHPExcel_Chart_DataSeriesValues('String',$seriesSource,$seriesData['formatCode'],$seriesData['pointCount'],$seriesData['dataValues'],$marker,$smoothLine);
} elseif (isset($seriesDetail->v)) {
$seriesData = array( 'formatCode' => '@',
'pointCount' => 1,
'dataValues' => array((string) $seriesDetail->v)
);
return new PHPExcel_Chart_DataSeriesValues('String',NULL,'@',1,$seriesData['dataValues'],$marker,$smoothLine);
}
return null;
} // function _chartDataSeriesValueSet()
@ -447,4 +452,62 @@ class PHPExcel_Reader_Excel2007_Chart
return $value;
}
private static function _readChartAttributes($chartDetail) {
$plotAttributes = array();
if (isset($chartDetail->dLbls)) {
if (isset($chartDetail->dLbls->howLegendKey)) {
$plotAttributes['showLegendKey'] = self::_getAttribute($chartDetail->dLbls->showLegendKey, 'val', 'string');
}
if (isset($chartDetail->dLbls->showVal)) {
$plotAttributes['showVal'] = self::_getAttribute($chartDetail->dLbls->showVal, 'val', 'string');
}
if (isset($chartDetail->dLbls->showCatName)) {
$plotAttributes['showCatName'] = self::_getAttribute($chartDetail->dLbls->showCatName, 'val', 'string');
}
if (isset($chartDetail->dLbls->showSerName)) {
$plotAttributes['showSerName'] = self::_getAttribute($chartDetail->dLbls->showSerName, 'val', 'string');
}
if (isset($chartDetail->dLbls->showPercent)) {
$plotAttributes['showPercent'] = self::_getAttribute($chartDetail->dLbls->showPercent, 'val', 'string');
}
if (isset($chartDetail->dLbls->showBubbleSize)) {
$plotAttributes['showBubbleSize'] = self::_getAttribute($chartDetail->dLbls->showBubbleSize, 'val', 'string');
}
if (isset($chartDetail->dLbls->showLeaderLines)) {
$plotAttributes['showLeaderLines'] = self::_getAttribute($chartDetail->dLbls->showLeaderLines, 'val', 'string');
}
}
return $plotAttributes;
}
private static function _setChartAttributes($plotArea,$plotAttributes)
{
foreach($plotAttributes as $plotAttributeKey => $plotAttributeValue) {
switch($plotAttributeKey) {
case 'showLegendKey' :
$plotArea->setShowLegendKey($plotAttributeValue);
break;
case 'showVal' :
$plotArea->setShowVal($plotAttributeValue);
break;
case 'showCatName' :
$plotArea->setShowCatName($plotAttributeValue);
break;
case 'showSerName' :
$plotArea->setShowSerName($plotAttributeValue);
break;
case 'showPercent' :
$plotArea->setShowPercent($plotAttributeValue);
break;
case 'showBubbleSize' :
$plotArea->setShowBubbleSize($plotAttributeValue);
break;
case 'showLeaderLines' :
$plotArea->setShowLeaderLines($plotAttributeValue);
break;
}
}
}
}

View File

@ -233,6 +233,7 @@ class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPa
$objWriter->startElement('c:plotArea');
$layout = $plotArea->getLayout();
$this->_writeLayout($layout, $objWriter);
$chartTypes = self::_getChartType($plotArea);
@ -262,7 +263,7 @@ class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPa
}
}
$this->_writeDataLbls($objWriter);
$this->_writeDataLbls($objWriter, $layout);
if ($chartType === PHPExcel_Chart_DataSeries::TYPE_LINECHART) {
// Line only, Line3D can't be smoothed
@ -353,38 +354,47 @@ class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPa
* Write Data Labels
*
* @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
* @param PHPExcel_Chart_Layout $chartLayout Chart layout
* @throws Exception
*/
private function _writeDataLbls($objWriter)
private function _writeDataLbls($objWriter, $chartLayout)
{
$objWriter->startElement('c:dLbls');
$objWriter->startElement('c:showLegendKey');
$objWriter->writeAttribute('val', 0);
$showLegendKey = (empty($chartLayout)) ? 0 : $chartLayout->getShowLegendKey();
$objWriter->writeAttribute('val', ((empty($showLegendKey)) ? 0 : 1) );
$objWriter->endElement();
$objWriter->startElement('c:showVal');
$objWriter->writeAttribute('val', 0);
$showVal = (empty($chartLayout)) ? 0 : $chartLayout->getShowVal();
$objWriter->writeAttribute('val', ((empty($showVal)) ? 0 : 1) );
$objWriter->endElement();
$objWriter->startElement('c:showCatName');
$objWriter->writeAttribute('val', 0);
$showCatName = (empty($chartLayout)) ? 0 : $chartLayout->getShowCatName();
$objWriter->writeAttribute('val', ((empty($showCatName)) ? 0 : 1) );
$objWriter->endElement();
$objWriter->startElement('c:showSerName');
$objWriter->writeAttribute('val', 0);
$showSerName = (empty($chartLayout)) ? 0 : $chartLayout->getShowSerName();
$objWriter->writeAttribute('val', ((empty($showSerName)) ? 0 : 1) );
$objWriter->endElement();
$objWriter->startElement('c:showPercent');
$objWriter->writeAttribute('val', 0);
$showPercent = (empty($chartLayout)) ? 0 : $chartLayout->getShowPercent();
$objWriter->writeAttribute('val', ((empty($showPercent)) ? 0 : 1) );
$objWriter->endElement();
$objWriter->startElement('c:showBubbleSize');
$objWriter->writeAttribute('val', 0);
$showBubbleSize = (empty($chartLayout)) ? 0 : $chartLayout->getShowBubbleSize();
$objWriter->writeAttribute('val', ((empty($showBubbleSize)) ? 0 : 1) );
$objWriter->endElement();
$objWriter->startElement('c:showLeaderLines');
$objWriter->writeAttribute('val', 1);
$showLeaderLines = (empty($chartLayout)) ? 1 : $chartLayout->getShowLeaderLines();
$objWriter->writeAttribute('val', ((empty($showLeaderLines)) ? 0 : 1) );
$objWriter->endElement();
$objWriter->endElement();