From ee2747a16227f839c9ea83e31710dcceccef9fac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Wed, 13 Nov 2024 16:12:57 +0000 Subject: [PATCH] wip --- app/Livewire/Pages/ElectionResults.php | 9 ++-- app/View/Components/StackedBar.php | 7 ++- app/helpers.php | 4 +- resources/views/components/legend.blade.php | 25 ++++++---- .../views/components/stacked-bar.blade.php | 48 ++++++++++++------- .../livewire/pages/election-results.blade.php | 25 ++++------ 6 files changed, 67 insertions(+), 51 deletions(-) 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 +
  • merge(['style' => "--color-custom: {$color};"])->class(['flex gap-2', 'items-center flex-wrap' => blank($image), 'items-start' => filled($image)]) }}> + @if ($image) + {{ $label }} -@endphp +
    +
    + + {{ $label }} +
    + {{ $description }} +
    + @else + -
  • merge([ - 'class' => 'flex items-center gap-2', - 'style' => "--color-custom: {$color};", -]) }}> - - {{ $label }} - {{ $description }} + {{ $label }} + {{ $description }} + @endif
  • diff --git a/resources/views/components/stacked-bar.blade.php b/resources/views/components/stacked-bar.blade.php index 00d952e..f1830d7 100644 --- a/resources/views/components/stacked-bar.blade.php +++ b/resources/views/components/stacked-bar.blade.php @@ -2,23 +2,35 @@ $cursor = 0; @endphp - - @foreach ($items as $item) - +
    + + @foreach ($items as $item) + - @php - $cursor += $item['percent']; - @endphp - @endforeach + @php + $cursor += $item['percent']; + @endphp + @endforeach - @if ($showThreshold) - - @endif - + @if ($showThreshold) + + @endif + + +
      + @foreach ($items as $item) + + @endforeach +
    +
    diff --git a/resources/views/livewire/pages/election-results.blade.php b/resources/views/livewire/pages/election-results.blade.php index da9696b..62ddbe7 100644 --- a/resources/views/livewire/pages/election-results.blade.php +++ b/resources/views/livewire/pages/election-results.blade.php @@ -4,19 +4,7 @@ {{ $this->form }} @if (filled($this->aggregate)) -
    - - -
      - @foreach ($this->aggregate as $item) - - @endforeach - -
    -
    + @endif + Partid / Alianță / Candidat independent @@ -45,16 +34,20 @@ @foreach ($this->aggregate as $row) + + + + - {{ data_get($row, 'name', '-') }} + {{ $row['name'] }} - {{ data_get($row, 'votes', '-') }} + {{ Number::format($row['votes']) }} - {{ data_get($row, 'percent', '-') }} + {{ Number::percentage($row['percent'], 2) }} @endforeach