Skip to content

Commit

Permalink
Configure chart
Browse files Browse the repository at this point in the history
  • Loading branch information
timvandijck committed Mar 26, 2024
1 parent 36f5804 commit 5ae0610
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 41 deletions.
38 changes: 0 additions & 38 deletions app/Filament/Widgets/NewUsersChartWidget.php

This file was deleted.

66 changes: 66 additions & 0 deletions app/Filament/Widgets/PurchasesPerProductWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Filament\Widgets;

use App\Domain\Shop\Models\Purchase;
use Filament\Widgets\ChartWidget;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

class PurchasesPerProductWidget extends ChartWidget
{
protected static ?string $heading = 'Chart';

protected function getData(): array
{
$data = Purchase::query()
->whereHas('receipt', function (Builder $query): void {
$query->where('amount', '!=', 0);
})
->select('purchasables.product_id', 'products.title', DB::raw('count(*) as aggregate'))
->join('purchasables', 'purchasables.id', '=', 'purchases.purchasable_id')
->join('products', 'products.id', '=', 'purchasables.product_id')
->groupBy('product_id')
->get();

return [
// 'datasets' => $data->map(function ($item) {
// return [
// 'label' => $item->title,
// 'data' => [$item->aggregate],
// ];
// })->toArray(),
'datasets' => [
[
'label' => 'Purchases Per Product',
'data' => $data->pluck('aggregate')->toArray(),
'backgroundColor' => [
'#4dc9f6',
'#f67019',
'#f87171',
'#537bc4',
'#acc236',
'#166a8f',
'#00a950',
'#58595b',
'#8549ba',
'#6366f1',
'#bef264'
],
],
],
'labels' => $data->pluck('title')->toArray(),
];
}

protected function getType(): string
{
return 'pie';
}

protected function getFilters(): ?array
{
return [
];
}
}
2 changes: 0 additions & 2 deletions app/Nova/Metrics/Earnings.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public function calculate(NovaRequest $request)
->dollars()
->showSumValue();

ray($result);

return $result;
}

Expand Down
3 changes: 2 additions & 1 deletion app/Providers/Filament/AdminPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Providers\Filament;

use App\Filament\Widgets\NewUsersChartWidget;
use App\Filament\Widgets\PurchasesPerProductWidget;
use App\Filament\Widgets\StatsOverviewWidget;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
Expand Down Expand Up @@ -40,6 +40,7 @@ public function panel(Panel $panel): Panel
])
->widgets([
StatsOverviewWidget::class,
PurchasesPerProductWidget::class,
])
->middleware([
EncryptCookies::class,
Expand Down

0 comments on commit 5ae0610

Please sign in to comment.