@props(['module']) @php $labels = collect(data_get($module, 'data.labels', []))->values(); $dates = collect(data_get($module, 'data.dates', []))->values(); $dataset = collect(data_get($module, 'data.datasets', []))->first() ?? []; $values = collect(data_get($dataset, 'data', []))->map(fn ($value) => $value === null ? null : (float) $value)->values(); $numericValues = $values->filter(fn ($value) => $value !== null)->values(); $unit = (string) data_get($dataset, 'unit', ''); $formatNumber = fn (mixed $value): string => rtrim(rtrim(number_format((float) $value, 1, ',', ' '), '0'), ','); $formatValue = fn (mixed $value): string => trim($formatNumber($value) . ' ' . $unit); $buildAxis = function (float $min, float $max): array { if ($min === $max) { $max = $min + 1; } $rawStep = ($max - $min) / 4; $magnitude = 10 ** floor(log10(max($rawStep, 1))); $normalized = $rawStep / $magnitude; $step = match (true) { $normalized <= 1 => $magnitude, $normalized <= 2 => 2 * $magnitude, $normalized <= 5 => 5 * $magnitude, default => 10 * $magnitude, }; $axisMin = $min < 0 ? floor($min / $step) * $step : 0; $axisMax = max($axisMin + $step, ceil($max / $step) * $step); return [$axisMin, $axisMax, max($step, $axisMax - $axisMin)]; }; [$axisMin, $maxValue, $rangeValue] = $buildAxis((float) ($numericValues->min() ?? 0), (float) ($numericValues->max() ?? 0)); $count = max(1, $values->count()); $plotLeft = 2; $plotRight = 98; $plotTop = 8; $plotBottom = 88; $plotWidth = $plotRight - $plotLeft; $plotHeight = $plotBottom - $plotTop; $points = []; $dots = []; foreach ($values as $index => $value) { if ($value === null) { continue; } $x = $count === 1 ? 50 : $plotLeft + (($index / max(1, $count - 1)) * $plotWidth); $y = $plotBottom - ((($value - $axisMin) / $rangeValue) * $plotHeight); $points[] = number_format($x, 2, '.', '') . ',' . number_format($y, 2, '.', ''); $dots[] = [ 'x' => $x, 'y' => $y, 'value' => $value, 'label' => (string) ($labels[$index] ?? ''), 'date' => (string) ($dates[$index] ?? ''), ]; } $xAxisLabels = $labels ->map(fn ($label, $index) => [ 'label' => $label, 'x' => $labels->count() <= 1 ? 50 : $plotLeft + (($index / max(1, $labels->count() - 1)) * $plotWidth), 'index' => $index, ]) ->values(); $axisTicks = collect([1, 0.75, 0.5, 0.25, 0])->map(function (float $ratio) use ($axisMin, $rangeValue, $plotTop, $plotHeight) { return [ 'value' => $axisMin + ($rangeValue * $ratio), 'y' => $plotTop + ((1 - $ratio) * $plotHeight), ]; }); @endphp
@foreach ($axisTicks as $tick) @endforeach @if ($points !== []) @endif
@foreach ($dots as $dot) @endforeach
{{ data_get($dataset, 'label') }} @if (data_get($dataset, 'unit')) {{ data_get($dataset, 'unit') }} @endif