From fac2858b1d133137d1baeb00eb1a2994c3091b98 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Thu, 23 Feb 2012 17:20:25 +0000 Subject: [PATCH] Fixes to rich text in charts git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@86842 2327b42d-5241-43d6-9e2a-de5ac946f064 --- Classes/PHPExcel/Writer/Excel2007/Chart.php | 146 +++++++++++------- .../PHPExcel/Writer/Excel2007/StringTable.php | 66 ++++++++ 2 files changed, 155 insertions(+), 57 deletions(-) diff --git a/Classes/PHPExcel/Writer/Excel2007/Chart.php b/Classes/PHPExcel/Writer/Excel2007/Chart.php index 2ddfb092..60a82bc2 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Chart.php +++ b/Classes/PHPExcel/Writer/Excel2007/Chart.php @@ -117,12 +117,19 @@ class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPa $objWriter->startElement('c:title'); $objWriter->startElement('c:tx'); $objWriter->startElement('c:rich'); + + $objWriter->startElement('a:bodyPr'); + $objWriter->endElement(); + + $objWriter->startElement('a:lstStyle'); + $objWriter->endElement(); + $objWriter->startElement('a:p'); $caption = $title->getCaption(); if (is_array($caption)) $caption = $caption[0]; - $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter, $caption, 'a'); + $this->getParentWriter()->getWriterPart('stringtable')->writeRichTextForCharts($objWriter, $caption, 'a'); $objWriter->endElement(); $objWriter->endElement(); @@ -214,14 +221,14 @@ class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPa $this->_writeDataLbls($objWriter); if (($groupType === PHPExcel_Chart_DataSeries::TYPE_LINECHART) || - ($groupType === PHPExcel_Chart_DataSeries::TYPE_LINECHART_3D) || - ($groupType === PHPExcel_Chart_DataSeries::TYPE_AREACHART) || - ($groupType === PHPExcel_Chart_DataSeries::TYPE_AREACHART_3D)) { + ($groupType === PHPExcel_Chart_DataSeries::TYPE_LINECHART_3D)) { + $objWriter->startElement('c:smooth'); $objWriter->writeAttribute('val', (integer) $plotGroup->getSmoothLine() ); $objWriter->endElement(); } elseif (($groupType === PHPExcel_Chart_DataSeries::TYPE_BARCHART) || ($groupType === PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D)) { + $objWriter->startElement('c:gapWidth'); $objWriter->writeAttribute('val', 150 ); $objWriter->endElement(); @@ -230,6 +237,7 @@ class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPa if (($groupType !== PHPExcel_Chart_DataSeries::TYPE_PIECHART) && ($groupType !== PHPExcel_Chart_DataSeries::TYPE_PIECHART_3D) && ($groupType !== PHPExcel_Chart_DataSeries::TYPE_DONUTCHART)) { + // Generate 2 unique numbers to use for axId values $id1 = $id2 = rand(10000000,99999999); do { @@ -317,6 +325,44 @@ class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPa $objWriter->writeAttribute('val', "b"); $objWriter->endElement(); + if (!is_null($xAxisLabel)) { + $objWriter->startElement('c:title'); + $objWriter->startElement('c:tx'); + $objWriter->startElement('c:rich'); + + $objWriter->startElement('a:bodyPr'); + $objWriter->endElement(); + + $objWriter->startElement('a:lstStyle'); + $objWriter->endElement(); + + $objWriter->startElement('a:p'); + $objWriter->startElement('a:r'); + + $caption = $xAxisLabel->getCaption(); + if (is_array($caption)) + $caption = $caption[0]; + $objWriter->startElement('a:t'); + $objWriter->writeAttribute('xml:space', 'preserve'); + $objWriter->writeRawData(PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $caption )); + $objWriter->endElement(); + + $objWriter->endElement(); + $objWriter->endElement(); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->startElement('c:overlay'); + $objWriter->writeAttribute('val', 0); + $objWriter->endElement(); + + $layout = $xAxisLabel->getLayout(); + $this->_writeLayout($layout, $objWriter); + + $objWriter->endElement(); + + } + $objWriter->startElement('c:numFmt'); $objWriter->writeAttribute('formatCode', "General"); $objWriter->writeAttribute('sourceLinked', 1); @@ -334,31 +380,6 @@ class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPa $objWriter->writeAttribute('val', "nextTo"); $objWriter->endElement(); - if (!is_null($xAxisLabel)) { - $objWriter->startElement('c:title'); - $objWriter->startElement('c:tx'); - $objWriter->startElement('c:rich'); - $objWriter->startElement('a:p'); - - $caption = $xAxisLabel->getCaption(); - if (is_array($caption)) - $caption = $caption[0]; - $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter, $caption, 'a'); - - $objWriter->endElement(); - $objWriter->endElement(); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->startElement('c:overlay'); - $objWriter->writeAttribute('val', 0); - $objWriter->endElement(); - - $layout = $xAxisLabel->getLayout(); - $this->_writeLayout($layout, $objWriter); - - } - if ($id2 > 0) { $objWriter->startElement('c:crossAx'); $objWriter->writeAttribute('val', $id2); @@ -418,6 +439,43 @@ class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPa $objWriter->startElement('c:majorGridlines'); $objWriter->endElement(); + if (!is_null($yAxisLabel)) { + $objWriter->startElement('c:title'); + $objWriter->startElement('c:tx'); + $objWriter->startElement('c:rich'); + + $objWriter->startElement('a:bodyPr'); + $objWriter->endElement(); + + $objWriter->startElement('a:lstStyle'); + $objWriter->endElement(); + + $objWriter->startElement('a:p'); + $objWriter->startElement('a:r'); + + $caption = $yAxisLabel->getCaption(); + if (is_array($caption)) + $caption = $caption[0]; + $objWriter->startElement('a:t'); + $objWriter->writeAttribute('xml:space', 'preserve'); + $objWriter->writeRawData(PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $caption )); + $objWriter->endElement(); + + $objWriter->endElement(); + $objWriter->endElement(); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->startElement('c:overlay'); + $objWriter->writeAttribute('val', 0); + $objWriter->endElement(); + + $layout = $yAxisLabel->getLayout(); + $this->_writeLayout($layout, $objWriter); + + $objWriter->endElement(); + } + $objWriter->startElement('c:numFmt'); $objWriter->writeAttribute('formatCode', "General"); $objWriter->writeAttribute('sourceLinked', 1); @@ -435,31 +493,6 @@ class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPa $objWriter->writeAttribute('val', "nextTo"); $objWriter->endElement(); - if (!is_null($yAxisLabel)) { - $objWriter->startElement('c:title'); - $objWriter->startElement('c:tx'); - $objWriter->startElement('c:rich'); - $objWriter->startElement('a:p'); - - $caption = $yAxisLabel->getCaption(); - if (is_array($caption)) - $caption = $caption[0]; - $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter, $caption, 'a'); - - $objWriter->endElement(); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->startElement('c:overlay'); - $objWriter->writeAttribute('val', 0); - $objWriter->endElement(); - - $layout = $yAxisLabel->getLayout(); - $this->_writeLayout($layout, $objWriter); - - $objWriter->endElement(); - } - if ($id1 > 0) { $objWriter->startElement('c:crossAx'); $objWriter->writeAttribute('val', $id2); @@ -581,9 +614,8 @@ class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPa $objWriter->endElement(); } - if (($groupType !== PHPExcel_Chart_DataSeries::TYPE_PIECHART) && - ($groupType !== PHPExcel_Chart_DataSeries::TYPE_PIECHART_3D) && - ($groupType !== PHPExcel_Chart_DataSeries::TYPE_DONUTCHART)) { + if (($groupType === PHPExcel_Chart_DataSeries::TYPE_BARCHART) || + ($groupType === PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D)) { $objWriter->startElement('c:invertIfNegative'); $objWriter->writeAttribute('val', 0); diff --git a/Classes/PHPExcel/Writer/Excel2007/StringTable.php b/Classes/PHPExcel/Writer/Excel2007/StringTable.php index 8fe36abc..d517147c 100644 --- a/Classes/PHPExcel/Writer/Excel2007/StringTable.php +++ b/Classes/PHPExcel/Writer/Excel2007/StringTable.php @@ -220,6 +220,72 @@ class PHPExcel_Writer_Excel2007_StringTable extends PHPExcel_Writer_Excel2007_Wr } } + /** + * Write Rich Text + * + * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer + * @param PHPExcel_RichText $pRichText Rich text + * @throws Exception + */ + public function writeRichTextForCharts(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_RichText $pRichText = null, $prefix=NULL) + { + if ($prefix !== NULL) + $prefix .= ':'; + // Loop through rich text elements + $elements = $pRichText->getRichTextElements(); + foreach ($elements as $element) { + // r + $objWriter->startElement($prefix.'r'); + + // rPr + $objWriter->startElement($prefix.'rPr'); + + // Bold + $objWriter->writeAttribute('b', ($element->getFont()->getBold() ? 1 : 0)); + // Italic + $objWriter->writeAttribute('i', ($element->getFont()->getItalic() ? 1 : 0)); + // Underline + $underlineType = $element->getFont()->getUnderline(); + switch($underlineType) { + case 'single' : + $underlineType = 'sng'; + break; + case 'double' : + $underlineType = 'dbl'; + break; + } + $objWriter->writeAttribute('u', $underlineType); + // Strikethrough + $objWriter->writeAttribute('strike', ($element->getFont()->getStrikethrough() ? 'sngStrike' : 'noStrike')); + + // rFont + $objWriter->startElement($prefix.'latin'); + $objWriter->writeAttribute('typeface', $element->getFont()->getName()); + $objWriter->endElement(); + + // Superscript / subscript +// if ($element->getFont()->getSuperScript() || $element->getFont()->getSubScript()) { +// $objWriter->startElement($prefix.'vertAlign'); +// if ($element->getFont()->getSuperScript()) { +// $objWriter->writeAttribute('val', 'superscript'); +// } else if ($element->getFont()->getSubScript()) { +// $objWriter->writeAttribute('val', 'subscript'); +// } +// $objWriter->endElement(); +// } +// + $objWriter->endElement(); + + // t + $objWriter->startElement($prefix.'t'); + $objWriter->writeAttribute('xml:space', 'preserve'); + $objWriter->writeRawData(PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $element->getText() )); + $objWriter->endElement(); + + $objWriter->endElement(); + } + } + /** * Flip string table (for index searching) *