@php $chartX = 80; $chartY = 30; $chartWidth = 680; $chartHeight = 396.667; // unchanged plot height // Reference SD from the first lot with a usable SD (the controller // floors a missing SD to 1) so the report window can mirror the // on-screen chart's axisY: mean ± (3·SD + 1). $refSd = 1.0; foreach ($lots as $lot) { if (!empty($lot['sd']) && (float) $lot['sd'] > 0) { $refSd = (float) $lot['sd']; break; } } $halfWindowUnits = 3 * $refSd + 1; $yPerUnit = $chartHeight / (2 * $halfWindowUnits); $yPerSD = $refSd * $yPerUnit; $centerY = $chartY + $chartHeight / 2; $topY = $centerY - 3 * $yPerSD; $bottomY = $centerY + 3 * $yPerSD; $clipTop = $chartY; $clipBottom = $chartY + $chartHeight; $daySecs = 86400; // Flatten every point across all lots, sort by time, and give each an // index. Points are then spaced EVENLY by index (one tick per point), // which mirrors the on-screen CanvasJS chart (xValueType "label") and // stops same-day points from collapsing onto a single x position. $flat = []; foreach ($lots as $li => $lot) { foreach ($lot['points'] as $pi => $p) { $flat[] = ['li' => $li, 'pi' => $pi, 'x' => $p['x']]; } } usort($flat, function ($a, $b) { return ($a['x'] ?? 0) <=> ($b['x'] ?? 0); }); $N = count($flat); $hasPoints = $N > 0; $posOf = []; foreach ($flat as $idx => $f) { $posOf[$f['li'] . ':' . $f['pi']] = $idx; } $xAt = function ($idx) use ($chartX, $chartWidth, $N) { if ($N <= 1) { return round($chartX + $chartWidth / 2, 1); } return round($chartX + ($idx / ($N - 1)) * $chartWidth, 1); }; // Day label position: the first point of each day (in global time order). $dayLabelX = []; $dayLabelText = []; foreach ($flat as $idx => $f) { $day = (int) (($f['x'] / 1000) / $daySecs); if (!isset($dayLabelX[$day])) { $dayLabelX[$day] = $xAt($idx); $dayLabelText[$day] = date('d-M', $day * $daySecs); } } // Auto-skip day labels when they would overlap. $dayXs = array_values($dayLabelX); $labelStep = 1; for ($i = 1; $i < count($dayXs); $i++) { if (($dayXs[$i] - $dayXs[$i - 1]) < 40) { $labelStep = 2; break; } } $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 {{-- Vertical grid line per point (evenly spaced, like the on-screen chart) --}} @foreach($flat as $idx => $f) @endforeach @foreach($lots as $li => $lot) @php // Connect every point in time order; out-of-range points are // clipped by the plot area (same as the on-screen chart). $polylinePoints = ''; foreach ($lot['points'] as $pi => $p) { $idx = $posOf[$li . ':' . $pi] ?? null; if ($idx === null) continue; $xPos = $xAt($idx); $yPos = round($centerY - $p['normY'] * $yPerSD, 1); if ($polylinePoints) { $polylinePoints .= ' '; } $polylinePoints .= number_format($xPos, 1, '.', '') . ',' . number_format($yPos, 1, '.', ''); } @endphp @if($polylinePoints && $N > 1) @endif @foreach($lot['points'] as $pi => $p) @php $idx = $posOf[$li . ':' . $pi] ?? null; if ($idx === null) continue; $xPos = $xAt($idx); $yPos = round($centerY - $p['normY'] * $yPerSD, 1); @endphp @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 @php $dayIx = 0; $dayCount = count($dayLabelX); @endphp @foreach($dayLabelX as $day => $xPos) @if($dayIx % $labelStep == 0 || $dayIx == $dayCount - 1) {{ $dayLabelText[$day] }} @endif @php $dayIx++; @endphp @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