diff --git a/app/Livewire/Pages/ElectionResults.php b/app/Livewire/Pages/ElectionResults.php index c052f9b..3e4d11a 100644 --- a/app/Livewire/Pages/ElectionResults.php +++ b/app/Livewire/Pages/ElectionResults.php @@ -9,7 +9,6 @@ use App\Models\Vote; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; -use Illuminate\Support\Number; use Livewire\Attributes\Computed; use Livewire\Attributes\Layout; use stdClass; @@ -58,7 +57,7 @@ public function aggregate(): Collection locality: $this->locality, aggregate: true, ) - ->withVotable() + ->withVotable(true) ->get(); $total = $result->sum('votes'); @@ -66,7 +65,8 @@ public function aggregate(): Collection return $result ->map(fn (Vote $vote) => [ 'name' => $vote->votable->acronym ?? $vote->votable->name, - 'votes' => Number::format(ensureNumeric($vote->votes)), + 'image' => $vote->votable->getFirstMediaUrl(), + 'votes' => ensureNumeric($vote->votes), 'percent' => percent($vote->votes, $total), 'color' => hex2rgb($vote->votable->color ?? $this->fallbackColor), ]); @@ -106,8 +106,7 @@ public function data(): Collection return [ $vote->place => [ - 'value' => Number::format(ensureNumeric($vote->votes)), - 'percent' => percent($vote->votes, $vote->total_votes, formatted: true), + 'value' => percent($vote->votes, $vote->total_votes, formatted: true), 'color' => $votable->color, 'label' => $votable->getDisplayName(), ], diff --git a/app/View/Components/StackedBar.php b/app/View/Components/StackedBar.php index ceb7036..5d0c40c 100644 --- a/app/View/Components/StackedBar.php +++ b/app/View/Components/StackedBar.php @@ -32,12 +32,15 @@ protected function limit(Collection $items): Collection return $items; } + $totalVotes = $items->sum('votes'); + $items = $items->take($this->maxItems); $items->push([ - 'name' => 'Other', + 'name' => __('app.others'), 'percent' => 100 - $items->sum('percent'), - 'color' => $this->fallbackColor, + 'votes' => $totalVotes - $items->sum('votes'), + 'color' => hex2rgb($this->fallbackColor), ]); return $items; diff --git a/app/helpers.php b/app/helpers.php index 3fc8f45..e5fe75a 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use Illuminate\Support\Number; + if (! function_exists('ensureNumeric')) { function ensureNumeric(int|float|string $value): int|float { @@ -34,7 +36,7 @@ function percent(int|float|string $value, int|float|string $max, int $precision $percent = (float) number_format(min(100, max(0, $value / $max * 100)), $precision); if ($formatted) { - $percent = sprintf('%s%%', $percent); + $percent = Number::percentage($percent, $precision); } return $percent; diff --git a/resources/views/components/legend.blade.php b/resources/views/components/legend.blade.php index 2ebf910..47a7365 100644 --- a/resources/views/components/legend.blade.php +++ b/resources/views/components/legend.blade.php @@ -1,12 +1,19 @@ -@php +