Add chart on phpoffice/phpword

2019-07-04 00:56发布

问题:

I know how to add charts on PHPExcel, but I also need to insert charts on a docx file. Is it possible to manipulate charts with phpoffice/phpword?

If it's not possible, do you know a good library for the job?

回答1:

It's not actually possible to add chart in a PHPWord Document. But the feature is in the pipe (follow this ticket #123).

But you can use of one of some libraries used (actually and in the future) by PHPExcel : PHP Charting Libraries . Actually, PHPExcel just use JpGraph for rendering some graphs.



回答2:

I don't know the library, but you can probably generate image in php (use PHP GD or imagemagick for example) and insert generated image into docx file.



回答3:

Yes it is possible to add charts in phpoffice. The problem is the documentation hasn't been completed yet.

$chart = $section->addChart([$chartType], [$categories], [$series]);
$chart->getStyle() //For styling the chart

$chartTypes = 'pie' / 'doughnut' / 'bar' / 'column'/ 'line'/ 'area' / 'scatter' / 'radar';

//Example
$chart = $section->addChart('pie', array('A', 'B', 'C', 'D', 'E'), array(1, 3, 2, 5, 4));


回答4:

Some example from official github, hope this could answer your question :)

PHPWord : https://github.com/PHPOffice/PHPWord/blob/master/samples/Sample_32_Chart.php

PHPExcel : https://github.com/PHPOffice/PHPExcel/tree/master/Examples



回答5:

You can add a chart, but its a bit limited and there are some hard codings in what's been created so far.

Example of what you can do: http://elumina.co/libs/PHPWord-master/samples/Sample_32_Chart.php

Source Code:https://github.com/PHPOffice/PHPWord/blob/master/samples/Sample_32_Chart.php

Some modification is needed to the classes to get certain graph options to work as these options were hard coded https://github.com/PHPOffice/PHPWord/issues/957

Can't edit the data https://github.com/PHPOffice/PHPWord/issues/956