Skip to content

Commit

Permalink
PHPOffice#129 : Implementation of Gridlines in ODPresentation + Docs …
Browse files Browse the repository at this point in the history
…+ UnitsTests
  • Loading branch information
Progi1984 committed Mar 8, 2016
1 parent cbfcde3 commit 64927b6
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 309 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion docs/shapes_chart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^

Expand All @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions samples/Sample_05_Chart_Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 3 additions & 7 deletions src/PhpPresentation/Shape/Chart/Gridlines.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<?php
/**
* Created by PhpStorm.
* User: lefevre_f
* Date: 02/03/2016
* Time: 14:25
*/

namespace PhpPresentation\Shape\Chart;
namespace PhpOffice\PhpPresentation\Shape\Chart;

use PhpOffice\PhpPresentation\Style\Outline;

class Gridlines
{
Expand Down
87 changes: 68 additions & 19 deletions src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -275,77 +284,117 @@ 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');
$this->xmlContent->writeAttribute('chart:tick-marks-major-outer', 'false');
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');
$this->xmlContent->writeAttribute('chart:tick-marks-major-outer', 'false');
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();
}

Expand Down
Loading

0 comments on commit 64927b6

Please sign in to comment.