@props([ 'series' => [], // [['label' => 'Mon', 'value' => 1200], …] 'height' => 220, 'formatter' => null, // callable for the value labels 'slot' => 1, // categorical slot (1–4) 'label' => 'Value', ]) @php /* * Column chart for change over time. * * Server-rendered SVG on purpose: the dashboard must paint on the first * response, and a POS runs on hardware where a charting bundle is a real * cost. Bars carry a 2px surface gap, 4px rounded data-ends anchored to * the baseline, and a native tooltip per mark. */ $rows = collect($series)->values(); $max = max(1, (float) $rows->max('value')); $count = max(1, $rows->count()); $width = 640; $padTop = 16; $padBottom = 28; $padLeft = 8; $plotHeight = $height - $padTop - $padBottom; $slotWidth = ($width - $padLeft * 2) / $count; $barWidth = max(3, min(46, $slotWidth - 6)); // the 2px+ surface gap $format = $formatter ?? fn ($value) => number_format((float) $value); // Only label the peak and the latest column — a number on every bar is noise. $peakIndex = $rows->search(fn ($row) => (float) $row['value'] === $max); $lastIndex = $count - 1; @endphp
@if ($rows->isEmpty() || $max <= 0)
No data for this period.
@else {{-- Recessive gridlines --}} @foreach ([0, 0.25, 0.5, 0.75, 1] as $fraction) @php $y = $padTop + $plotHeight * (1 - $fraction); @endphp @endforeach @foreach ($rows as $index => $row) @php $value = (float) $row['value']; $barHeight = max(2, $plotHeight * ($value / $max)); $x = $padLeft + $index * $slotWidth + ($slotWidth - $barWidth) / 2; $y = $padTop + $plotHeight - $barHeight; @endphp {{ $row['label'] }}: {{ $format($value) }} {{-- Direct labels on the peak and the most recent column only. --}} @if ($index === $peakIndex || $index === $lastIndex) {{ $format($value) }} @endif {{-- Thin out x labels once the bars get crowded. --}} @if ($count <= 14 || $index % (int) ceil($count / 12) === 0) {{ $row['label'] }} @endif @endforeach {{-- Table view: the relief the palette's contrast WARN requires. --}}
View as table @foreach ($rows as $row) @endforeach
Period{{ $label }}
{{ $row['label'] }} {{ $format($row['value']) }}
@endif