39 lines
1.9 KiB
PHP
39 lines
1.9 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
|
|
{{-- Data di data-chart (bukan x-data) agar muat di tempat memperbarui grafik, bukan membuatnya ulang. --}}
|
|
<div class="chart__canvas" style="height: {{ $height }}" x-data="chartCanvas" data-chart="{{ json_encode(['type' => $type, 'labels' => array_values($labels), 'datasets' => $datasets], JSON_THROW_ON_ERROR) }}">
|
|
<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>
|