Forum


Replies: 1   Views: 449
Creating a bar chart
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 fiwoyax200  · 07-09-2023 - 09:59

I have been reading through the documentation and I have managed to create a Horizontal bar chart but I am currently struggling to create a vertical bar chart. Please could someone point me into the right direction?

 

$data = array(
    'legend' => array('Legend 1', 'Legend 2', 'Legend 3'),
    'data' => array(
        array(
            'name' => 'data 1',
            'values' => array(10, 20, 5),
        ),
        array(
            'name' => 'data 2',
            'values' => array(20, 60, 3),
        ),
        array(
            'name' => 'data 3',
            'values' => array(50, 33, 7),
        ),
    ),
);

$paramsChart = array(
    'data' => $data,
    'type' => 'barChart',
    'color' => '3',
    'perspective' => '10',
    'rotX' => '10',
    'rotY' => '10',
    'chartAlign' => 'center',
    'sizeX' => '10',
    'sizeY' => '10',
    'legendPos' => 'b',
    'legendOverlay' => '0',
    'border' => '1',
    'hgrid' => '3',
    'vgrid' => '0',
    'groupBar' => 'stacked',
    'barDirection' => 'col' // Set barDirection to 'col' for vertical bars
);
$docx->addChart($paramsChart);

$chart = new WordFragment($docx, 'document');
$chart->addChart($paramsChart);
$docx->replaceVariableByWordFragment(array('CHART' => $chart), array('type' => 'block'));

 

Posted by admin  · 07-09-2023 - 10:27

Hello,

Please note there's no barDirection option in the addChart method, you need to set the type option. Please check the available documentation on addChart.

If you need to create a column chart you need to set type as colChart. For example, using the same styles from your code:

$data = array(
    'legend' => array('Legend 1', 'Legend 2', 'Legend 3'),
    'data' => array(
        array(
            'name' => 'data 1',
            'values' => array(10, 20, 5),
        ),
        array(
            'name' => 'data 2',
            'values' => array(20, 60, 3),
        ),
        array(
            'name' => 'data 3',
            'values' => array(50, 33, 7),
        ),
    ),
);
$paramsChart = array(
    'data' => $data,
    'type' => 'colChart',
    'color' => '3',
    'perspective' => '10',
    'rotX' => '10',
    'rotY' => '10',
    'chartAlign' => 'center',
    'sizeX' => '10',
    'sizeY' => '10',
    'legendPos' => 'b',
    'legendOverlay' => '0',
    'border' => '1',
    'hgrid' => '3',
    'vgrid' => '0',
    'groupBar' => 'stacked',
);
$docx->addChart($paramsChart);

Regards.