diff --git a/CHANGELOG.md b/CHANGELOG.md index b69363744..8512d17ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ ### Features - ODPresentation & PowerPoint2007 Writer : Add support for Comment - @Progi1984 GH-116 - ODPresentation & PowerPoint2007 Writer : Thumbnail of the presentation - @Progi1984 GH-125 +- ODPresentation & PowerPoint2007 Writer : Add support for Gridlines in Chart - @Progi1984 GH-129 - ODPresentation & PowerPoint2007 Writer : Marker of Series in Line & Scatter chart is customizable - @Progi1984 GH-169 - ODPresentation & PowerPoint2007 Writer : Outline of Series in Line & Scatter chart is customizable - @Progi1984 GH-169 - PowerPoint2007 & Serialized Writer : Support for Zip Adapter - @Progi1984 GH-176 diff --git a/docs/shapes_chart.rst b/docs/shapes_chart.rst index f20116e1a..8b0d3f999 100644 --- a/docs/shapes_chart.rst +++ b/docs/shapes_chart.rst @@ -14,6 +14,26 @@ Example: Parts ----- +Axis +^^^^ + +You can define gridlines (minor and major) for each axis (X & Y). +For each gridline, you can custom the width (in points), the fill type and the fill color. + +.. code-block:: php + + use \PhpOffice\PhpPresentation\Shape\Chart\Gridlines; + + $oLine = new Line(); + + $oGridLines = new Gridlines(); + $oGridLines->getOutline()->setWidth(10); + $oGridLines->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE)); + + $oShape = $oSlide->createChartShape(); + $oShape->getPlotArea()->setType($oLine); + $oShape->getPlotArea()->getAxisX()->setMajorGridlines($oGridLines); + Title ^^^^^ @@ -22,8 +42,8 @@ For hiding it, you define its visibility to false. .. code-block:: php - $chartShape = $slide->createChartShape(); $oLine = new Line(); + $oShape = $slide->createChartShape(); $oShape->getPlotArea()->setType($oLine); // Hide the title $oShape->getTitle()->setVisible(false); diff --git a/samples/Sample_05_Chart_Line.php b/samples/Sample_05_Chart_Line.php index d6a7d1151..bac621988 100644 --- a/samples/Sample_05_Chart_Line.php +++ b/samples/Sample_05_Chart_Line.php @@ -125,6 +125,33 @@ $shape2->getPlotArea()->getAxisY()->setFormatCode('#,##0'); $currentSlide->addShape($shape2); + +// Create templated slide +echo EOL . date('H:i:s') . ' Create templated slide' . EOL; +$currentSlide = createTemplatedSlide($objPHPPresentation); + +// Create a line chart (that should be inserted in a shape) +echo date('H:i:s') . ' Create a line chart (that should be inserted in a chart shape)' . EOL; +$lineChart3 = clone $lineChart; + +$oGridLines1 = new \PhpOffice\PhpPresentation\Shape\Chart\Gridlines(); +$oGridLines1->getOutline()->setWidth(10); +$oGridLines1->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE)); + +$oGridLines2 = new \PhpOffice\PhpPresentation\Shape\Chart\Gridlines(); +$oGridLines2->getOutline()->setWidth(1); +$oGridLines2->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_DARKGREEN)); + +// Create a shape (chart) +echo date('H:i:s') . ' Create a shape (chart3)' . EOL; +echo date('H:i:s') . ' Feature : Gridlines' . EOL; +$shape3 = clone $shape; +$shape3->setName('Shape 3'); +$shape3->getTitle()->setText('Chart with Gridlines'); +$shape3->getPlotArea()->setType($lineChart3); +$shape3->getPlotArea()->getAxisX()->setMajorGridlines($oGridLines1); +$shape3->getPlotArea()->getAxisY()->setMinorGridlines($oGridLines2); +$currentSlide->addShape($shape3); // Save file echo EOL . write($objPHPPresentation, basename(__FILE__, '.php'), $writers); diff --git a/samples/results/.gitkeep b/samples/results/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/PhpPresentation/Shape/Chart/Axis.php b/src/PhpPresentation/Shape/Chart/Axis.php index 33d7ec680..48cdf43be 100644 --- a/src/PhpPresentation/Shape/Chart/Axis.php +++ b/src/PhpPresentation/Shape/Chart/Axis.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpPresentation\Shape\Chart; use PhpOffice\PhpPresentation\ComparableInterface; +use PhpOffice\PhpPresentation\Shape\Chart\Gridlines; use PhpOffice\PhpPresentation\Style\Font; /** @@ -25,6 +26,9 @@ */ class Axis implements ComparableInterface { + const AXIS_X = 'x'; + const AXIS_Y = 'y'; + /** * Title * @@ -46,6 +50,16 @@ class Axis implements ComparableInterface */ private $font; + /** + * @var Gridlines + */ + protected $majorGridlines; + + /** + * @var Gridlines + */ + protected $minorGridlines; + /** * Create a new \PhpOffice\PhpPresentation\Shape\Chart\Axis instance * @@ -126,6 +140,42 @@ public function setFormatCode($value = '') return $this; } + /** + * @return Gridlines + */ + public function getMajorGridlines() + { + return $this->majorGridlines; + } + + /** + * @param Gridlines $majorGridlines + * @return Axis + */ + public function setMajorGridlines(Gridlines $majorGridlines) + { + $this->majorGridlines = $majorGridlines; + return $this; + } + + /** + * @return Gridlines + */ + public function getMinorGridlines() + { + return $this->minorGridlines; + } + + /** + * @param Gridlines $minorGridlines + * @return Axis + */ + public function setMinorGridlines(Gridlines $minorGridlines) + { + $this->minorGridlines = $minorGridlines; + return $this; + } + /** * Get hash code * diff --git a/src/PhpPresentation/Shape/Chart/Gridlines.php b/src/PhpPresentation/Shape/Chart/Gridlines.php new file mode 100644 index 000000000..4040a5a4b --- /dev/null +++ b/src/PhpPresentation/Shape/Chart/Gridlines.php @@ -0,0 +1,36 @@ +outline = new Outline(); + } + + /** + * @return Outline + */ + public function getOutline() + { + return $this->outline; + } + + /** + * @param Outline $outline + * @return Gridlines + */ + public function setOutline(Outline $outline) + { + $this->outline = $outline; + return $this; + } +} diff --git a/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php b/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php index acc253c82..2ac8181c3 100644 --- a/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php +++ b/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php @@ -252,20 +252,29 @@ private function writeAxis(Chart $chart) $this->xmlContent->writeAttribute('chart:name', 'primary-x'); $this->xmlContent->writeAttribute('chartooo:axis-type', 'text'); $this->xmlContent->writeAttribute('chart:style-name', 'styleAxisX'); - // chart:categories + // chart:axis > chart:categories $this->xmlContent->startElement('chart:categories'); $this->xmlContent->writeAttribute('table:cell-range-address', 'table-local.$A$2:.$A$'.($this->numData+1)); - // > chart:categories $this->xmlContent->endElement(); - // > chart:axis + // chart:axis > chart:grid + $this->writeGridline($chart->getPlotArea()->getAxisX()->getMajorGridlines(), 'styleAxisXGridlinesMajor', 'major'); + // chart:axis > chart:grid + $this->writeGridline($chart->getPlotArea()->getAxisX()->getMinorGridlines(), 'styleAxisXGridlinesMinor', 'minor'); + // ##chart:axis $this->xmlContent->endElement(); + // chart:axis $this->xmlContent->startElement('chart:axis'); $this->xmlContent->writeAttribute('chart:dimension', 'y'); $this->xmlContent->writeAttribute('chart:name', 'primary-y'); $this->xmlContent->writeAttribute('chart:style-name', 'styleAxisY'); - // > chart:axis + // chart:axis > chart:grid + $this->writeGridline($chart->getPlotArea()->getAxisY()->getMajorGridlines(), 'styleAxisYGridlinesMajor', 'major'); + // chart:axis > chart:grid + $this->writeGridline($chart->getPlotArea()->getAxisY()->getMinorGridlines(), 'styleAxisYGridlinesMinor', 'minor'); + // ##chart:axis $this->xmlContent->endElement(); + if ($chartType instanceof Bar3D || $chartType instanceof Pie3D) { // chart:axis $this->xmlContent->startElement('chart:axis'); @@ -275,21 +284,33 @@ private function writeAxis(Chart $chart) $this->xmlContent->endElement(); } } + + protected function writeGridline($oGridlines, $styleName, $chartClass) + { + if (!($oGridlines instanceof Chart\Gridlines)) { + return ; + } + + $this->xmlContent->startElement('chart:grid'); + $this->xmlContent->writeAttribute('chart:style-name', $styleName); + $this->xmlContent->writeAttribute('chart:class', $chartClass); + $this->xmlContent->endElement(); + } /** * @param Chart $chart * @todo Set function in \PhpPresentation\Shape\Chart\Axis for defining width and color of the axis */ - private function writeAxisStyle(Chart $chart) + protected function writeAxisStyle(Chart $chart) { $chartType = $chart->getPlotArea()->getType(); - + // AxisX // style:style $this->xmlContent->startElement('style:style'); $this->xmlContent->writeAttribute('style:name', 'styleAxisX'); $this->xmlContent->writeAttribute('style:family', 'chart'); - // style:chart-properties + // style:style > style:chart-properties $this->xmlContent->startElement('style:chart-properties'); $this->xmlContent->writeAttribute('chart:display-label', 'true'); $this->xmlContent->writeAttribute('chart:tick-marks-major-inner', 'false'); @@ -297,31 +318,34 @@ private function writeAxisStyle(Chart $chart) if ($chartType instanceof AbstractTypePie) { $this->xmlContent->writeAttribute('chart:reverse-direction', 'true'); } - // > style:chart-properties $this->xmlContent->endElement(); - // style:text-properties + // style:style > style:text-properties $this->xmlContent->startElement('style:text-properties'); $this->xmlContent->writeAttribute('fo:color', '#'.$chart->getPlotArea()->getAxisX()->getFont()->getColor()->getRGB()); $this->xmlContent->writeAttribute('fo:font-family', $chart->getPlotArea()->getAxisX()->getFont()->getName()); $this->xmlContent->writeAttribute('fo:font-size', $chart->getPlotArea()->getAxisX()->getFont()->getSize().'pt'); $this->xmlContent->writeAttribute('fo:font-style', $chart->getPlotArea()->getAxisX()->getFont()->isItalic() ? 'italic' : 'normal'); - // > style:text-properties $this->xmlContent->endElement(); - // style:graphic-properties + // style:style > style:graphic-properties $this->xmlContent->startElement('style:graphic-properties'); $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm'); $this->xmlContent->writeAttribute('svg:stroke-color', '#878787'); - // > style:graphic-properties $this->xmlContent->endElement(); - // > style:style + // ##style:style $this->xmlContent->endElement(); + + // AxisX GridLines Major + $this->writeGridlineStyle($chart->getPlotArea()->getAxisX()->getMajorGridlines(), 'styleAxisXGridlinesMajor'); + + // AxisX GridLines Minor + $this->writeGridlineStyle($chart->getPlotArea()->getAxisX()->getMinorGridlines(), 'styleAxisXGridlinesMinor'); // AxisY // style:style $this->xmlContent->startElement('style:style'); $this->xmlContent->writeAttribute('style:name', 'styleAxisY'); $this->xmlContent->writeAttribute('style:family', 'chart'); - // style:chart-properties + // style:style > style:chart-properties $this->xmlContent->startElement('style:chart-properties'); $this->xmlContent->writeAttribute('chart:display-label', 'true'); $this->xmlContent->writeAttribute('chart:tick-marks-major-inner', 'false'); @@ -329,23 +353,48 @@ private function writeAxisStyle(Chart $chart) if ($chartType instanceof AbstractTypePie) { $this->xmlContent->writeAttribute('chart:reverse-direction', 'true'); } - // > style:chart-properties $this->xmlContent->endElement(); - // style:text-properties + // style:style > style:text-properties $this->xmlContent->startElement('style:text-properties'); $this->xmlContent->writeAttribute('fo:color', '#'.$chart->getPlotArea()->getAxisY()->getFont()->getColor()->getRGB()); $this->xmlContent->writeAttribute('fo:font-family', $chart->getPlotArea()->getAxisY()->getFont()->getName()); $this->xmlContent->writeAttribute('fo:font-size', $chart->getPlotArea()->getAxisY()->getFont()->getSize().'pt'); $this->xmlContent->writeAttribute('fo:font-style', $chart->getPlotArea()->getAxisY()->getFont()->isItalic() ? 'italic' : 'normal'); - // > style:text-properties $this->xmlContent->endElement(); // style:graphic-properties $this->xmlContent->startElement('style:graphic-properties'); $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm'); $this->xmlContent->writeAttribute('svg:stroke-color', '#878787'); - // > style:graphic-properties $this->xmlContent->endElement(); - // > style:style + // ## style:style + $this->xmlContent->endElement(); + + // AxisY GridLines Major + $this->writeGridlineStyle($chart->getPlotArea()->getAxisY()->getMajorGridlines(), 'styleAxisYGridlinesMajor'); + + // AxisY GridLines Minor + $this->writeGridlineStyle($chart->getPlotArea()->getAxisY()->getMinorGridlines(), 'styleAxisYGridlinesMinor'); + } + + /** + * @param Chart\Gridlines $oGridlines + * @param string $styleName + */ + protected function writeGridlineStyle($oGridlines, $styleName) + { + if (!($oGridlines instanceof Chart\Gridlines)) { + return; + } + // style:style + $this->xmlContent->startElement('style:style'); + $this->xmlContent->writeAttribute('style:name', $styleName); + $this->xmlContent->writeAttribute('style:family', 'chart'); + // style:style > style:graphic-properties + $this->xmlContent->startElement('style:graphic-properties'); + $this->xmlContent->writeAttribute('svg:stroke-width', number_format(CommonDrawing::pointsToCentimeters($oGridlines->getOutline()->getWidth()), 2, '.', '').'cm'); + $this->xmlContent->writeAttribute('svg:stroke-color', '#'.$oGridlines->getOutline()->getFill()->getStartColor()->getRGB()); + $this->xmlContent->endElement(); + // ##style:style $this->xmlContent->endElement(); } diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php index f5fcf062c..3fcbf0078 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php @@ -6,6 +6,7 @@ use PhpOffice\Common\XMLWriter; use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Shape\Chart; +use PhpOffice\PhpPresentation\Shape\Chart\Gridlines; use PhpOffice\PhpPresentation\Shape\Chart\Legend; use PhpOffice\PhpPresentation\Shape\Chart\PlotArea; use PhpOffice\PhpPresentation\Shape\Chart\Title; @@ -526,281 +527,12 @@ protected function writePlotArea(XMLWriter $objWriter, PlotArea $subject, Chart // Write X axis? if ($chartType->hasAxisX()) { - // c:catAx (Axis X) - $objWriter->startElement('c:catAx'); - - // c:axId - $objWriter->startElement('c:axId'); - $objWriter->writeAttribute('val', '52743552'); - $objWriter->endElement(); - - // c:scaling - $objWriter->startElement('c:scaling'); - - // c:orientation - $objWriter->startElement('c:orientation'); - $objWriter->writeAttribute('val', 'minMax'); - $objWriter->endElement(); - - $objWriter->endElement(); - - // c:axPos - $objWriter->startElement('c:axPos'); - $objWriter->writeAttribute('val', 'b'); - $objWriter->endElement(); - - // c:numFmt - $objWriter->startElement('c:numFmt'); - $objWriter->writeAttribute('formatCode', $subject->getAxisX()->getFormatCode()); - $objWriter->writeAttribute('sourceLinked', '0'); - $objWriter->endElement(); - - // c:majorTickMark - $objWriter->startElement('c:majorTickMark'); - $objWriter->writeAttribute('val', 'none'); - $objWriter->endElement(); - - // c:tickLblPos - $objWriter->startElement('c:tickLblPos'); - $objWriter->writeAttribute('val', 'nextTo'); - $objWriter->endElement(); - - // c:txPr - $objWriter->startElement('c:txPr'); - - // a:bodyPr - $objWriter->writeElement('a:bodyPr', null); - - // a:lstStyle - $objWriter->writeElement('a:lstStyle', null); - - // a:p - $objWriter->startElement('a:p'); - - // a:pPr - $objWriter->startElement('a:pPr'); - - // a:defRPr - $objWriter->startElement('a:defRPr'); - - $objWriter->writeAttribute('b', ($subject->getAxisX()->getFont()->isBold() ? 'true' : 'false')); - $objWriter->writeAttribute('i', ($subject->getAxisX()->getFont()->isItalic() ? 'true' : 'false')); - $objWriter->writeAttribute('strike', ($subject->getAxisX()->getFont()->isStrikethrough() ? 'sngStrike' : 'noStrike')); - $objWriter->writeAttribute('sz', ($subject->getAxisX()->getFont()->getSize() * 100)); - $objWriter->writeAttribute('u', $subject->getAxisX()->getFont()->getUnderline()); - - if ($subject->getAxisX()->getFont()->isSuperScript() || $subject->getAxisX()->getFont()->isSubScript()) { - if ($subject->getAxisX()->getFont()->isSuperScript()) { - $objWriter->writeAttribute('baseline', '30000'); - } elseif ($subject->getAxisX()->getFont()->isSubScript()) { - $objWriter->writeAttribute('baseline', '-25000'); - } - } - - // Font - a:solidFill - $objWriter->startElement('a:solidFill'); - - // a:srgbClr - $objWriter->startElement('a:srgbClr'); - $objWriter->writeAttribute('val', $subject->getAxisX()->getFont()->getColor()->getRGB()); - $objWriter->endElement(); - - $objWriter->endElement(); - - // Font - a:latin - $objWriter->startElement('a:latin'); - $objWriter->writeAttribute('typeface', $subject->getAxisX()->getFont()->getName()); - $objWriter->endElement(); - - $objWriter->endElement(); - - - $objWriter->endElement(); - - // a:r - $objWriter->startElement('a:r'); - - // a:rPr - $objWriter->startElement('a:rPr'); - $objWriter->writeAttribute('lang', 'en-US'); - $objWriter->writeAttribute('dirty', '0'); - $objWriter->endElement(); - - // a:t - $objWriter->writeElement('a:t', $subject->getAxisX()->getTitle()); - - $objWriter->endElement(); - - // a:endParaRPr - $objWriter->startElement('a:endParaRPr'); - $objWriter->writeAttribute('lang', 'en-US'); - $objWriter->writeAttribute('dirty', '0'); - $objWriter->endElement(); - - $objWriter->endElement(); - - $objWriter->endElement(); - - // c:crossAx - $objWriter->startElement('c:crossAx'); - $objWriter->writeAttribute('val', '52749440'); - $objWriter->endElement(); - - // c:crosses - $objWriter->startElement('c:crosses'); - $objWriter->writeAttribute('val', 'autoZero'); - $objWriter->endElement(); - - // c:lblAlgn - $objWriter->startElement('c:lblAlgn'); - $objWriter->writeAttribute('val', 'ctr'); - $objWriter->endElement(); - - // c:lblOffset - $objWriter->startElement('c:lblOffset'); - $objWriter->writeAttribute('val', '100'); - $objWriter->endElement(); - - $objWriter->endElement(); + $this->writeAxis($objWriter, $subject->getAxisX(), Chart\Axis::AXIS_X); } // Write Y axis? if ($chartType->hasAxisY()) { - // c:valAx (Axis Y) - $objWriter->startElement('c:valAx'); - - // c:axId - $objWriter->startElement('c:axId'); - $objWriter->writeAttribute('val', '52749440'); - $objWriter->endElement(); - - // c:scaling - $objWriter->startElement('c:scaling'); - - // c:orientation - $objWriter->startElement('c:orientation'); - $objWriter->writeAttribute('val', 'minMax'); - $objWriter->endElement(); - - // ## c:scaling - $objWriter->endElement(); - - // c:axPos - $objWriter->startElement('c:axPos'); - $objWriter->writeAttribute('val', 'l'); - $objWriter->endElement(); - - // c:numFmt - $objWriter->startElement('c:numFmt'); - $objWriter->writeAttribute('formatCode', $subject->getAxisY()->getFormatCode()); - $objWriter->writeAttribute('sourceLinked', '0'); - $objWriter->endElement(); - - // c:majorTickMark - $objWriter->startElement('c:majorTickMark'); - $objWriter->writeAttribute('val', 'none'); - $objWriter->endElement(); - - // c:tickLblPos - $objWriter->startElement('c:tickLblPos'); - $objWriter->writeAttribute('val', 'nextTo'); - $objWriter->endElement(); - - // c:txPr - $objWriter->startElement('c:txPr'); - - // a:bodyPr - $objWriter->writeElement('a:bodyPr', null); - - // a:lstStyle - $objWriter->writeElement('a:lstStyle', null); - - // a:p - $objWriter->startElement('a:p'); - - // a:pPr - $objWriter->startElement('a:pPr'); - - // a:defRPr - $objWriter->startElement('a:defRPr'); - - $objWriter->writeAttribute('b', ($subject->getAxisY()->getFont()->isBold() ? 'true' : 'false')); - $objWriter->writeAttribute('i', ($subject->getAxisY()->getFont()->isItalic() ? 'true' : 'false')); - $objWriter->writeAttribute('strike', ($subject->getAxisY()->getFont()->isStrikethrough() ? 'sngStrike' : 'noStrike')); - $objWriter->writeAttribute('sz', ($subject->getAxisY()->getFont()->getSize() * 100)); - $objWriter->writeAttribute('u', $subject->getAxisY()->getFont()->getUnderline()); - - if ($subject->getAxisY()->getFont()->isSuperScript() || $subject->getAxisY()->getFont()->isSubScript()) { - if ($subject->getAxisY()->getFont()->isSuperScript()) { - $objWriter->writeAttribute('baseline', '30000'); - } elseif ($subject->getAxisY()->getFont()->isSubScript()) { - $objWriter->writeAttribute('baseline', '-25000'); - } - } - - // Font - a:solidFill - $objWriter->startElement('a:solidFill'); - - // a:srgbClr - $objWriter->startElement('a:srgbClr'); - $objWriter->writeAttribute('val', $subject->getAxisY()->getFont()->getColor()->getRGB()); - $objWriter->endElement(); - - $objWriter->endElement(); - - // Font - a:latin - $objWriter->startElement('a:latin'); - $objWriter->writeAttribute('typeface', $subject->getAxisY()->getFont()->getName()); - $objWriter->endElement(); - - $objWriter->endElement(); - - // ## a:pPr - $objWriter->endElement(); - - // a:r - $objWriter->startElement('a:r'); - - // a:rPr - $objWriter->startElement('a:rPr'); - $objWriter->writeAttribute('lang', 'en-US'); - $objWriter->writeAttribute('dirty', '0'); - $objWriter->endElement(); - - // a:t - $objWriter->writeElement('a:t', $subject->getAxisY()->getTitle()); - - // ## a:r - $objWriter->endElement(); - - // a:endParaRPr - $objWriter->startElement('a:endParaRPr'); - $objWriter->writeAttribute('lang', 'en-US'); - $objWriter->writeAttribute('dirty', '0'); - $objWriter->endElement(); - - // ## a:p - $objWriter->endElement(); - - // ## c:txPr - $objWriter->endElement(); - - // c:crossAx - $objWriter->startElement('c:crossAx'); - $objWriter->writeAttribute('val', '52743552'); - $objWriter->endElement(); - - // c:crosses - $objWriter->startElement('c:crosses'); - $objWriter->writeAttribute('val', 'autoZero'); - $objWriter->endElement(); - - // c:crossBetween - $objWriter->startElement('c:crossBetween'); - $objWriter->writeAttribute('val', 'between'); - $objWriter->endElement(); - - $objWriter->endElement(); + $this->writeAxis($objWriter, $subject->getAxisY(), Chart\Axis::AXIS_Y); } $objWriter->endElement(); @@ -2353,4 +2085,215 @@ protected function writeSeriesMarker(XMLWriter $objWriter, Chart\Marker $oMarker $objWriter->endElement(); } } + + /** + * @param XMLWriter $objWriter + * @param Chart\Axis $oAxis + * @param $typeAxis + */ + protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, $typeAxis) + { + if ($typeAxis != Chart\Axis::AXIS_X && $typeAxis != Chart\Axis::AXIS_Y) { + return; + } + + switch ($typeAxis) { + case Chart\Axis::AXIS_X: + $mainElement = 'c:catAx'; + $axIdVal = '52743552'; + $axPosVal = 'b'; + $crossAxVal = '52749440'; + break; + case Chart\Axis::AXIS_Y: + $mainElement = 'c:valAx'; + $axIdVal = '52749440'; + $axPosVal = 'l'; + $crossAxVal = '52743552'; + break; + } + + // $mainElement + $objWriter->startElement($mainElement); + + // $mainElement > c:axId + $objWriter->startElement('c:axId'); + $objWriter->writeAttribute('val', $axIdVal); + $objWriter->endElement(); + + // $mainElement > c:scaling + $objWriter->startElement('c:scaling'); + + // $mainElement > c:scaling > c:orientation + $objWriter->startElement('c:orientation'); + $objWriter->writeAttribute('val', 'minMax'); + $objWriter->endElement(); + + // $mainElement > ##c:scaling + $objWriter->endElement(); + + // $mainElement > c:axPos + $objWriter->startElement('c:axPos'); + $objWriter->writeAttribute('val', $axPosVal); + $objWriter->endElement(); + + $oMajorGridlines = $oAxis->getMajorGridlines(); + if ($oMajorGridlines instanceof Gridlines) { + $objWriter->startElement('c:majorGridlines'); + + $this->writeAxisGridlines($objWriter, $oMajorGridlines); + + $objWriter->endElement(); + } + + $oMinorGridlines = $oAxis->getMinorGridlines(); + if ($oMinorGridlines instanceof Gridlines) { + $objWriter->startElement('c:minorGridlines'); + + $this->writeAxisGridlines($objWriter, $oMinorGridlines); + + $objWriter->endElement(); + } + + // c:numFmt + $objWriter->startElement('c:numFmt'); + $objWriter->writeAttribute('formatCode', $oAxis->getFormatCode()); + $objWriter->writeAttribute('sourceLinked', '0'); + $objWriter->endElement(); + + // c:majorTickMark + $objWriter->startElement('c:majorTickMark'); + $objWriter->writeAttribute('val', 'none'); + $objWriter->endElement(); + + // c:tickLblPos + $objWriter->startElement('c:tickLblPos'); + $objWriter->writeAttribute('val', 'nextTo'); + $objWriter->endElement(); + + // c:txPr + $objWriter->startElement('c:txPr'); + + // a:bodyPr + $objWriter->writeElement('a:bodyPr', null); + + // a:lstStyle + $objWriter->writeElement('a:lstStyle', null); + + // a:p + $objWriter->startElement('a:p'); + + // a:pPr + $objWriter->startElement('a:pPr'); + + // a:defRPr + $objWriter->startElement('a:defRPr'); + + $objWriter->writeAttribute('b', ($oAxis->getFont()->isBold() ? 'true' : 'false')); + $objWriter->writeAttribute('i', ($oAxis->getFont()->isItalic() ? 'true' : 'false')); + $objWriter->writeAttribute('strike', ($oAxis->getFont()->isStrikethrough() ? 'sngStrike' : 'noStrike')); + $objWriter->writeAttribute('sz', ($oAxis->getFont()->getSize() * 100)); + $objWriter->writeAttribute('u', $oAxis->getFont()->getUnderline()); + + if ($oAxis->getFont()->isSuperScript() || $oAxis->getFont()->isSubScript()) { + if ($oAxis->getFont()->isSuperScript()) { + $objWriter->writeAttribute('baseline', '30000'); + } elseif ($oAxis->getFont()->isSubScript()) { + $objWriter->writeAttribute('baseline', '-25000'); + } + } + + // Font - a:solidFill + $objWriter->startElement('a:solidFill'); + + // a:srgbClr + $objWriter->startElement('a:srgbClr'); + $objWriter->writeAttribute('val', $oAxis->getFont()->getColor()->getRGB()); + $objWriter->endElement(); + + $objWriter->endElement(); + + // Font - a:latin + $objWriter->startElement('a:latin'); + $objWriter->writeAttribute('typeface', $oAxis->getFont()->getName()); + $objWriter->endElement(); + + $objWriter->endElement(); + + // ## a:pPr + $objWriter->endElement(); + + // a:r + $objWriter->startElement('a:r'); + + // a:rPr + $objWriter->startElement('a:rPr'); + $objWriter->writeAttribute('lang', 'en-US'); + $objWriter->writeAttribute('dirty', '0'); + $objWriter->endElement(); + + // a:t + $objWriter->writeElement('a:t', $oAxis->getTitle()); + + // ## a:r + $objWriter->endElement(); + + // a:endParaRPr + $objWriter->startElement('a:endParaRPr'); + $objWriter->writeAttribute('lang', 'en-US'); + $objWriter->writeAttribute('dirty', '0'); + $objWriter->endElement(); + + // ## a:p + $objWriter->endElement(); + + // ## c:txPr + $objWriter->endElement(); + + // c:crossAx + $objWriter->startElement('c:crossAx'); + $objWriter->writeAttribute('val', $crossAxVal); + $objWriter->endElement(); + + // c:crosses + $objWriter->startElement('c:crosses'); + $objWriter->writeAttribute('val', 'autoZero'); + $objWriter->endElement(); + + if ($typeAxis == Chart\Axis::AXIS_X) { + // c:lblAlgn + $objWriter->startElement('c:lblAlgn'); + $objWriter->writeAttribute('val', 'ctr'); + $objWriter->endElement(); + + // c:lblOffset + $objWriter->startElement('c:lblOffset'); + $objWriter->writeAttribute('val', '100'); + $objWriter->endElement(); + } + + if ($typeAxis == Chart\Axis::AXIS_Y) { + // c:crossBetween + $objWriter->startElement('c:crossBetween'); + $objWriter->writeAttribute('val', 'between'); + $objWriter->endElement(); + } + + $objWriter->endElement(); + } + + /** + * @param XMLWriter $objWriter + * @param Gridlines $oGridlines + */ + protected function writeAxisGridlines(XMLWriter $objWriter, Gridlines $oGridlines) + { + // c:spPr + $objWriter->startElement('c:spPr'); + + // Outline + $this->writeOutline($objWriter, $oGridlines->getOutline()); + + // ##c:spPr + $objWriter->endElement(); + } } diff --git a/tests/PhpPresentation/Tests/Shape/Chart/AxisTest.php b/tests/PhpPresentation/Tests/Shape/Chart/AxisTest.php index b4d8e5178..bb23a2f14 100644 --- a/tests/PhpPresentation/Tests/Shape/Chart/AxisTest.php +++ b/tests/PhpPresentation/Tests/Shape/Chart/AxisTest.php @@ -33,6 +33,18 @@ public function testConstruct() $this->assertEquals('Axis Title', $object->getTitle()); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->getFont()); + $this->assertNull($object->getMinorGridlines()); + $this->assertNull($object->getMajorGridlines()); + } + + public function testFont() + { + $object = new Axis(); + + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setFont()); + $this->assertNull($object->getFont()); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setFont(new Font())); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->getFont()); } public function testFormatCode() @@ -44,6 +56,18 @@ public function testFormatCode() $this->assertEquals('AAAA', $object->getFormatCode()); } + public function testGridLines() + { + $object = new Axis(); + + $oMock = $this->getMock('PhpOffice\PhpPresentation\Shape\Chart\Gridlines'); + + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setMajorGridlines($oMock)); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Gridlines', $object->getMajorGridlines()); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setMinorGridlines($oMock)); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Gridlines', $object->getMinorGridlines()); + } + public function testHashIndex() { $object = new Axis(); @@ -61,14 +85,4 @@ public function testTitle() $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setTitle('AAAA')); $this->assertEquals('AAAA', $object->getTitle()); } - - public function testFont() - { - $object = new Axis(); - - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setFont()); - $this->assertNull($object->getFont()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setFont(new Font())); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->getFont()); - } } diff --git a/tests/PhpPresentation/Tests/Shape/Chart/GridlinesTest.php b/tests/PhpPresentation/Tests/Shape/Chart/GridlinesTest.php new file mode 100644 index 000000000..658062945 --- /dev/null +++ b/tests/PhpPresentation/Tests/Shape/Chart/GridlinesTest.php @@ -0,0 +1,26 @@ +assertInstanceOf('PhpOffice\PhpPresentation\Style\Outline', $object->getOutline()); + } + + public function testGetSetOutline() + { + $object = new Gridlines(); + + $oStub = $this->getMock('PhpOffice\PhpPresentation\Style\Outline'); + + $this->assertInstanceOf('PhpOffice\PhpPresentation\Style\Outline', $object->getOutline()); + $this->assertInstanceOf('PhpOffice\PhpPresentation\Shape\Chart\Gridlines', $object->setOutline($oStub)); + $this->assertInstanceOf('PhpOffice\PhpPresentation\Style\Outline', $object->getOutline()); + } +} diff --git a/tests/PhpPresentation/Tests/Writer/ODPresentation/ChartsTest.php b/tests/PhpPresentation/Tests/Writer/ODPresentation/ChartsTest.php index 085635574..4fe264080 100644 --- a/tests/PhpPresentation/Tests/Writer/ODPresentation/ChartsTest.php +++ b/tests/PhpPresentation/Tests/Writer/ODPresentation/ChartsTest.php @@ -19,6 +19,7 @@ use PhpOffice\Common\Drawing as CommonDrawing; use PhpOffice\PhpPresentation\PhpPresentation; +use PhpOffice\PhpPresentation\Shape\Chart\Gridlines; use PhpOffice\PhpPresentation\Shape\Chart\Marker; use PhpOffice\PhpPresentation\Shape\Chart\Series; use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar; @@ -367,6 +368,80 @@ public function testLegend() $this->assertTrue($pres->elementExists($element, 'Object 1/content.xml')); } + public function testTypeLineGridlines() + { + $arrayTests = array( + array( + 'dimension' => 'x', + 'styleName' => 'styleAxisXGridlinesMajor', + 'styleClass' => 'major', + 'methodAxis' => 'getAxisX', + 'methodGrid' => 'setMajorGridlines' + ), + array( + 'dimension' => 'x', + 'styleName' => 'styleAxisXGridlinesMinor', + 'styleClass' => 'minor', + 'methodAxis' => 'getAxisX', + 'methodGrid' => 'setMinorGridlines' + ), + array( + 'dimension' => 'y', + 'styleName' => 'styleAxisYGridlinesMajor', + 'styleClass' => 'major', + 'methodAxis' => 'getAxisY', + 'methodGrid' => 'setMajorGridlines' + ), + array( + 'dimension' => 'y', + 'styleName' => 'styleAxisYGridlinesMinor', + 'styleClass' => 'minor', + 'methodAxis' => 'getAxisY', + 'methodGrid' => 'setMinorGridlines' + ), + ); + $expectedColor = new Color(Color::COLOR_BLUE); + foreach ($arrayTests as $arrayTest) { + $expectedSizePts = rand(1, 100); + $expectedSizeCm = number_format(CommonDrawing::pointsToCentimeters($expectedSizePts), 2, '.', '').'cm'; + $expectedElementGrid = '/office:document-content/office:body/office:chart/chart:chart/chart:plot-area/chart:axis[@chart:dimension=\''.$arrayTest['dimension'].'\']/chart:grid'; + $expectedElementStyle = '/office:document-content/office:automatic-styles/style:style[@style:name=\''.$arrayTest['styleName'].'\']/style:graphic-properties'; + + $oPresentation = new PhpPresentation(); + $oSlide = $oPresentation->getActiveSlide(); + $oShape = $oSlide->createChartShape(); + $oLine = new Line(); + $oLine->addSeries(new Series('Downloads', array( + 'A' => 1, + 'B' => 2, + 'C' => 4, + 'D' => 3, + 'E' => 2, + ))); + $oShape->getPlotArea()->setType($oLine); + $oGridlines = new Gridlines(); + $oGridlines->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor($expectedColor); + $oGridlines->getOutline()->setWidth($expectedSizePts); + $oShape->getPlotArea()->{$arrayTest['methodAxis']}()->{$arrayTest['methodGrid']}($oGridlines); + + $oXMLDoc = TestHelperDOCX::getDocument($oPresentation, 'ODPresentation'); + $this->assertTrue($oXMLDoc->fileExists('Object 1/content.xml')); + $this->assertTrue($oXMLDoc->elementExists($expectedElementGrid, 'Object 1/content.xml')); + $this->assertTrue($oXMLDoc->attributeElementExists($expectedElementGrid, 'chart:style-name', 'Object 1/content.xml')); + $this->assertEquals($arrayTest['styleName'], $oXMLDoc->getElementAttribute($expectedElementGrid, 'chart:style-name', 'Object 1/content.xml')); + $this->assertTrue($oXMLDoc->attributeElementExists($expectedElementGrid, 'chart:class', 'Object 1/content.xml')); + $this->assertEquals($arrayTest['styleClass'], $oXMLDoc->getElementAttribute($expectedElementGrid, 'chart:class', 'Object 1/content.xml')); + $this->assertTrue($oXMLDoc->elementExists($expectedElementStyle, 'Object 1/content.xml')); + $this->assertStringStartsWith($expectedSizeCm, $oXMLDoc->getElementAttribute($expectedElementStyle, 'svg:stroke-width', 'Object 1/content.xml')); + $this->assertStringEndsWith('cm', $oXMLDoc->getElementAttribute($expectedElementStyle, 'svg:stroke-width', 'Object 1/content.xml')); + $this->assertStringStartsWith('#', $oXMLDoc->getElementAttribute($expectedElementStyle, 'svg:stroke-color', 'Object 1/content.xml')); + $this->assertStringEndsWith($expectedColor->getRGB(), $oXMLDoc->getElementAttribute($expectedElementStyle, 'svg:stroke-color', 'Object 1/content.xml')); + + unset($oPresentation); + TestHelperDOCX::clear(); + } + } + public function testTypeLineMarker() { $expectedSymbol1 = Marker::SYMBOL_PLUS; diff --git a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php index 9cb2b72b6..39c317b04 100644 --- a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php +++ b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php @@ -10,6 +10,7 @@ use PhpOffice\Common\Drawing; use PhpOffice\PhpPresentation\PhpPresentation; +use PhpOffice\PhpPresentation\Shape\Chart\Gridlines; use PhpOffice\PhpPresentation\Shape\Chart\Marker; use PhpOffice\PhpPresentation\Shape\Chart\Series; use PhpOffice\PhpPresentation\Shape\Chart\Type\Area; @@ -284,6 +285,70 @@ public function testTypeLine() $this->assertEquals($oSeries->getTitle(), $oXMLDoc->getElement($element, 'ppt/charts/'.$oShape->getIndexedFilename())->nodeValue); } + public function testTypeLineGridlines() + { + $arrayTests = array( + array( + 'methodAxis' => 'getAxisX', + 'methodGrid' => 'setMajorGridlines', + 'expectedElement' => '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:majorGridlines/c:spPr/a:ln', + 'expectedElementColor' => '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:majorGridlines/c:spPr/a:ln/a:solidFill/a:srgbClr', + ), + array( + 'methodAxis' => 'getAxisX', + 'methodGrid' => 'setMinorGridlines', + 'expectedElement' => '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:minorGridlines/c:spPr/a:ln', + 'expectedElementColor' => '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:minorGridlines/c:spPr/a:ln/a:solidFill/a:srgbClr', + ), + array( + 'methodAxis' => 'getAxisY', + 'methodGrid' => 'setMajorGridlines', + 'expectedElement' => '/c:chartSpace/c:chart/c:plotArea/c:valAx/c:majorGridlines/c:spPr/a:ln', + 'expectedElementColor' => '/c:chartSpace/c:chart/c:plotArea/c:valAx/c:majorGridlines/c:spPr/a:ln/a:solidFill/a:srgbClr', + ), + array( + 'methodAxis' => 'getAxisY', + 'methodGrid' => 'setMinorGridlines', + 'expectedElement' => '/c:chartSpace/c:chart/c:plotArea/c:valAx/c:minorGridlines/c:spPr/a:ln', + 'expectedElementColor' => '/c:chartSpace/c:chart/c:plotArea/c:valAx/c:minorGridlines/c:spPr/a:ln/a:solidFill/a:srgbClr', + ), + ); + $expectedColor = new Color(Color::COLOR_BLUE); + foreach ($arrayTests as $arrayTest) { + $expectedSizePts = rand(1, 100); + $expectedSizeEmu = round(Drawing::pointsToPixels(Drawing::pixelsToEmu($expectedSizePts))); + + $oPresentation = new PhpPresentation(); + $oSlide = $oPresentation->getActiveSlide(); + $oShape = $oSlide->createChartShape(); + $oLine = new Line(); + $oLine->addSeries(new Series('Downloads', array( + 'A' => 1, + 'B' => 2, + 'C' => 4, + 'D' => 3, + 'E' => 2, + ))); + $oShape->getPlotArea()->setType($oLine); + $oGridlines = new Gridlines(); + $oGridlines->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor($expectedColor); + $oGridlines->getOutline()->setWidth($expectedSizePts); + $oShape->getPlotArea()->{$arrayTest['methodAxis']}()->{$arrayTest['methodGrid']}($oGridlines); + + $oXMLDoc = TestHelperDOCX::getDocument($oPresentation, 'PowerPoint2007'); + $this->assertTrue($oXMLDoc->fileExists('ppt/charts/'.$oShape->getIndexedFilename())); + $this->assertTrue($oXMLDoc->elementExists($arrayTest['expectedElement'], 'ppt/charts/'.$oShape->getIndexedFilename())); + $this->assertTrue($oXMLDoc->attributeElementExists($arrayTest['expectedElement'], 'w', 'ppt/charts/'.$oShape->getIndexedFilename())); + $this->assertEquals($expectedSizeEmu, $oXMLDoc->getElementAttribute($arrayTest['expectedElement'], 'w', 'ppt/charts/'.$oShape->getIndexedFilename())); + $this->assertTrue($oXMLDoc->elementExists($arrayTest['expectedElementColor'], 'ppt/charts/'.$oShape->getIndexedFilename())); + $this->assertTrue($oXMLDoc->attributeElementExists($arrayTest['expectedElementColor'], 'val', 'ppt/charts/'.$oShape->getIndexedFilename())); + $this->assertEquals($expectedColor->getRGB(), $oXMLDoc->getElementAttribute($arrayTest['expectedElementColor'], 'val', 'ppt/charts/'.$oShape->getIndexedFilename())); + + unset($oPresentation); + TestHelperDOCX::clear(); + } + } + public function testTypeLineMarker() { do {