Search code examples
phpyii2pie-chart

Yii2: Customize PieChart colors using GoogleChart Widget


I have tried this code to change the color:

<?php
    echo GoogleChart::widget(array('visualization' => 'PieChart',
        'data' => array(
            array('Task', 'All Statuses'),
            //   array('Picked Up', (int) $all_vehicle['picked_up']),
            array('Car on Way', (int) $all_vehicle['car_on_way']),
            array('Shipped', (int) $all_vehicle['shipped']),
            array('On Hand', (int) $all_vehicle['on_hand']),
        ),
        'options' => array('title' => 'All','width' => 442, 'height' => 400, 'pieHole' => 0.4, 'seriesColors' => [ "#000", "#000", "#000", "#000" ])
    ));
?>

Using this array to change the color

seriesColors' => [ "#000", "#000", "#000", "#000" ])

Solution

  • You have to use the option color rather than seriesColor as the option for the plugin see DOCS. so change your widget code to the below

    <?php
        echo GoogleChart::widget(array('visualization' => 'PieChart',
            'data' => array(
                array('Task', 'All Statuses'),
                //   array('Picked Up', (int) $all_vehicle['picked_up']),
                array('Car on Way', (int) $all_vehicle['car_on_way']),
                array('Shipped', (int) $all_vehicle['shipped']),
                array('On Hand', (int) $all_vehicle['on_hand']),
            ),
            'options' => array(
                'title' => 'All', 
                'width' => 442, 
                'height' => 400, 
                'pieHole' => 0.4, 
                'colors'=> ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6']
            )
        ));
    ?>