Forum


Replies: 7   Views: 307
Serdatalabels - how to use?
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 256bytes  · 18-05-2024 - 11:45

Hello phpDocx Team,

i want to show Number-Values next to the LineChart i created. However i am unable to get that running. Currently i am using

'showValue'  => true,

but i can not format the numbers with

'formatCode' => '#,##0.00'

I still get numbers like 1.73456 and not 1.73 like expected.

Can you provide a working example for serDataLabels, as i think this might be used for further finetuning the visualisation of numbers in the diagram .

unfortunatly theres no sample in the sourcecode provided and i am unable tio guess the array structure.

'serDataLabels' (array): data labels options (bar, bubble, column, line ofPie, pie and scatter charts)
*          'formatCode' (array)
*          'position (array): center, insideEnd, insideBase, outsideEnd
*          'showCategory' (array): 0, 1
*          'showLegendKey' (array): 0, 1
*          'showPercent' (array): 0, 1
*          'showSeries' (array): 0, 1
*          'showValue' (array): 0, 1

Looking forward to that answer and thank you in advance!!

Posted by admin  · 18-05-2024 - 14:31

Hello,

In the Premium package you can find the following samples that use the serDataLabels option: sample_20.php and sample_22.php. For example, from sample_22.php:

$data = array(
    'legend' => array('Series 1', 'Series 2', 'Series 3'),
    'data' => array(
        array(
            'name' => 'data 1',
            'values' => array(10, 7, 5),
        ),
        array(
            'name' => 'data 2',
            'values' => array(20, 60, 3),
        ),
        array(
            'name' => 'data 3',
            'values' => array(50, 33, 7),
        ),
        array(
            'name' => 'data 4',
            'values' => array(25, 0, 14),
        ),
    ),
);
$paramsChart = array(
    'data' => $data,
    'type' => 'lineChart',
    'color' => '5',
    'chartAlign' => 'center',
    'showTable' => 0,
    'sizeX' => 12,
    'sizeY' => 10,
    'legendPos' => 'b',
    'legendOverlay' => '0',
    'haxLabel' => 'X Axis',
    'vaxLabel' => 'Y Axis',
    'haxLabelDisplay' => 'horizontal',
    'vaxLabelDisplay' => 'vertical',
    'hgrid' => '3',
    'vgrid' => '1',
    'theme' => array(
        'serDataLabels' => array(
            array(
                'showValue' => 1,
            ),
            array(
                'showValue' => 1,
            ),
            array(
                'showValue' => 1,
            ),
        ),
    ),
);
$docx->addChart($paramsChart);

And the same sample setting custom formatCode options:

$data = array(
    'legend' => array('Series 1', 'Series 2', 'Series 3'),
    'data' => array(
        array(
            'name' => 'data 1',
            'values' => array(10, 7, 5),
        ),
        array(
            'name' => 'data 2',
            'values' => array(20, 60, 3),
        ),
        array(
            'name' => 'data 3',
            'values' => array(50, 33, 7),
        ),
        array(
            'name' => 'data 4',
            'values' => array(25, 0, 14),
        ),
    ),
);
$paramsChart = array(
    'data' => $data,
    'type' => 'lineChart',
    'color' => '5',
    'chartAlign' => 'center',
    'showTable' => 0,
    'sizeX' => 12,
    'sizeY' => 10,
    'legendPos' => 'b',
    'legendOverlay' => '0',
    'haxLabel' => 'X Axis',
    'vaxLabel' => 'Y Axis',
    'haxLabelDisplay' => 'horizontal',
    'vaxLabelDisplay' => 'vertical',
    'hgrid' => '3',
    'vgrid' => '1',
    'theme' => array(
        'serDataLabels' => array(
            array(
                'showValue' => 1,
                'formatCode' => '#,##0.00',
            ),
            array(
                'showValue' => 1,
                'formatCode' => '##,##0.000',
            ),
            array(
                'showValue' => 1,
                'formatCode' => '#,##0.0000',
            ),
        ),
    ),
);

Please note that a Premium package must be used to apply theme options such as serDataLabels.

Regards.

Posted by 256bytes  · 21-05-2024 - 10:23

That did the trick, thanks!

Is there any possibility to make the font smaller of the

serDataLabels

text?

Posted by admin  · 21-05-2024 - 10:43

Hello,

Sorry, but there's no option in the current stable version of phpdocx to change the font size of serDataLabels. We have added it to the task list to be supported.

Regards.

Posted by 256bytes  · 21-05-2024 - 12:59

the position property seems not to work on the linecharts but the column chart positioning looks good. is there a patch or some trick i can do to achive the positioning on the line chart too. currently the values on the line chart appear overlapped

'serDataLabels' => [
    [
       'showValue' => 1,
'formatCode' => '#,##0.00',
'position' => 'outsideEnd'
],
[
       'showValue' => 1,
'formatCode' => '#,##0.00',
'position' => 'insideBase'
],
]

Posted by admin  · 21-05-2024 - 13:18

Hello,

Please note that each chart type support only specific positions.

If you open MS Word and create a line chart, you can check the available positions for this chart type are the following: center, left, right, above (top), below (bottom).

Column chart supports the following positions: center, insideEnd, insideBase and outsideEnd.

Unfortunately, MS Word doesn't support all positions types for all chart types (https://c-rex.net/samples/ooxml/e1/Part4/OOXML_P4_DOCX_ST_DLblPos_topic_ID0EJ3QRB.html#topic_ID0EJ3QRB). And the bestFit value is not supported in most cases (and may generate a corrupted document).

For your task, maybe the best approach is adding a supported position for each series (mixing them):

'theme' => array(
    'serDataLabels' => array(
        array(
            'showValue' => 1,
            'formatCode' => '#,##0.00',
            'position' => 'top',
        ),
        array(
            'showValue' => 1,
            'formatCode' => '##,##0.000',
            'position' => 'bottom',
        ),
        array(
            'showValue' => 1,
            'formatCode' => '#,##0.0000',
            'position' => 'right',
        ),
    ),
),

Regards.

Posted by 256bytes  · 21-05-2024 - 14:15

Thanks, that did the trick. i patched

generateDATALABELS_SER()

in CreateGraphic.php and added

case 'top':
    $position = 't';
    break;
case 'left':
    $position = 'l';
    break;
case 'right':
    $position = 'r';
    break;
case 'bottom':
    $position = 'b';
    break;
case 'bestFit':
    $position = 'bestFit';
    break;

Posted by admin  · 21-05-2024 - 15:17

Hello,

Please note that bestFit is not supported by all MS Word versions. A corrupted DOCX document error may appear with some MS Word versions.

In any case, we have added it to do task list to consider adding it into the stable release.

Regards.