Skip to content

Instantly share code, notes, and snippets.

@ManojKiranA
Last active September 29, 2022 08:27
Show Gist options
  • Save ManojKiranA/abae777d7d6455f51ad90fa66da5ab67 to your computer and use it in GitHub Desktop.
Save ManojKiranA/abae777d7d6455f51ad90fa66da5ab67 to your computer and use it in GitHub Desktop.
Doughnut Chart Implementation in Filament Admin Panel
<?php
namespace App\Filament\Widgets;
use Carbon\CarbonPeriod;
use Filament\Widgets\DoughnutChartWidget;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
class DoughnutChart extends DoughnutChartWidget
{
public function getColumnSpan(): int | string | array
{
return 1;
}
protected function getHeading(): string
{
return 'Doughnut Chart Example';
}
protected function getData(): array
{
return [
'labels' => collect(CarbonPeriod::create(Carbon::now()->startOfYear(), '1 month', Carbon::now()->endOfYear()))
->map(fn(Carbon $date) => $date->format('F'))
->toArray(),
'datasets' => [
[
'backgroundColor' => collect(range($start = 10, $start + 12))
->map(fn($eachNum) => $this->getColor($eachNum))
->map(fn($eachArray) => (string) Str::of($eachArray[0])->append(',')->append($eachArray[1])->append(',')->append($eachArray[2])->wrap('(', ')')->prepend('rgb'))
->values()
->toArray(),
'label' => 'Doughnut Chart Example',
'data' => collect(range(1,12))
->map(fn() => rand(150, 250))
->toArray(),
],
],
];
}
/**
* Generate the Color Code
*
*
* @param int $num
* @return array
* @link https: //stackoverflow.com/a/9186155/8487424
**/
public function getColor($num)
{
$hash = md5('color' . $num); // modify 'color' to get a different palette
return array(
hexdec(substr($hash, 0, 2)), // r
hexdec(substr($hash, 2, 2)), // g
hexdec(substr($hash, 4, 2))); //b
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment