Properly display the googlechart

I’m trying to display a data coming from the GridView column using the Google Chart and there’s something wrong in my code because it does not display the Google Pie Chart in the page.

Here’s what I have done:




    $modelCategory = Category::find()->select(['id', 'category_name'])->all();

    $arr = array('id' => array(),

                'category_name' => array());


    echo GoogleChart::widget(array('visualization' => 'PieChart',

            'data' => array(

                array('category_name'),

            )

        )

    )



The page says "Not enough columns given to draw the requested chart" but I just want to display the different categories from the Category Model.

I think your data set is not in right format, documentation has pretty good examples, data should also include headers as well the data entries


$data = array_map(

    function($i) { return [$i['category_name'], $i['id']];},

    Category::find()->select(['id', 'category_name'])->all()

);


echo GoogleChart::widget(array('visualization' => 'PieChart',

        'data' => array_merge([['category_name', 'id']], $data)

    )

);

I see, by the way why it counts the ‘id’ of the category. I want to count the Categories under the Ticket Model.

Here’s what I have done and I’m getting an error “Pie chart should have a first column of type string×”




use app\models\Tickets;


$data = array_map(

    function($i) {

        return [$i['category_id'], $i['id']];},


    Tickets::find()->select(['id', 'category_id' ])->all()

    );


    

    echo GoogleChart::widget(array('visualization' => 'PieChart',

        'data' => array_merge([['category_id', 'id']], $data)

    )

);



I think the error is pretty clear your first column is a type of integer, maybe you can put your category name first and then your category id


$data = array_map(

    function($i) {

        return [$i['category_name'], $i['category_id']];},


    Tickets::find()->select(['category_name', 'category_id' ])->all()

    );


    

    echo GoogleChart::widget(array('visualization' => 'PieChart',

        'data' => array_merge([['Category name', 'Category id']], $data)

    )

);