Forum


Replies: 7   Views: 1276
Importchartstyle from example doesn't work?
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 nbiz  · 23-01-2020 - 18:59

Hello,

I've just download the 9.5 version, set it up and  when I launch the example from examples/LayoutAndGeneral/importChartStyle/sample_1.php, the style and color  from the second word doesn't apply on the output.

I've tried myself with an other example and it doesn't work neither.

Can you please check on your side?

Thanks.

Posted by admin  · 23-01-2020 - 20:07

Hello,

The importChartStyle method imports colors and style XML files from an external DOCX chart, but these XML files don't apply color to the chart automatically (they add them to be used, but colors and some options can only be used after being applied to the chart XML), colors can be applied using the theme option available in the addChart method. On the package, examples/Core/addChart, you can find samples about using the theme option.

We have tested the included sample (LayoutAndGeneral/importChartStyle/sample_1.php) and the output is the expected; both files (colors and style XML files are imported). This method can be used with the theme option available in the addChart method to get the needed output.

As an alternative approach, if you don't want to use the theme option, you can import a DOCX with a chart using mergeDocx or mergeDocxAt and change its values using replaceChartData (https://www.phpdocx.com/api-documentation/docxutilities/replace-chart-data-Word-document). The three XML files of the chart will be imported: chart, colors and style.

Regards.

Posted by nbiz  · 23-01-2020 - 21:03

When I launch the sample file if I let or remove the customStyle property the output is exactly the same.

The chart i need in my document has a custom style and he has to be dynamic (from 1 to 7 bar chart).

I already tried with replaceChartData but if it doesn't fit the number of bar in my template it doesn't work.

Any ideas on how can I achieve that? 

Thanks for your help

 

Posted by admin  · 23-01-2020 - 21:18

Hello,

About 'When I launch the sample file if I let or remove the customStyle property the output is exactly the same.' ; this is because it needs to be used with the theme option available in the addChart method, otherwise a default color scheme is used. The colors XML file defines schemes and variations, but not the colors of columns, lines, titles and other chart contents that are applied in the chart XML file.

We recommend you to use the customStyle option to add the imported colors and style XML files and then use the theme option to apply the chosen colors to the chart contents. If you don't know the colors to be applied to the new chart with the theme option, they can be extracted from the external DOCX using DOCXPath and a custom XPath query so you can get them dynamically to be used with the theme option (as long as they use RGB values that are the supported ones).

Regards.

Posted by nbiz  · 24-01-2020 - 09:49

Hi,

Are we agreed that if  I use your example from importChartStyle, the output is a pie with the style from 'ChartStyles.docx' ( a pie with big white line to separate sections) but  with the values setup in the example (10,20,50,25) ?

Otherwise what is the output supposed to looked ? 

Thanks

Posted by admin  · 24-01-2020 - 12:01

Hello,

Let's try to explain both methods:

  •  importChartStyle: as explained on the API page, this method imports colors and style XML files from an existing chart in a DOCX.
  •  addChart: adds a new chart. In addition to many options, Premium licenses include the theme option that allows applying colors, sizes and others.

colors and style XML files doesn't apply colors (and other styles) to the chart, MS Word uses them as base tags that can be applied.

Just to explain this, it's the same case as HTML and CSS, you can have a CSS with colors set:

.colorRed {
  color: #ff0000;
}

.colorGreen {
  color: #00ff00;
}

But they aren't applied to the HTML unless you use them:

<span class="colorRed">My text</span>

This is the same case in MS Word for charts; the color XML file set color styles (schemes and variations) that can be used in the added chart (besides many other styles).

This is the same sample (importChartStyle) included in the package but applying styles with the theme option:

<?php

require_once 'classes/CreateDocx.php';

$docx = new CreateDocx();

$docx->importChartStyle('ChartStyles.docx', '2', 'myChartStyle');

$data = array(
    'data' => array(
        array(
            'name' => 'data 1',
            'values' => array(10),
        ),
        array(
            'name' => 'data 2',
            'values' => array(20),
        ),
        array(
            'name' => 'data 3',
            'values' => array(50),
        ),
        array(
            'name' => 'data 4',
            'values' => array(25),
        ),
    ),
);

$paramsChart = array(
    'data' => $data,
    'type' => 'pieChart',
    'rotX' => 20,
    'rotY' => 20,
    'perspective' => 30,
    'color' => 2,
    'sizeX' => 10,
    'sizeY' => 5,
    'chartAlign' => 'center',
    'showPercent' => 1,
    'customStyle' => 'myChartStyle',
    'theme' => array(
        'valueRgbColors' => array(
            array('ED7D31', 'A5A5A5', 'FFC000', 'C5E0B4'),
        ),
    ),
);
$docx->addChart($paramsChart);

$docx->createDocx('output');

This script imports both color and styles XML files and then apply custom colors. Importing color and styles XML files may be needed in some special cases to generate custom charts from existing charts.

If you don't know the colors to be used or you need to apply styles not supported by the addChart method, the only solution available would be using replaceChartData importing the DOCX that contains the chart; some users generate a DOCX with as many charts as needed with the exact numbers of values to be replaced. We have moved your post to the dev team to be checked so they can improve the library to ease this workflow.

Regards.

Posted by nbiz  · 24-01-2020 - 15:08

Hello, 

Ok thanks for the precision.

I put the chart in a new document  but how can I grab the chart and put it at a specific place in my original document ?

I've tried this.

$docOriginal = new CreateDocxFromTemplate('Original.docx');
$docChart = new CreateDocxFromTemplate('Chart.docx');
$referenceNode = array(
    'type' => 'chart',
    'occurrence' => 1,
   
);
$Contents = $docChart ->GetWordContents($referenceNode);
$docOriginal ->replaceVariableByWordFragment(array('MYCHART' => $Contents[0]), array('type' => 'block'));

 i've print_r(Contents) ,he has one key but it's empty .

Can you tell me how to do ?

Thanks

Posted by admin  · 24-01-2020 - 15:47

Hello,

For this task we recommend you to use mergeDocxAt , which allows getting a DOCX content with the relationships (charts have internal relationships) and add it after a specific position of the document.

On the previous API page and the package you can find a sample of using it. DOCXPath folder includes many other samples to illustrate using the referenceNode option.

If the DOCX contains more content than the chart to be imported, you can use removeWordContent to clean it before importing it.

Regards.