Skip to content

Commit e2f670e

Browse files
authored
Merge pull request #546 from Progi1984/issue171
Support for interval unit for Chart's Axis
2 parents f8c8206 + 40ee1ad commit e2f670e

File tree

6 files changed

+107
-11
lines changed

6 files changed

+107
-11
lines changed

docs/changes/1.0.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
- Support for defining ticks label position for Axis in Chart - @Web-Mobiledev GH-591 & @Progi1986 GH-660
4747
- ODPresentation Writer
4848
- PowerPoint2007 Writer
49+
- Support for interval unit for Chart's Axis - @Progi1984 GH-546
50+
- ODPresentation Writer
51+
- PowerPoint2007 Writer
4952

5053
## Project Management
5154
- Migrated from Travis CI to Github Actions - @Progi1984 GH-635

samples/Sample_05_Chart_Line.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,20 @@
3636
// Generate sample data for chart
3737
echo date('H:i:s') . ' Generate sample data for chart' . EOL;
3838
$seriesData = [
39-
'Monday' => 12,
40-
'Tuesday' => 15,
41-
'Wednesday' => 13,
42-
'Thursday' => 17,
43-
'Friday' => 14,
44-
'Saturday' => 9,
45-
'Sunday' => 7,
39+
'Monday 01' => 12,
40+
'Tuesday 02' => 15,
41+
'Wednesday 03' => 13,
42+
'Thursday 04' => 17,
43+
'Friday 05' => 14,
44+
'Saturday 06' => 9,
45+
'Sunday 07' => 7,
46+
'Monday 08' => 8,
47+
'Tuesday 09' => 8,
48+
'Wednesday 10' => 15,
49+
'Thursday 11' => 16,
50+
'Friday 12' => 14,
51+
'Saturday 13' => 14,
52+
'Sunday 14' => 13,
4653
];
4754

4855
// Create templated slide
@@ -71,6 +78,8 @@
7178
$shape->getView3D()->setPerspective(30);
7279
$shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE);
7380
$shape->getLegend()->getFont()->setItalic(true);
81+
$shape->getPlotArea()->getAxisX()->setMajorUnit(3);
82+
$shape->getPlotArea()->getAxisY()->setMajorUnit(5);
7483

7584
// Create templated slide
7685
echo EOL . date('H:i:s') . ' Create templated slide' . EOL;

