Backpack chart displays all datasets under the first label

237 views Asked by At

I am using Laravel + Backpack for admin panel and I am trying to display a chart with 12 labels (for each month) and integer data corresponding for each month. My problem is that all datasets go under the first label and I cannot figure out how to put each dataset under the correct label.

Code (keep it short):

$array; // array of eloquent models
$array2; // array of eloquent models

$this->chart->labels([
            'January',
            'February',
        ]);

$this->chart->dataset('January Exp', 'bar', count($array))
            ->color('rgba(205, 32, 31, 1)')
            ->backgroundColor('rgba(205, 32, 31, 0.4)');


$this->chart->dataset('February Exp', 'bar', count($array2))
            ->color('rgba(205, 32, 31, 1)')
            ->backgroundColor('rgba(205, 32, 31, 0.4)');

I have tried looking up on Google of a way I can bind each dataset to a particular label, but I was not able to find a solution. Perhaps I am going at it from the wrong direction.

Following Backpack's docs and Laravel Charts' docs I should be able to declare the labels and the datasets and those should show up as expected.

I have also tried declaring the datasets in the setup() function as well as the data() function. Both ways lead to the same result.

1

There are 1 answers

0
Fact0Log On BEST ANSWER

Found a solution that seems to work for my use case.

Declare the labels then one dataset with an array for the values. Code:

$array; // array of eloquent models
$array2; // array of eloquent models

$this->chart->labels([
            'January',
            'February',
        ]);

$this->chart->dataset('Exp', 'bar', [count($array), count($array2)])
            ->color('rgba(205, 32, 31, 1)')
            ->backgroundColor('rgba(205, 32, 31, 0.4)');

I hope someone finds this useful in the future.