PhpPresentation Axis Label Format

636 views Asked by At

I am trying to format the labels on the y axis

$axis->setFormatCode('#.0\%');

This is providing no change but I have confirmed that the format code is setting properly by adding an echo of getFormatCode(); in the writer class.

I have tested and this will work as expected if you change Writer\PowerPoint2007\PptCharts.php https://github.com/PHPOffice/PHPPresentation/blob/develop/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php#L2265

$objWriter->writeAttribute('sourceLinked', '0');

Can anyone tell me why that is and more importantly how do I get the Y axis to format value labels with '%'?

edit: adding code

public function configure_axis($axis, $title='', $visible=true, 
$formatCode=null){
        
        $axis = strtolower($axis);
        
        // get the axis 
        switch($axis){
            case 'x':
            case 'categorical':
                $axis = $this->shape->getPlotArea()->getAxisX();
                break;
            case 'y':
                $axis = $this->shape->getPlotArea()->getAxisY();
                break;
            default:
                throw new Exception('Invalid axis');
        }
        
        $axis->setTitle($title);
        $axis->setIsVisible($visible);
        
        if(isset($formatCode)){
            $axis->setFormatCode($formatCode);
        }
        
    }
1

There are 1 answers

4
Oleh Demkiv On

Could you show your code and tell which version of PhpPresentation you are using?

Because for me the next formatting setFormatCode('#.0\%') is works fine.

Here is an example:

<?php

require_once 'vendor/autoload.php';

use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Shape\Chart\Series;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Area;

$seriesData = [
    'Monday' => 12,
    'Tuesday' => 15,
    'Wednesday' => 13,
    'Thursday' => 17,
    'Friday' => 14,
    'Saturday' => 9,
    'Sunday' => 7
];

$objPHPPowerPoint = new PhpPresentation();
$currentSlide = $objPHPPowerPoint->getActiveSlide();
$areaChart = new Area();
$areaChart->addSeries(new Series('Downloads', $seriesData));

$shape = $currentSlide->createChartShape();
$shape->getTitle()->setVisible(false);
$shape->getLegend()->setVisible(false);
$shape->setResizeProportional(false)
    ->setWidth(800)
    ->setHeight(500)
    ->setOffsetX(50)
    ->setOffsetY(50);
$shape->getPlotArea()->setType($areaChart);

$shape->getPlotArea()->getAxisY()->setFormatCode('#.0\%');

$oWriterPPTX = IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$oWriterPPTX->save(__DIR__ . '/demo.pptx');

And result:

enter image description here

And here's the second result for the column chart (PhpOffice\PhpPresentation\Shape\Chart\Type\Bar): enter image description here