Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for interval unit for Chart's Axis #546

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changes/1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
- Support for defining ticks label position for Axis in Chart - @Web-Mobiledev GH-591 & @Progi1986 GH-660
- ODPresentation Writer
- PowerPoint2007 Writer
- Support for interval unit for Chart's Axis - @Progi1984 GH-546
- ODPresentation Writer
- PowerPoint2007 Writer

## Project Management
- Migrated from Travis CI to Github Actions - @Progi1984 GH-635
Expand Down
23 changes: 16 additions & 7 deletions samples/Sample_05_Chart_Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@
// Generate sample data for chart
echo date('H:i:s') . ' Generate sample data for chart' . EOL;
$seriesData = [
'Monday' => 12,
'Tuesday' => 15,
'Wednesday' => 13,
'Thursday' => 17,
'Friday' => 14,
'Saturday' => 9,
'Sunday' => 7,
'Monday 01' => 12,
'Tuesday 02' => 15,
'Wednesday 03' => 13,
'Thursday 04' => 17,
'Friday 05' => 14,
'Saturday 06' => 9,
'Sunday 07' => 7,
'Monday 08' => 8,
'Tuesday 09' => 8,
'Wednesday 10' => 15,
'Thursday 11' => 16,
'Friday 12' => 14,
'Saturday 13' => 14,
'Sunday 14' => 13,
];

// Create templated slide
Expand Down Expand Up @@ -71,6 +78,8 @@
$shape->getView3D()->setPerspective(30);
$shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE);
$shape->getLegend()->getFont()->setItalic(true);
$shape->getPlotArea()->getAxisX()->setMajorUnit(3);
$shape->getPlotArea()->getAxisY()->setMajorUnit(5);

// Create templated slide
echo EOL . date('H:i:s') . ' Create templated slide' . EOL;
Expand Down
2 changes: 2 additions & 0 deletions src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ private function writeAxisMainStyle(Chart\Axis $axis, string $styleName, bool $i
$this->xmlContent->writeAttributeIf($isPieChart, 'chart:reverse-direction', 'true');
$this->xmlContent->writeAttributeIf(null !== $axis->getMinBounds(), 'chart:minimum', $axis->getMinBounds());
$this->xmlContent->writeAttributeIf(null !== $axis->getMaxBounds(), 'chart:maximum', $axis->getMaxBounds());
$this->xmlContent->writeAttributeIf(null !== $axis->getMajorUnit(), 'chart:interval-major', $axis->getMajorUnit());
$this->xmlContent->writeAttributeIf(null !== $axis->getMinorUnit(), 'chart:interval-minor-divisor', $axis->getMinorUnit());
switch ($axis->getTickLabelPosition()) {
case Axis::TICK_LABEL_POSITION_NEXT_TO:
$this->xmlContent->writeAttribute('chart:axis-label-position', 'near-axis');
Expand Down
7 changes: 7 additions & 0 deletions src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,13 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty
$objWriter->startElement('c:lblOffset');
$objWriter->writeAttribute('val', '100');
$objWriter->endElement();

// c:majorUnit
if ($oAxis->getMajorUnit() != null) {
$objWriter->startElement('c:tickLblSkip');
$objWriter->writeAttribute('val', $oAxis->getMajorUnit());
$objWriter->endElement();
}
}

if (Chart\Axis::AXIS_Y == $typeAxis) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,7 @@ public function testTypeAxisBounds(): void
{
$value = mt_rand(0, 100);

$oSeries = new Series('Downloads', ['A' => '1', 'B' => '2', 'C' => '4', 'D' => '3', 'E' => '2']);
$oSeries->getFill()->setStartColor(new Color('FFAABBCC'));
$oSeries = new Series('Downloads', $this->seriesData);
$oLine = new Line();
$oLine->addSeries($oSeries);
$oShape = $this->oPresentation->getActiveSlide()->createChartShape();
Expand Down Expand Up @@ -512,6 +511,58 @@ public function testTypeAxisBounds(): void
$this->assertIsSchemaOpenDocumentNotValid('1.2');
}

public function testTypeAxisUnit(): void
{
$value = mt_rand(0, 100);

$series = new Series('Downloads', $this->seriesData);
$line = new Line();
$line->addSeries($series);
$shape = $this->oPresentation->getActiveSlide()->createChartShape();
$shape->getPlotArea()->setType($line);

$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'styleAxisX\']/style:chart-properties';

$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interval-minor-divisor');
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interval-major');

// chart:title : Element chart failed to validate attributes
$this->assertIsSchemaOpenDocumentNotValid('1.2');

$shape->getPlotArea()->getAxisX()->setMinorUnit($value);
$this->resetPresentationFile();

$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interval-major');
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interval-minor-divisor');
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interval-minor-divisor', $value);

// chart:title : Element chart failed to validate attributes
$this->assertIsSchemaOpenDocumentNotValid('1.2');

$shape->getPlotArea()->getAxisX()->setMinorUnit(null);
$shape->getPlotArea()->getAxisX()->setMajorUnit($value);
$this->resetPresentationFile();

$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interval-minor-divisor');
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interval-major');
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interval-major', $value);

// chart:title : Element chart failed to validate attributes
$this->assertIsSchemaOpenDocumentNotValid('1.2');

$shape->getPlotArea()->getAxisX()->setMinorUnit($value);
$shape->getPlotArea()->getAxisX()->setMajorUnit($value);
$this->resetPresentationFile();

$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interval-minor-divisor');
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interval-minor-divisor', $value);
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interval-major');
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interval-major', $value);

// chart:title : Element chart failed to validate attributes
$this->assertIsSchemaOpenDocumentNotValid('1.2');
}

public function testTypeAxisTickLabelPosition(): void
{
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'styleAxisX\']/style:chart-properties';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,36 @@ public function testTypeAxisTickMark(): void
$this->assertIsSchemaECMA376Valid();
}

public function testTypeAxisUnit(): void
public function testTypeAxisXUnit(): void
{
$value = mt_rand(0, 100);

$oSeries = new Series('Downloads', $this->seriesData);
$oLine = new Line();
$oLine->addSeries($oSeries);
$oShape = $this->oPresentation->getActiveSlide()->createChartShape();
$oShape->getPlotArea()->setType($oLine);

$elementMax = '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:tickLblSkip';

$this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax);

$this->assertIsSchemaECMA376Valid();

$oShape->getPlotArea()->getAxisX()->setMajorUnit($value);
$this->resetPresentationFile();

$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax);
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax, 'val', $value);

$this->assertIsSchemaECMA376Valid();
}

public function testTypeAxisYUnit(): void
{
$value = mt_rand(0, 100);

$oSeries = new Series('Downloads', $this->seriesData);
$oSeries->getFill()->setStartColor(new Color('FFAABBCC'));
$oLine = new Line();
$oLine->addSeries($oSeries);
$oShape = $this->oPresentation->getActiveSlide()->createChartShape();
Expand Down