src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ private function writeAxisMainStyle(Chart\Axis $axis, string $styleName, bool $i
310310
$this->xmlContent->writeAttributeIf($isPieChart, 'chart:reverse-direction', 'true');
311311
$this->xmlContent->writeAttributeIf(null !== $axis->getMinBounds(), 'chart:minimum', $axis->getMinBounds());
312312
$this->xmlContent->writeAttributeIf(null !== $axis->getMaxBounds(), 'chart:maximum', $axis->getMaxBounds());
313+
$this->xmlContent->writeAttributeIf(null !== $axis->getMajorUnit(), 'chart:interval-major', $axis->getMajorUnit());
314+
$this->xmlContent->writeAttributeIf(null !== $axis->getMinorUnit(), 'chart:interval-minor-divisor', $axis->getMinorUnit());
313315
switch ($axis->getTickLabelPosition()) {
314316
case Axis::TICK_LABEL_POSITION_NEXT_TO:
315317
$this->xmlContent->writeAttribute('chart:axis-label-position', 'near-axis');

src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,13 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty
23142314
$objWriter->startElement('c:lblOffset');
23152315
$objWriter->writeAttribute('val', '100');
23162316
$objWriter->endElement();
2317+
2318+
// c:majorUnit
2319+
if ($oAxis->getMajorUnit() != null) {
2320+
$objWriter->startElement('c:tickLblSkip');
2321+
$objWriter->writeAttribute('val', $oAxis->getMajorUnit());
2322+
$objWriter->endElement();
2323+
}
23172324
}
23182325

23192326
if (Chart\Axis::AXIS_Y == $typeAxis) {

tests/PhpPresentation/Tests/Writer/ODPresentation/ObjectsChartTest.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,7 @@ public function testTypeAxisBounds(): void
463463
{
464464
$value = mt_rand(0, 100);
465465

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

514+
public function testTypeAxisUnit(): void
515+
{
516+
$value = mt_rand(0, 100);
517+
518+
$series = new Series('Downloads', $this->seriesData);
519+
$line = new Line();
520+
$line->addSeries($series);
521+
$shape = $this->oPresentation->getActiveSlide()->createChartShape();
522+
$shape->getPlotArea()->setType($line);
523+
524+
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'styleAxisX\']/style:chart-properties';
525+
526+
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interval-minor-divisor');
527+
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interval-major');
528+
529+
// chart:title : Element chart failed to validate attributes
530+
$this->assertIsSchemaOpenDocumentNotValid('1.2');
531+
532+
$shape->getPlotArea()->getAxisX()->setMinorUnit($value);
533+
$this->resetPresentationFile();
534+
535+
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interval-major');
536+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interval-minor-divisor');
537+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interval-minor-divisor', $value);
538+
539+
// chart:title : Element chart failed to validate attributes
540+
$this->assertIsSchemaOpenDocumentNotValid('1.2');
541+
542+
$shape->getPlotArea()->getAxisX()->setMinorUnit(null);
543+
$shape->getPlotArea()->getAxisX()->setMajorUnit($value);
544+
$this->resetPresentationFile();
545+
546+
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interval-minor-divisor');
547+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interval-major');
548+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interval-major', $value);
549+
550+
// chart:title : Element chart failed to validate attributes
551+
$this->assertIsSchemaOpenDocumentNotValid('1.2');
552+
553+
$shape->getPlotArea()->getAxisX()->setMinorUnit($value);
554+
$shape->getPlotArea()->getAxisX()->setMajorUnit($value);
555+
$this->resetPresentationFile();
556+
557+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interval-minor-divisor');
558+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interval-minor-divisor', $value);
559+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interval-major');
560+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interval-major', $value);
561+
562+
// chart:title : Element chart failed to validate attributes
563+
$this->assertIsSchemaOpenDocumentNotValid('1.2');
564+
}
565+
515566
public function testTypeAxisTickLabelPosition(): void
516567
{
517568
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'styleAxisX\']/style:chart-properties';

tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,12 +499,36 @@ public function testTypeAxisTickMark(): void
499499
$this->assertIsSchemaECMA376Valid();
500500
}
501501

502-
public function testTypeAxisUnit(): void
502+
public function testTypeAxisXUnit(): void
503+
{
504+
$value = mt_rand(0, 100);
505+
506+
$oSeries = new Series('Downloads', $this->seriesData);
507+
$oLine = new Line();
508+
$oLine->addSeries($oSeries);
509+
$oShape = $this->oPresentation->getActiveSlide()->createChartShape();
510+
$oShape->getPlotArea()->setType($oLine);
511+
512+
$elementMax = '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:tickLblSkip';
513+
514+
$this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax);
515+
516+
$this->assertIsSchemaECMA376Valid();
517+
518+
$oShape->getPlotArea()->getAxisX()->setMajorUnit($value);
519+
$this->resetPresentationFile();
520+
521+
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax);
522+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax, 'val', $value);
523+
524+
$this->assertIsSchemaECMA376Valid();
525+
}
526+
527+
public function testTypeAxisYUnit(): void
503528
{
504529
$value = mt_rand(0, 100);
505530

506531
$oSeries = new Series('Downloads', $this->seriesData);
507-
$oSeries->getFill()->setStartColor(new Color('FFAABBCC'));
508532
$oLine = new Line();
509533
$oLine->addSeries($oSeries);
510534
$oShape = $this->oPresentation->getActiveSlide()->createChartShape();

0 commit comments

Comments
 (0)