Files
siakad/resources/views/components/ui/chart.blade.php
T

38 lines
1.7 KiB
PHP

{{--
Grafik Chart.js dengan data nyata. type: bar | line | doughnut. datasets: [['label' => ..., 'data' => [...]]].
Tabel tersembunyi menyediakan data yang sama bagi pembaca layar.
--}}
@props(['title', 'type' => 'bar', 'labels' => [], 'datasets' => [], 'description' => null, 'height' => '16rem'])
@php
$type = in_array($type, ['bar', 'line', 'doughnut'], true) ? $type : 'bar';
$id = 'chart-'.\Illuminate\Support\Str::random(6);
@endphp
<x-glass.panel as="figure" {{ $attributes->class('chart') }}>
<figcaption class="panel-title">
<h2>{{ $title }}</h2>
</figcaption>
@if ($description) <p class="stat__hint">{{ $description }}</p> @endif
@if (count($labels) === 0)
<x-ui.empty icon="chart-bar" title="Belum ada data" description="Grafik tampil setelah data tersedia." />
@else
<div class="chart__canvas" style="height: {{ $height }}" x-data="chartCanvas(@js(['type' => $type, 'labels' => $labels, 'datasets' => $datasets]))">
<canvas x-ref="canvas" id="{{ $id }}" role="img" aria-label="{{ $title }}"></canvas>
</div>
{{-- Tabel tidak mematuhi width/overflow: sembunyikan lewat pembungkus agar tidak melebar. --}}
<div class="visually-hidden"><table>
<caption>{{ $title }}</caption>
<thead>
<tr><th scope="col">Kategori</th>@foreach ($datasets as $ds)<th scope="col">{{ $ds['label'] ?? 'Nilai' }}</th>@endforeach</tr>
</thead>
<tbody>
@foreach ($labels as $i => $label)
<tr><th scope="row">{{ $label }}</th>@foreach ($datasets as $ds)<td>{{ $ds['data'][$i] ?? 0 }}</td>@endforeach</tr>
@endforeach
</tbody>
</table></div>
@endif
</x-glass.panel>