【问题标题】:PhpPresentation Axis Label FormatPhpPresentation 轴标签格式
【发布时间】:2023-04-09 09:27:01
【问题描述】:

我正在尝试格式化 y 轴上的标签

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

这没有提供任何更改,但我已通过在编写器类中添加 getFormatCode(); 的回显来确认格式代码设置正确。

我已经测试过,如果您更改 Writer\PowerPoint2007\PptCharts.php,这将按预期工作 https://github.com/PHPOffice/PHPPresentation/blob/develop/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php#L2265

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

谁能告诉我为什么会这样,更重要的是如何让 Y 轴用 '%' 格式化值标签?

编辑:添加代码

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);
        }
        
    }

【问题讨论】:

    标签: php phpoffice phppresentation


    【解决方案1】:

    您能否展示您的代码并说明您使用的是哪个版本的 PhpPresentation?

    因为对我来说,下一个格式 setFormatCode('#.0\%') 可以正常工作。

    这是一个例子:

    <?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');
    
    

    结果:

    这是柱形图的第二个结果 (PhpOffice\PhpPresentation\Shape\Chart\Type\Bar):

    【讨论】:

    • 你能用柱形图试试看是否有区别吗?我正在使用 dev-develop 的一个分支。 github.com/ksmeeks0001/PHPPresentation
    • @KevinSmeeks 柱形图也可以。我添加了第二张截图
    猜你喜欢
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多