Skip to content

Commit

Permalink
Add URL Click Chart (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed May 7, 2024
1 parent a74f757 commit 1689824
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
54 changes: 54 additions & 0 deletions app/Livewire/Chart/UrlClickChart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Livewire\Chart;

use App\Models\Url;
use App\Models\Visit;
use Carbon\CarbonPeriod;
use Filament\Widgets\ChartWidget;
use Flowframe\Trend\Trend;
use Flowframe\Trend\TrendValue;

class UrlClickChart extends ChartWidget
{
protected static ?string $maxHeight = '250px';

public Url $model;

protected function getData(): array
{
$startDate = now()->subQuarter();
$endDate = now();
$carbon = CarbonPeriod::create($startDate, $endDate)->toArray();
$label = collect($carbon)->map(fn ($date) => $date->format('M d'))
->toArray();

$visitModel = Visit::where('url_id', $this->model->id);
$data = Trend::query($visitModel)
->between(start: $startDate, end: $endDate)
->perDay()
->count();

return [
'datasets' => [
[
'label' => 'Clicks',
'data' => $data->map(fn (TrendValue $value) => $value->aggregate),
'backgroundColor' => '#36A2EB',
'borderColor' => '#9BD0F5',
],
],
'labels' => $label,
];
}

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

public function getDescription(): ?string
{
return 'Number of clicks on each day for 4 months';
}
}
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"blade-ui-kit/blade-icons": "^1.6",
"embed/embed": "^4.4",
"endroid/qr-code": "^5.0",
"filament/widgets": "^3.2",
"flowframe/laravel-trend": "^0.2.0",
"laravel/fortify": "^1.20",
"laravel/framework": "^11.0",
"laravel/tinker": "^2.9",
Expand Down Expand Up @@ -56,7 +58,8 @@
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
"@php artisan package:discover --ansi",
"@php artisan filament:upgrade"
],
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
Expand Down
5 changes: 5 additions & 0 deletions resources/views/frontend/short.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
</div>
</div>
</div>

<div class="mt-20">
@livewire(\App\Livewire\Chart\UrlClickChart::class, ['model' => $url])
</div>

</div>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions resources/views/layouts/frontend.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<title>{{config('app.name').' - Simplify your links'}}</title>

@livewireStyles
@filamentStyles
@vite(['resources/css/main.css', 'resources/js/app.js'])
</head>

Expand All @@ -18,6 +19,7 @@
@yield('content')

@livewireScripts
@filamentScripts
</body>

</html>
1 change: 1 addition & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default {
"./app/Livewire/**/*Table.php",
"./vendor/power-components/livewire-powergrid/resources/views/**/*.php",
"./vendor/power-components/livewire-powergrid/src/Themes/Tailwind.php",
'./vendor/filament/**/*.blade.php',
],
presets: [
presetPowerGrid,
Expand Down

0 comments on commit 1689824

Please sign in to comment.