@php $chartX = 80; $chartY = 30; $chartWidth = 680; $yPerSD = 340 / 6; $yPaddingPx = 0.5 * $yPerSD; $chartHeight = 6 * $yPerSD + 2 * $yPaddingPx; $centerY = $chartY + $chartHeight / 2; $topY = $centerY - 3 * $yPerSD; $bottomY = $centerY + 3 * $yPerSD; $clipTop = $chartY; $clipBottom = $chartY + $chartHeight; $daySecs = 86400; $dataDaysMap = []; foreach ($lots as $lot) { foreach ($lot['points'] as $p) { $day = (int) (($p['x'] / 1000) / $daySecs); $dataDaysMap[$day] = true; } } $dataDays = array_keys($dataDaysMap); sort($dataDays); $nDays = count($dataDays); $hasPoints = $nDays > 0; $singleDay = $nDays < 2; $timeRange = $singleDay ? 1 : $nDays - 1; $labelStep = ($nDays > 1 && $chartWidth / ($nDays - 1) < 40) ? (int) ceil(40 / ($chartWidth / ($nDays - 1))) : 1; $dayToIndex = array_flip($dataDays); $chartTitle = ''; if ($hasPoints) { $firstLot = $lots[0] ?? null; if ($firstLot) { $chartTitle = $firstLot['test_name'] . ' — ' . $firstLot['lot_name'] . ' / ' . $firstLot['lot_no']; } } @endphp @if($chartTitle && ($isFirstChart ?? false)) {{ $chartTitle }} @endif @foreach([-2.5, -1.5, -0.5, 0.5, 1.5, 2.5] as $hsd) @endforeach @foreach($dataDays as $dayIndex => $day) @php $xPos = $singleDay ? $chartX + $chartWidth / 2 : $chartX + ($dayIndex / $timeRange) * $chartWidth; @endphp @endforeach @foreach($lots as $lot) @php $polylinePoints = ''; foreach ($lot['points'] as $p) { if (abs($p['normY']) > 3.3) continue; $day = (int) (($p['x'] / 1000) / $daySecs); $dayIndex = $dayToIndex[$day] ?? -1; if ($dayIndex < 0) continue; $xPos = $singleDay ? $chartX + $chartWidth / 2 : $chartX + ($dayIndex / $timeRange) * $chartWidth; $yPos = $centerY - $p['normY'] * $yPerSD; if ($polylinePoints) { $polylinePoints .= ' '; } $polylinePoints .= number_format($xPos, 1, '.', '') . ',' . number_format($yPos, 1, '.', ''); } @endphp @if($polylinePoints && !$singleDay) @endif @foreach($lot['points'] as $p) @php $day = (int) (($p['x'] / 1000) / $daySecs); $dayIndex = $dayToIndex[$day] ?? -1; if ($dayIndex < 0) continue; $xPos = $singleDay ? $chartX + $chartWidth / 2 : $chartX + ($dayIndex / $timeRange) * $chartWidth; $yPos = $centerY - $p['normY'] * $yPerSD; @endphp @if(abs($p['normY']) > 3.3) @if($p['normY'] > 0) {{ number_format($p['y'], 1) }} @else {{ number_format($p['y'], 1) }} @endif @elseif($p['is_outlier'] ?? false) @else @endif @endforeach @endforeach @php $labelDefs = [ ['text' => '3S', 'lineY' => $topY, 'bold' => false], ['text' => '2S', 'lineY' => $centerY - 2 * $yPerSD, 'bold' => false], ['text' => '1S', 'lineY' => $centerY - $yPerSD, 'bold' => false], ['text' => 'Mean', 'lineY' => $centerY, 'bold' => true], ['text' => '-1S', 'lineY' => $centerY + $yPerSD, 'bold' => false], ['text' => '-2S', 'lineY' => $centerY + 2 * $yPerSD, 'bold' => false], ['text' => '-3S', 'lineY' => $bottomY, 'bold' => false], ]; @endphp @foreach($labelDefs as $lbl) @php $textY = $lbl['bold'] ? $lbl['lineY'] - 4 : $lbl['lineY'] + 5; $pillY = $lbl['bold'] ? $lbl['lineY'] - 11 : $lbl['lineY'] - 6; @endphp {{ $lbl['text'] }} @endforeach @foreach($dataDays as $dayIndex => $day) @php $xPos = $singleDay ? $chartX + $chartWidth / 2 : $chartX + ($dayIndex / $timeRange) * $chartWidth; $dateLabel = date('d-M', $day * $daySecs); @endphp @if($singleDay || $dayIndex % $labelStep == 0 || $dayIndex == $nDays - 1) {{ $dateLabel }} @endif @endforeach @if($lots && $hasPoints && ($isFirstChart ?? false)) @php $legX = $chartX + $chartWidth + 15; $legY = $chartY + 8; $legEndY = $legY; foreach ($lots as $lot) { $legEndY += !empty($lot['test_name']) ? 32 : 20; } @endphp @foreach($lots as $lot) @php $legendItem = $lot['lot_name'] . ' / ' . $lot['lot_no']; @endphp {{ $legendItem }} @if(!empty($lot['test_name'])) {{ $lot['test_name'] }} @php $legY += 32; @endphp @else @php $legY += 20; @endphp @endif @endforeach @endif @if(!$hasPoints) No data points in selected range @endif