36 lines
1.0 KiB
PHP
36 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
class ChartJsFormatter
|
|
{
|
|
public static function barChart(array|Collection $labels, array $datasets): array
|
|
{
|
|
return [
|
|
'type' => 'bar',
|
|
'data' => [
|
|
'labels' => $labels,
|
|
'datasets' => array_map(function ($dataset) {
|
|
return [
|
|
'label' => $dataset['label'],
|
|
'data' => $dataset['data'],
|
|
'backgroundColor' => $dataset['backgroundColor'] ?? 'rgba(75, 192, 192, 0.6)',
|
|
'borderColor' => $dataset['borderColor'] ?? 'rgba(75, 192, 192, 1)',
|
|
'borderWidth' => $dataset['borderWidth'] ?? 1,
|
|
];
|
|
}, $datasets),
|
|
],
|
|
'options' => [
|
|
'responsive' => true,
|
|
'scales' => [
|
|
'y' => [
|
|
'beginAtZero' => true,
|
|
],
|
|
],
|
|
],
|
|
];
|
|
}
|
|
}
|