Forum


Replies: 7   Views: 1794
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 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.