1521 lines
63 KiB
PHP
1521 lines
63 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
/**
|
||
|
|
* PHPExcel_Writer_Excel2007_Chart
|
||
|
|
*
|
||
|
|
* Copyright (c) 2006 - 2015 PHPExcel
|
||
|
|
*
|
||
|
|
* This library is free software; you can redistribute it and/or
|
||
|
|
* modify it under the terms of the GNU Lesser General Public
|
||
|
|
* License as published by the Free Software Foundation; either
|
||
|
|
* version 2.1 of the License, or (at your option) any later version.
|
||
|
|
*
|
||
|
|
* This library is distributed in the hope that it will be useful,
|
||
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
|
* Lesser General Public License for more details.
|
||
|
|
*
|
||
|
|
* You should have received a copy of the GNU Lesser General Public
|
||
|
|
* License along with this library; if not, write to the Free Software
|
||
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
|
|
*
|
||
|
|
* @category PHPExcel
|
||
|
|
* @package PHPExcel_Writer_Excel2007
|
||
|
|
* @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||
|
|
* @version ##VERSION##, ##DATE##
|
||
|
|
*/
|
||
|
|
class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPart
|
||
|
|
{
|
||
|
|
protected $calculateCellValues;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Write charts to XML format
|
||
|
|
*
|
||
|
|
* @param PHPExcel_Chart $pChart
|
||
|
|
*
|
||
|
|
* @return string XML Output
|
||
|
|
* @throws PHPExcel_Writer_Exception
|
||
|
|
*/
|
||
|
|
public function writeChart(PHPExcel_Chart $pChart = null, $calculateCellValues = true)
|
||
|
|
{
|
||
|
|
$this->calculateCellValues = $calculateCellValues;
|
||
|
|
|
||
|
|
// Create XML writer
|
||
|
|
$objWriter = null;
|
||
|
|
if ($this->getParentWriter()->getUseDiskCaching()) {
|
||
|
|
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
|
||
|
|
} else {
|
||
|
|
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
|
||
|
|
}
|
||
|
|
// Ensure that data series values are up-to-date before we save
|
||
|
|
if ($this->calculateCellValues) {
|
||
|
|
$pChart->refresh();
|
||
|
|
}
|
||
|
|
|
||
|
|
// XML header
|
||
|
|
$objWriter->startDocument('1.0', 'UTF-8', 'yes');
|
||
|
|
|
||
|
|
// c:chartSpace
|
||
|
|
$objWriter->startElement('c:chartSpace');
|
||
|
|
$objWriter->writeAttribute('xmlns:c', 'http://schemas.openxmlformats.org/drawingml/2006/chart');
|
||
|
|
$objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
|
||
|
|
$objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
|
||
|
|
|
||
|
|
$objWriter->startElement('c:date1904');
|
||
|
|
$objWriter->writeAttribute('val', 0);
|
||
|
|
$objWriter->endElement();
|
||
|
|
$objWriter->startElement('c:lang');
|
||
|
|
$objWriter->writeAttribute('val', "en-GB");
|
||
|
|
$objWriter->endElement();
|
||
|
|
$objWriter->startElement('c:roundedCorners');
|
||
|
|
$objWriter->writeAttribute('val', 0);
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$this->writeAlternateContent($objWriter);
|
||
|
|
|
||
|
|
$objWriter->startElement('c:chart');
|
||
|
|
|
||
|
|
$this->writeTitle($pChart->getTitle(), $objWriter);
|
||
|
|
|
||
|
|
$objWriter->startElement('c:autoTitleDeleted');
|
||
|
|
$objWriter->writeAttribute('val', 0);
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$this->writePlotArea($pChart->getPlotArea(), $pChart->getXAxisLabel(), $pChart->getYAxisLabel(), $objWriter, $pChart->getWorksheet(), $pChart->getChartAxisX(), $pChart->getChartAxisY(), $pChart->getMajorGridlines(), $pChart->getMinorGridlines());
|
||
|
|
|
||
|
|
$this->writeLegend($pChart->getLegend(), $objWriter);
|
||
|
|
|
||
|
|
$objWriter->startElement('c:plotVisOnly');
|
||
|
|
$objWriter->writeAttribute('val', 1);
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->startElement('c:dispBlanksAs');
|
||
|
|
$objWriter->writeAttribute('val', "gap");
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->startElement('c:showDLblsOverMax');
|
||
|
|
$objWriter->writeAttribute('val', 0);
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$this->writePrintSettings($objWriter);
|
||
|
|
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
// Return
|
||
|
|
return $objWriter->getData();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Write Chart Title
|
||
|
|
*
|
||
|
|
* @param PHPExcel_Chart_Title $title
|
||
|
|
* @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
|
||
|
|
*
|
||
|
|
* @throws PHPExcel_Writer_Exception
|
||
|
|
*/
|
||
|
|
private function writeTitle(PHPExcel_Chart_Title $title = null, $objWriter)
|
||
|
|
{
|
||
|
|
if (is_null($title)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$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)) && (count($caption) > 0)) {
|
||
|
|
$caption = $caption[0];
|
||
|
|
}
|
||
|
|
$this->getParentWriter()->getWriterPart('stringtable')->writeRichTextForCharts($objWriter, $caption, 'a');
|
||
|
|
|
||
|
|
$objWriter->endElement();
|
||
|
|
$objWriter->endElement();
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$this->writeLayout($title->getLayout(), $objWriter);
|
||
|
|
|
||
|
|
$objWriter->startElement('c:overlay');
|
||
|
|
$objWriter->writeAttribute('val', 0);
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->endElement();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Write Chart Legend
|
||
|
|
*
|
||
|
|
* @param PHPExcel_Chart_Legend $legend
|
||
|
|
* @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
|
||
|
|
*
|
||
|
|
* @throws PHPExcel_Writer_Exception
|
||
|
|
*/
|
||
|
|
private function writeLegend(PHPExcel_Chart_Legend $legend = null, $objWriter)
|
||
|
|
{
|
||
|
|
if (is_null($legend)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$objWriter->startElement('c:legend');
|
||
|
|
|
||
|
|
$objWriter->startElement('c:legendPos');
|
||
|
|
$objWriter->writeAttribute('val', $legend->getPosition());
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$this->writeLayout($legend->getLayout(), $objWriter);
|
||
|
|
|
||
|
|
$objWriter->startElement('c:overlay');
|
||
|
|
$objWriter->writeAttribute('val', ($legend->getOverlay()) ? '1' : '0');
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->startElement('c:txPr');
|
||
|
|
$objWriter->startElement('a:bodyPr');
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->startElement('a:lstStyle');
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->startElement('a:p');
|
||
|
|
$objWriter->startElement('a:pPr');
|
||
|
|
$objWriter->writeAttribute('rtl', 0);
|
||
|
|
|
||
|
|
$objWriter->startElement('a:defRPr');
|
||
|
|
$objWriter->endElement();
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->startElement('a:endParaRPr');
|
||
|
|
$objWriter->writeAttribute('lang', "en-US");
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->endElement();
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->endElement();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Write Chart Plot Area
|
||
|
|
*
|
||
|
|
* @param PHPExcel_Chart_PlotArea $plotArea
|
||
|
|
* @param PHPExcel_Chart_Title $xAxisLabel
|
||
|
|
* @param PHPExcel_Chart_Title $yAxisLabel
|
||
|
|
* @param PHPExcel_Chart_Axis $xAxis
|
||
|
|
* @param PHPExcel_Chart_Axis $yAxis
|
||
|
|
* @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
|
||
|
|
*
|
||
|
|
* @throws PHPExcel_Writer_Exception
|
||
|
|
*/
|
||
|
|
private function writePlotArea(PHPExcel_Chart_PlotArea $plotArea, PHPExcel_Chart_Title $xAxisLabel = null, PHPExcel_Chart_Title $yAxisLabel = null, $objWriter, PHPExcel_Worksheet $pSheet, PHPExcel_Chart_Axis $xAxis, PHPExcel_Chart_Axis $yAxis, PHPExcel_Chart_GridLines $majorGridlines, PHPExcel_Chart_GridLines $minorGridlines)
|
||
|
|
{
|
||
|
|
if (is_null($plotArea)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$id1 = $id2 = 0;
|
||
|
|
$this->_seriesIndex = 0;
|
||
|
|
$objWriter->startElement('c:plotArea');
|
||
|
|
|
||
|
|
$layout = $plotArea->getLayout();
|
||
|
|
|
||
|
|
$this->writeLayout($layout, $objWriter);
|
||
|
|
|
||
|
|
$chartTypes = self::getChartType($plotArea);
|
||
|
|
$catIsMultiLevelSeries = $valIsMultiLevelSeries = false;
|
||
|
|
$plotGroupingType = '';
|
||
|
|
foreach ($chartTypes as $chartType) {
|
||
|
|
$objWriter->startElement('c:' . $chartType);
|
||
|
|
|
||
|
|
$groupCount = $plotArea->getPlotGroupCount();
|
||
|
|
for ($i = 0; $i < $groupCount; ++$i) {
|
||
|
|
$plotGroup = $plotArea->getPlotGroupByIndex($i);
|
||
|
|
$groupType = $plotGroup->getPlotType();
|
||
|
|
if ($groupType == $chartType) {
|
||
|
|
$plotStyle = $plotGroup->getPlotStyle();
|
||
|
|
if ($groupType === PHPExcel_Chart_DataSeries::TYPE_RADARCHART) {
|
||
|
|
$objWriter->startElement('c:radarStyle');
|
||
|
|
$objWriter->writeAttribute('val', $plotStyle);
|
||
|
|
$objWriter->endElement();
|
||
|
|
} elseif ($groupType === PHPExcel_Chart_DataSeries::TYPE_SCATTERCHART) {
|
||
|
|
$objWriter->startElement('c:scatterStyle');
|
||
|
|
$objWriter->writeAttribute('val', $plotStyle);
|
||
|
|
$objWriter->endElement();
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->writePlotGroup($plotGroup, $chartType, $objWriter, $catIsMultiLevelSeries, $valIsMultiLevelSeries, $plotGroupingType, $pSheet);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->writeDataLabels($objWriter, $layout);
|
||
|
|
|
||
|
|
if ($chartType === PHPExcel_Chart_DataSeries::TYPE_LINECHART) {
|
||
|
|
// Line only, Line3D can't be smoothed
|
||
|
|
|
||
|
|
$objWriter->startElement('c:smooth');
|
||
|
|
$objWriter->writeAttribute('val', (integer) $plotGroup->getSmoothLine());
|
||
|
|
$objWriter->endElement();
|
||
|
|
} elseif (($chartType === PHPExcel_Chart_DataSeries::TYPE_BARCHART) ||($chartType === PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D)) {
|
||
|
|
$objWriter->startElement('c:gapWidth');
|
||
|
|
$objWriter->writeAttribute('val', 150);
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
if ($plotGroupingType == 'percentStacked' || $plotGroupingType == 'stacked') {
|
||
|
|
$objWriter->startElement('c:overlap');
|
||
|
|
$objWriter->writeAttribute('val', 100);
|
||
|
|
$objWriter->endElement();
|
||
|
|
}
|
||
|
|
} elseif ($chartType === PHPExcel_Chart_DataSeries::TYPE_BUBBLECHART) {
|
||
|
|
$objWriter->startElement('c:bubbleScale');
|
||
|
|
$objWriter->writeAttribute('val', 25);
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->startElement('c:showNegBubbles');
|
||
|
|
$objWriter->writeAttribute('val', 0);
|
||
|
|
$objWriter->endElement();
|
||
|
|
} elseif ($chartType === PHPExcel_Chart_DataSeries::TYPE_STOCKCHART) {
|
||
|
|
$objWriter->startElement('c:hiLowLines');
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->startElement('c:upDownBars');
|
||
|
|
|
||
|
|
$objWriter->startElement('c:gapWidth');
|
||
|
|
$objWriter->writeAttribute('val', 300);
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->startElement('c:upBars');
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->startElement('c:downBars');
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
$objWriter->endElement();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Generate 2 unique numbers to use for axId values
|
||
|
|
// $id1 = $id2 = rand(10000000,99999999);
|
||
|
|
// do {
|
||
|
|
// $id2 = rand(10000000,99999999);
|
||
|
|
// } while ($id1 == $id2);
|
||
|
|
$id1 = '75091328';
|
||
|
|
$id2 = '75089408';
|
||
|
|
|
||
|
|
if (($chartType !== PHPExcel_Chart_DataSeries::TYPE_PIECHART) && ($chartType !== PHPExcel_Chart_DataSeries::TYPE_PIECHART_3D) && ($chartType !== PHPExcel_Chart_DataSeries::TYPE_DONUTCHART)) {
|
||
|
|
$objWriter->startElement('c:axId');
|
||
|
|
$objWriter->writeAttribute('val', $id1);
|
||
|
|
$objWriter->endElement();
|
||
|
|
$objWriter->startElement('c:axId');
|
||
|
|
$objWriter->writeAttribute('val', $id2);
|
||
|
|
$objWriter->endElement();
|
||
|
|
} else {
|
||
|
|
$objWriter->startElement('c:firstSliceAng');
|
||
|
|
$objWriter->writeAttribute('val', 0);
|
||
|
|
$objWriter->endElement();
|
||
|
|
|
||
|
|
if ($chartType === PHPExcel_Chart_DataSeries::TYPE_DONUTCHART) {
|
||
|
|
$objWriter->startElement('c:holeSize');
|
||
|
|
$objWriter->writeAttribute('val', 50);
|
||
|
|
$objWriter->endElement();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$objWriter->endElement();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (($chartType !== PHPExcel_Chart_DataSeries::TYPE_PIECHART) && ($chartType !== PHPExcel_Chart_DataSeries::TYPE_PIECHART_3D) && ($chartType !== PHPExcel_Chart_DataSeries::TYPE_DONUTCHART)) {
|
||
|
|
if ($chartType === PHPExcel_Chart_DataSeries::TYPE_BUBBLECHART) {
|
||
|
|
$this->writeValueAxis($objWriter, $plotArea, $xAxisLabel, $chartType, $id1, $id2, $catIsMultiLevelSeries, $xAxis, $yAxis, $majorGridlines, $minorGridlines);
|
||
|
|
} else {
|
||
|
|
$this->writeCategoryAxis($objWriter, $plotArea, $xAxisLabel, $chartType, $id1, $id2, $catIsMultiLevelSeries, $xAxis, $yAxis);
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->writeValueAxis($objWriter, $plotArea, $yAxisLabel, $chartType, $id1, $id2, $valIsMultiLevelSeries, $xAxis, $yAxis, $majorGridlines, $minorGridlines);
|
||
|
|
}
|
||
|
|
|
||
|
|
$objWriter->endElement();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Write Data Labels
|
||
|
|
*
|
||
|
|
* @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
|
||
|
|
* @param PHPExcel_Chart_Layout $chartLayout Chart layout
|
||
|
|
*
|
||
|
|
* @throws PHPExcel_Writer_Exception
|
||
|
|
*/
|
||
|
|
private function writeDataLabels($objWriter, $chartLayout)
|
||
|
|
{
|
||
|
|
$objWriter->startElement('c:dLbls');
|
||
|
|
|
||
|
|
$objWriter->startElement('c:showLegendKey');
|
||
|
|
$showLegendKey = (empty |