Replies: 4   Views: 1130
Change values of multiple graphs in a template
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by TazioTom  · 12-05-2021 - 15:37

Hi, we are using a word template with multiple graphs. We understand you can pass in the values for a graph, but we can't see how to specify which graph the values relate to. Is it just in order, or can we specify an id for the graph? 

Posted by admin  · 12-05-2021 - 16:01


replaceChartData is the method to be used when replacing chart values. Please note that this method replaces the exact number of values of the chart; extra values can't be added to a chart using this method, the method only replaces values.

About your question, replaceChartData replaces values by order using the data array:


The keys are the numbers of the charts to replace and the values are arrays of data as described in the addChart method.

For example, if we change the included sample replaceChartData/sample_1.php to replace only the second array we use the following code:

$data = array();

$data[1] = array(
    'title' => 'Other title',
    'legends' => array(
        'legend 1',
        'legend 2',
        'legend 3',
    'categories' => array(
        'other cat 1',
        'other cat 2',
        'other cat 3',
        'other cat 4',
    'values' => array(
        array(25, 10, 5),
        array(20, 5, 4),
        array(15, 0, 3),
        array(10, 15, 2),

Setting $data[1], the values and other information in the second chart (key 0 is the first chart, key 1 is the second chart and so on) will be replaced by the new contents.

For example, if you want to replace the first, third and fourth charts you need to use:

$data = array();

$data[0] = array(
$data[2] = array(
$data[3] = array(


Posted by TazioTom  · 13-05-2021 - 08:04

Great thanks for your help. We'll give it a go.

Posted by TazioTom  · 14-05-2021 - 11:54

"Atempting to use the replaceChartData call produces the error
PHP Warning: ZipArchive::close(): Failure to create temporary file: Permission denied in /home/api/phpDox/classes/DOCXStructure.php on line 211

The code seems to be trying to create the file datos9.xlsx.docx in the root directory. The configuration of temp_path seems to make no difference, and looking at the code I can't see how it could.

Is there a solution to this? Ideally without upgrading to a new version of phpdocx as that would introduce a large testing overhead."

Posted by admin  · 14-05-2021 - 12:38


Without updating the version, please edit DocxUtilities.php and go to the replaceChartData method. In this method you can find the following lines:

$chartStructure = $excel->createXlsx('datos' . str_replace('rId', '', $idCharts[$idChart]) . '.xlsx', $charData);
$chartStructure->saveDocx('datos' . str_replace('rId', '', $idCharts[$idChart]) . '.xlsx');
rename('datos' . str_replace('rId', '', $idCharts[$idChart]) . '.xlsx.docx', 'datos' . str_replace('rId', '', $idCharts[$idChart]) . '.xlsx');

replace them by:

$chartStructure = $excel->createXlsx('datos' . str_replace('rId', '', $idCharts[$idChart]) . '.xlsx', $charData);
$chartStructure->saveDocx($this->getTempDir() . '/' . 'datos' . str_replace('rId', '', $idCharts[$idChart]) . '.xlsx');
rename($this->getTempDir() . '/datos' . str_replace('rId', '', $idCharts[$idChart]) . '.xlsx.docx', $this->getTempDir() . '/datos' . str_replace('rId', '', $idCharts[$idChart]) . '.xlsx');

In this same method you need to replace two other lines:

$zip->addFile('datos' . str_replace('rId', '', $idCharts[$idChart]) . '.xlsx', str_replace('../', 'word/', $chartTarget));


$zip->addFile($this->getTempDir() . '/datos' . str_replace('rId', '', $idCharts[$idChart]) . '.xlsx', str_replace('../', 'word/', $chartTarget));


foreach ($chartData as $idChart => $data) {
    unlink('datos' . str_replace('rId', '', $idCharts[$idChart]) . '.xlsx');


foreach ($chartData as $idChart => $data) {
    unlink($this->getTempDir() . '/datos' . str_replace('rId', '', $idCharts[$idChart]) . '.xlsx');

The previous changes use the default temp dir to save the XLSX to be added instead of the current folder.

replaceChartData is being improved in the current testing branch to add new features.