-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* top counties widget * top localities widget
- Loading branch information
Showing
7 changed files
with
357 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Livewire\Charts; | ||
|
||
use App\Enums\DataLevel; | ||
use App\Models\County; | ||
use App\Models\Election; | ||
use App\Repositories\TurnoutRepository; | ||
use Filament\Support\Colors\Color; | ||
use Filament\Support\RawJs; | ||
use Filament\Widgets\ChartWidget; | ||
use Illuminate\Contracts\Support\Htmlable; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\HtmlString; | ||
|
||
class TopCountiesChart extends ChartWidget | ||
{ | ||
public Election $election; | ||
|
||
public ?Collection $topCounties = null; | ||
|
||
protected static ?string $maxHeight = '1200px'; | ||
|
||
protected static ?string $pollingInterval = null; | ||
|
||
public function getHeading(): Htmlable | ||
{ | ||
return new HtmlString(view('components.chart-heading', [ | ||
'title' => 'Topul județelor după prezență', | ||
'url' => route('front.elections.embed.top-counties', [ | ||
'election' => $this->election, | ||
]), | ||
])->render()); | ||
} | ||
|
||
protected function getTopCounties(): Collection | ||
{ | ||
if (blank($this->topCounties)) { | ||
$counties = County::pluck('name', 'id'); | ||
|
||
$this->topCounties = TurnoutRepository::getForLevel( | ||
election: $this->election, | ||
level: DataLevel::NATIONAL, | ||
toBase: true, | ||
) | ||
->map(function (object $turnout) use ($counties) { | ||
$turnout->name = $counties->get($turnout->place); | ||
$turnout->percent = percent($turnout->total, $turnout->initial_total); | ||
|
||
return $turnout; | ||
}) | ||
->sortByDesc('percent'); | ||
} | ||
|
||
return $this->topCounties; | ||
} | ||
|
||
protected function getData(): array | ||
{ | ||
$result = $this->getTopCounties(); | ||
|
||
$labels = []; | ||
$data = []; | ||
$backgroundColor = []; | ||
|
||
foreach ($result as $county) { | ||
$labels[] = $county->name; | ||
|
||
$backgroundColor[] = 'rgb(' . Color::Blue[400] . ')'; | ||
$data[] = percent($county->total, $county->initial_total); | ||
} | ||
|
||
return [ | ||
'labels' => $labels, | ||
'datasets' => [ | ||
[ | ||
'label' => 'Prezența', | ||
'borderWidth' => 0, | ||
'backgroundColor' => $backgroundColor, | ||
'data' => $data, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
protected function getType(): string | ||
{ | ||
return 'bar'; | ||
} | ||
|
||
protected function getOptions(): RawJs | ||
{ | ||
if ($this->getTopCounties()->isEmpty()) { | ||
return RawJs::make(<<<'JS' | ||
{ | ||
indexAxis: 'y', | ||
scales: { | ||
x: { | ||
ticks: { | ||
display: false | ||
}, | ||
}, | ||
}, | ||
events: [] | ||
} | ||
JS); | ||
} | ||
|
||
return RawJs::make(<<<'JS' | ||
{ | ||
maintainAspectRatio: false, | ||
aspectRatio: 0.15, | ||
indexAxis: 'y', | ||
scales: { | ||
y: { | ||
beginAtZero: true, | ||
stacked: true, | ||
}, | ||
x: { | ||
beginAtZero: true, | ||
}, | ||
}, | ||
plugins: { | ||
tooltip: { | ||
callbacks: { | ||
label: (context) => { | ||
let label = context.dataset.label; | ||
console.log(context); | ||
if (label) { | ||
label += ': '; | ||
} | ||
if (context.parsed.x !== null) { | ||
label += Math.abs(context.parsed.x) + ' %'; | ||
} | ||
return label; | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
JS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Livewire\Charts; | ||
|
||
use App\Enums\DataLevel; | ||
use App\Models\Election; | ||
use App\Repositories\TurnoutRepository; | ||
use Filament\Support\Colors\Color; | ||
use Filament\Support\RawJs; | ||
use Filament\Widgets\ChartWidget; | ||
use Illuminate\Contracts\Support\Htmlable; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\HtmlString; | ||
|
||
class TopLocalitiesChart extends ChartWidget | ||
{ | ||
public Election $election; | ||
|
||
public ?Collection $topLocalities = null; | ||
|
||
protected static ?string $maxHeight = '1200px'; | ||
|
||
protected static ?string $pollingInterval = null; | ||
|
||
public function getHeading(): Htmlable | ||
{ | ||
return new HtmlString(view('components.chart-heading', [ | ||
'title' => 'Top 10 mari orașe din România', | ||
'url' => route('front.elections.embed.top-localities', [ | ||
'election' => $this->election, | ||
]), | ||
])->render()); | ||
} | ||
|
||
protected function getTopLocalities(): Collection | ||
{ | ||
$localities = collect([ | ||
54975 => 'Cluj-Napoca', | ||
95060 => 'Iași', | ||
60419 => 'Constanța', | ||
155243 => 'Timișoara', | ||
40198 => 'Brașov', | ||
69900 => 'Craiova', | ||
75098 => 'Galați', | ||
26564 => 'Oradea', | ||
130534 => 'Ploiești', | ||
]); | ||
|
||
if (blank($this->topLocalities)) { | ||
$this->topLocalities = $localities | ||
->map(fn (string $name, int $locality) => TurnoutRepository::getForLevel( | ||
election: $this->election, | ||
level: DataLevel::NATIONAL, | ||
locality: $locality, | ||
toBase: true, | ||
aggregate: true, | ||
)) | ||
->prepend(TurnoutRepository::getForLevel( | ||
election: $this->election, | ||
level: DataLevel::NATIONAL, | ||
county: 403, // București | ||
toBase: true, | ||
aggregate: true, | ||
)) | ||
->map(function (object $turnout) use ($localities) { | ||
$turnout->name = $localities->get($turnout->place, 'București'); | ||
$turnout->percent = percent($turnout->total, $turnout->initial_total); | ||
|
||
return $turnout; | ||
}) | ||
->sortByDesc('percent'); | ||
} | ||
|
||
return $this->topLocalities; | ||
} | ||
|
||
protected function getData(): array | ||
{ | ||
$result = $this->getTopLocalities(); | ||
|
||
$labels = []; | ||
$data = []; | ||
$backgroundColor = []; | ||
|
||
foreach ($result as $county) { | ||
$labels[] = $county->name; | ||
|
||
$backgroundColor[] = 'rgb(' . Color::Blue[400] . ')'; | ||
$data[] = percent($county->total, $county->initial_total); | ||
} | ||
|
||
return [ | ||
'labels' => $labels, | ||
'datasets' => [ | ||
[ | ||
'label' => 'Prezența', | ||
'borderWidth' => 0, | ||
'backgroundColor' => $backgroundColor, | ||
'data' => $data, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
protected function getType(): string | ||
{ | ||
return 'bar'; | ||
} | ||
|
||
protected function getOptions(): RawJs | ||
{ | ||
if ($this->getTopLocalities()->isEmpty()) { | ||
return RawJs::make(<<<'JS' | ||
{ | ||
indexAxis: 'y', | ||
scales: { | ||
x: { | ||
ticks: { | ||
display: false | ||
}, | ||
}, | ||
}, | ||
events: [] | ||
} | ||
JS); | ||
} | ||
|
||
return RawJs::make(<<<'JS' | ||
{ | ||
indexAxis: 'y', | ||
scales: { | ||
y: { | ||
beginAtZero: true, | ||
stacked: true, | ||
}, | ||
x: { | ||
beginAtZero: true, | ||
}, | ||
}, | ||
plugins: { | ||
tooltip: { | ||
callbacks: { | ||
label: (context) => { | ||
let label = context.dataset.label; | ||
if (label) { | ||
label += ': '; | ||
} | ||
if (context.parsed.x !== null) { | ||
label += Math.abs(context.parsed.x) + ' %'; | ||
} | ||
return label; | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
JS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Livewire\Embeds; | ||
|
||
use App\Livewire\Pages\ElectionTurnouts; | ||
use Illuminate\View\View; | ||
use Livewire\Attributes\Layout; | ||
|
||
class TopCountiesEmbed extends ElectionTurnouts | ||
{ | ||
#[Layout('components.layouts.embed')] | ||
public function render(): View | ||
{ | ||
$this->seo('Topul județelor după prezență'); | ||
|
||
return view('livewire.embeds.top-counties'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Livewire\Embeds; | ||
|
||
use App\Livewire\Pages\ElectionTurnouts; | ||
use Illuminate\View\View; | ||
use Livewire\Attributes\Layout; | ||
|
||
class TopLocalitiesEmbed extends ElectionTurnouts | ||
{ | ||
#[Layout('components.layouts.embed')] | ||
public function render(): View | ||
{ | ||
$this->seo('Top 10 mari orașe din România'); | ||
|
||
return view('livewire.embeds.top-localities'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div class="grid gap-8"> | ||
<livewire:charts.top-counties-chart :election="$election" /> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div class="grid gap-8"> | ||
<livewire:charts.top-localities-chart :election="$election" /> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters