Chartjs-Passing json_encode to chart.js won't work

2πŸ‘

βœ…

$chartOsData['data']['datasets']['data'][] = (int)$o->total;
$chartOsData['data']['datasets']['backgroundColor'][] = $tiposOs[$o->status];

Here you’re constructing datasets as an object with the keys data and backgroundColor.

datasets: [
    {
        data: [300, 50, 100],
        backgroundColor: [...],
    }
]

Here you’re showing that datasets should be an array containing one object with those keys.

The fix is simple: make datasets an array:

$chartOsData['data']['datasets'][0]['data'][] = (int)$o->total;
$chartOsData['data']['datasets'][0]['backgroundColor'][] = $tiposOs[$o->status];
                                ^^^

Leave a comment