简体   繁体   中英

PhpOffice/PhpWord Charts - Cant show Axis, Grids, Labels

Im using the Sample_32_Chart.php from PhpOffice/PhpWord...

It's working, but I would love that my charts get axis, series, labels, titles, etc... on them.

Tried looking into the documentation http://phpoffice.github.io/PHPWord/docs/master/classes/PhpOffice.PhpWord.Element.Chart.html

But there is nothing any close to my needs.

Any help would be amazing.

Some code

<?php
include_once 'Sample_Header.php';

use PhpOffice\PhpWord\Shared\Converter;

// New Word document
echo date('H:i:s'), ' Create new PhpWord object', EOL;

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->addTitleStyle(1, array('size' => 14, 'bold' => true), array('keepNext' => true, 'spaceBefore' => 240));
$phpWord->addTitleStyle(2, array('size' => 14, 'bold' => true), array('keepNext' => true, 'spaceBefore' => 240));

// 2D charts
$section = $phpWord->addSection();
$section->addTitle(htmlspecialchars('2D charts', ENT_COMPAT, 'UTF-8'), 1);
$section = $phpWord->addSection(array('colsNum' => 2, 'breakType' => 'continuous'));

$chartTypes = array('pie', 'doughnut', 'bar', 'column', 'line', 'area', 'scatter', 'radar');
$twoSeries = array('bar', 'column', 'line', 'area', 'scatter', 'radar');
$threeSeries = array('bar', 'line');
$categories = array('A', 'B', 'C', 'D', 'E');
$series1 = array(1, 3, 2, 5, 4);
$series2 = array(3, 1, 7, 2, 6);
$series3 = array(8, 3, 2, 5, 4);

foreach ($chartTypes as $chartType) {
    $section->addTitle(ucfirst($chartType), 2);
    $chart = $section->addChart($chartType, $categories, $series1);
    $chart->getStyle()->setWidth(Converter::inchToEmu(2.5))->setHeight(Converter::inchToEmu(2));

    if (in_array($chartType, $twoSeries)) {
        $chart->addSeries($categories, $series2);
    }
    if (in_array($chartType, $threeSeries)) {
        $chart->addSeries($categories, $series3);
    }
    $section->addTextBreak();
}

// 3D charts
$section = $phpWord->addSection(array('breakType' => 'continuous'));
$section->addTitle(htmlspecialchars('3D charts', ENT_COMPAT, 'UTF-8'), 1);
$section = $phpWord->addSection(array('colsNum' => 2, 'breakType' => 'continuous'));

$chartTypes = array('pie', 'bar', 'column', 'line', 'area');
$multiSeries = array('bar', 'column', 'line', 'area');
$style = array('width' => Converter::cmToEmu(5), 'height' => Converter::cmToEmu(4), '3d' => true);
foreach ($chartTypes as $chartType) {
    $section->addTitle(ucfirst($chartType), 2);
    $chart = $section->addChart($chartType, $categories, $series1, $style);
    if (in_array($chartType, $multiSeries)) {
        $chart->addSeries($categories, $series2);
        $chart->addSeries($categories, $series3);
    }
    $section->addTextBreak();
}

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
    include_once 'Sample_Footer.php';
}

Which is the Sample_32_Chart.php, in this link I found someone talking about this functions already begin added, but dunno how to use them.

https://github.com/PHPOffice/PHPWord/pull/576

Thanks in advance!

I've worked on this a little bit and came up with some modifications that can be made to pass raw xml graph settings and have PHPWord output it. https://github.com/PHPOffice/PHPWord/issues/957 - See my second comment on that thread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM