Forum


Replies: 11   Views: 3224
Change data chart from xlsx
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 Red Nacional de Refugios  · 20-11-2017 - 17:50

https://drive.google.com/open?id=1y5H8rZOHw-vGM1QAnB0MTIA53NQdqjqD

 i haver it xlsx,  

can't   i change the data from chart?

can  you help me pls?

 my code is


 $gr1=storage_path('reporteword2017/gr.xlsx');

 $data = array(
                'legend' => array('Mujeres', 'Hijas', 'Hijos','Total'),
                'data' => array(
                    array(
                        'name' => 'Enero',
                        'values' => array(10, 20, 5,10, 20, 5,10, 20, 5,10, 20, 5),
                    ),
                    array(
                        'name' => 'Febrero',
                        'values' => array(10, 20, 5,10, 20, 5,10, 20, 5,10, 20, 5),
                    ),

                    array(
                        'name' => 'Marzo',
                        'values' => array(10, 20, 5,10, 20, 5,10, 20, 5,10, 20, 5),
                    ),
                    array(
                        'name' => 'Abril',
                        'values' => array(10, 20, 5,10, 20, 5,10, 20, 5,10, 20, 5),
                    ),
                    array(
                        'name' => 'Mayo',
                        'values' => array(10, 20, 5,10, 20, 5,10, 20, 5,10, 20, 5),
                    ),
                    array(
                        'name' => 'Junio',
                        'values' => array(10, 20, 5,10, 20, 5,10, 20, 5,10, 20, 5),
                    ),
                    array(
                        'name' => 'Julio',
                        'values' => array(10, 20, 5,10, 20, 5,10, 20, 5,10, 20, 5),
                    ),
                    array(
                        'name' => 'Agosto',
                        'values' => array(10, 20, 5,10, 20, 5,10, 20, 5,10, 20, 5),
                    ),
                    array(
                        'name' => 'Septiembre',
                        'values' => array(10, 20, 5,10, 20, 5,10, 20, 5,10, 20, 5),
                    ),
                    array(
                        'name' => 'Octubre',
                        'values' => array(10, 20, 5,10, 20, 5,10, 20, 5,10, 20, 5),
                    ),
                    array(
                        'name' => 'Noviembre',
                        'values' => array(10, 20, 5,10, 20, 5,10, 20, 5,10, 20, 5),
                    ),
                    array(
                        'name' => 'Diciembre',
                        'values' => array(10, 20, 5,10, 20, 5,10, 20, 5,10, 20, 5),
                    )
                )
            );
            
            $paramsChart = array(
                'externalXLSX' => array(
                    'src' => $gr1,
                ),
                'sizeX' => 15,
                'sizeY' => 7,
                'chartAlign' => 'center',
                'data' => $data
            );
    
//            return $this->docx->getTemplateVariables();
            $chart = new WordFragment($this->docx, 'document');
            $chart->addChart($paramsChart);
            $this->docx->replaceVariableByWordFragment(array('graficas' => $chart));

 

Posted by admin  · 20-11-2017 - 18:10

Hello,

You can use the replaceChartData method to change the values and data of charts in a DOCX.

Regards.

Posted by Red Nacional de Refugios  · 20-11-2017 - 18:30

 i try it but  this is the error

CreateBarChart::$_rId as non static

 

 $docx = new DocxUtilities();
    
        $source = storage_path('reporteword2017/grafica1.docx');
        $target = storage_path('reporteword2017/grafica2.docx');
    
        $data = array();
        $data[0] = array(
            'title' => 'New titleaaaa',
            'legends' => array(
                'new legend',
            ),
            'categories' => array(
                'cat 1',
                'cat 2',
                'cat 3',
                'cat 4',
            ),
            'values' => array(
                array(25,12,22,33),
                array(25,12,22,33),
                array(25,12,22,33),
                array(25,12,22,33),
                array(25,12,22,33),
                array(25,12,22,33),
                array(25,12,22,33),
                array(25,12,22,33),
                array(25,12,22,33),
                array(25,12,22,33),
                array(25,12,22,33),
                array(25,12,22,33)
                
            ),
        );
        $docx->replaceChartData($source, $target, $data);

 

Posted by admin  · 20-11-2017 - 18:47

Hello,

That message seems a static notice from PHP, not an error/warning. Please hide the static notices in your PHP configuration/framework or edit the CreateGraphic.inc file and change this line (around line number 31) :

protected static $_rId;

by:

protected $_rId;

and try again. You just need to remove the static keyword. The next version of phpdocx will include this change.

Regards.

Posted by Red Nacional de Refugios  · 20-11-2017 - 19:12

thank  you   now  the error is 

 

Exception in DocxUtilities.inc line 3987:The index legends does not exist.

i dont change the code 

 

 

Posted by admin  · 21-11-2017 - 07:25

Hello,

Please check and run examples/DocxUtilities/replaceChartData/sample_1.php included in the package. It seems you are trying to replace a not existing chart (that the index Exception error PHP throws).

If after checking that example, you still have problems, please send the file (reporteword2017/grafica1.docx) to contact[at]phpdocx.com and we'll check it.

Regards.

Posted by Red Nacional de Refugios  · 22-11-2017 - 04:57

i try to make  the example about replace data chart my code, the docx file is in folder of the example, i only moved it file in my storage

$docxx = new DocxUtilities();
        $source = storage_path('reporteword2017/example_area_chart.docx');
        $target = storage_path('reporteword2017/example_area_chartt.docx');
        $data = array();
        $data[0] = array(
            'title' => 'New title',
            'legends' => array(
                'new legend',
            ),
            'categories' => array(
                'cat 1',
                'cat 2',
                'cat 3',
                'cat 4',
            ),
            'values' => array(
                array(25),
                array(20),
                array(15),
                array(10)
            ),
        );
        $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),
            ),
        );
    
        $docxx->replaceChartData($source, $target, $data);

 

Posted by admin  · 22-11-2017 - 07:15

Hello,

We have tested the included sample in the DocxUtilities/replaceChartData folder with PHP 5.2, PHP 5.3, PHP 5.5, PHP 5.6, PHP 7.0 and PHP 7.1 on Linux, Windows and macOS and everything is working as expected.

Please extract the package again, and without changing anything, open the examples/DocxUtilities/replaceChartData folder and run the script using PHP CLI mode:

$ php sample_1.php

and check the DOCX output. Also please check the script can write to the target folder.

Do you see any PHP error? Could you post the PHP version you are using?

Regards,

Posted by Red Nacional de Refugios  · 22-11-2017 - 17:37

 

thanks, I use the example works, I made another and also, using php cli
everything is ok

when I use laravel the function replaceChartData works fine but it shows me the error
ErrorException in CreateCompletedXlsx.inc line 144: undefined index: name

* the file with the graphics if it is generated

my code is

 $docxx = new \Phpdocx\Utilities\DocxUtilities();
        $source = storage_path('/reporteword2017/example_area_chart.docx');
        $target = storage_path('/reporteword2017/example_area_chartt.docx');
        $data = array();
        $data[0] = array(
            'title' => 'ramiro',
            'legends' => array(
                'new legend',
            ),
            'categories' => array(
                'cat 1',
                'cat 2',
                'cat 3',
                'cat 4',
            ),
            'values' => array(
                array(30),
                array(10),
                array(15),
                array(10)
            ),
        );
        $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),
            ),
        );
        $docxx->replaceChartData($source, $target, $data);

i  think that is a warning  in the code 

i wait you  answer  to do in laravel   thanks.

Posted by admin  · 22-11-2017 - 19:05

Hello,

We are going to check the undefined index. As a workaround please add @ when you invoke replaceChartData to hide the notice:

@$docxx->replaceChartData($source, $target, $data);

Although this isn't a good practice, it will avoid the notice (and other errors that the method may show as well, so please use it carefully and only in this line until a new package is released).

Laravel (as Symfony and other PHP frameworks) throws an exception in the dev environment when a notice is thrown. You can avoid it changing the default configuration, but we are going to check and fix the undefined index notice.

Regards.

Posted by Red Nacional de Refugios  · 27-11-2017 - 17:30

thank you   finally this function  is  ok to change the data  of column chart  .

but when i try to   change data of  pie chart  doen`t  change,   it is the same array to change the data?

 

array:2 [â–¼
  0 => array:2 [â–¼
    "title" => "ss"
    "values" => array:12 [â–¼
      0 => array:4 [â–¶]
      1 => array:4 [â–¶]
      2 => array:4 [â–¶]
      3 => array:4 [â–¶]
      4 => array:4 [â–¶]
      5 => array:4 [â–¶]
      6 => array:4 [â–¶]
      7 => array:4 [â–¶]
      8 => array:4 [â–¶]
      9 => array:4 [â–¶]
      10 => array:4 [â–¶]
      11 => array:4 [â–¶]
    ]
  ]
  1 => array:2 [â–¼
    "title" => "ss"
    "values" => array:1 [â–¼
      0 => array:3 [â–¼
        0 => 54
        1 => 52
        2 => 30
      ]
    ]
  ]
]

the  pie chart is the number 2

Posted by admin  · 28-11-2017 - 07:51

Hello,

You need to use the same data structure than the addChart method for each chart type. For example, for the piechart generated by the included sample Core/addChart/sample_1.php, you can replace the data using this code:

$data = array();
$data[0] = array(
    'title' => 'New title',
    'values' => array(
        array(30),
        array(30),
        array(40),
    ),
);
$data[1] = array(
    'title' => 'Other title',
    'values' => array(
        array(50),
        array(40),
        array(30),
    ),
);

$docx->replaceChartData($source, $target, $data);

Regards.