ZH
apps/cryo-studio / src / Gui / MemoryChart.php · commit 76aa68d
<?phpnamespace App\Gui;use App\Cryo\Studio;use JumPHP\Gui\Theme;use JumPHP\Gui\Widget;use JumPHP\Renderer\Renderer;/** * A curve of memory over time (a .cryom series): `points` = list of [seconds from the start, bytes], `marks` = list of * [seconds, label] drawn as dashed vertical lines with their label on top. Used with `class: App\Gui\MemoryChart` in * the form file. The area under the curve is filled with the accent colour, light. * * @property list<array{0: float, 1: float}> $points * @property list<array{0: float, 1: string}> $marks */final class MemoryChart extends Widget{    public const DEFAULTS = ['points' => [], 'marks' => [], 'height' => 240, 'empty' => 'No samples'];    protected function content(Theme $t, float $maxWidth, string $breakpoint): array    {        return [min($maxWidth, $t->px(600)), $t->px(240)];    }    /** The plot area inside the frame (pixels): x, y, w, h. @return array{0: float, 1: float, 2: float, 3: float} */    public function plotArea(Theme $t): array    {        $left = $t->px(66);        $top = $t->px(44);        return [$this->x + $left, $this->y + $top, max(10, $this->w - $left - $t->px(14)), max(10, $this->h - $top - $t->px(26))];    }    /** min / max of the values with a little room, never a flat range. @return array{0: float, 1: float} */    public function range(): array    {        $values = array_map(fn (array $p) => (float)$p[1], (array)$this->props['points']);        if ($values === []) {            return [0.0, 1.0];        }        $lo = min($values);        $hi = max($values);        $pad = max(($hi - $lo) * 0.08, 1024.0);        return [max(0.0, $lo - $pad), $hi + $pad];    }    public function render(Renderer $r, Theme $t): void    {        $t->frame($r, $this->x, $this->y, $this->w, $this->h, $t->color('surface'), $t->color('border'));        $points = array_values((array)$this->props['points']);        $small = $t->font($t->fontSize * 0.8);        [$px, $py, $pw, $ph] = $this->plotArea($t);        if (count($points) < 2) {            $small->draw($r, $this->tr((string)$this->props['empty']), $this->x + $this->w / 2, $this->y + $this->h / 2, 1, $t->color('muted'), 'center');            return;        }        [$lo, $hi] = $this->range();        $t0 = (float)$points[0][0];        $span = max(0.001, (float)end($points)[0] - $t0);        $sx = fn (float $time) => $px + ($time - $t0) / $span * $pw;        $sy = fn (float $v) => $py + $ph - ($v - $lo) / ($hi - $lo) * $ph;        // grid and axes: 4 bands of memory, 5 times        for ($i = 0; $i <= 4; $i++) {            $v = $lo + ($hi - $lo) * $i / 4;            $y = $sy($v);            $r->fillRect($px, $y, $pw, max(1, round($t->scale)), $t->color($i === 0 ? 'border' : 'track'));            $small->draw($r, Studio::bytes($v), $px - $t->px(6), $y - $small->height() / 2, 1, $t->color('muted'), 'right');        }        for ($i = 0; $i <= 4; $i++) {            $time = $span * $i / 4;            $small->draw($r, sprintf('%.1f s', $time), $px + $pw * $i / 4, $py + $ph + $t->px(5), 1, $t->color('muted'), $i === 0 ? 'left' : ($i === 4 ? 'right' : 'center'));        }        // the area under the curve, one column per few pixels, then the curve itself (2 px wide)        $accent = $t->color('accent');        $fill = $accent->withAlpha(48);        $step = max(1.0, round($t->scale * 2));        $k = 0;        for ($x = $px; $x < $px + $pw; $x += $step) {            $time = $t0 + ($x - $px) / $pw * $span;            while ($k < count($points) - 2 && (float)$points[$k + 1][0] < $time) {                $k++;            }            [$ta, $va] = $points[$k];            [$tb, $vb] = $points[$k + 1];            $f = (float)$tb > (float)$ta ? max(0, min(1, ($time - (float)$ta) / ((float)$tb - (float)$ta))) : 0;            $y = $sy((float)$va + ((float)$vb - (float)$va) * $f);            $r->fillRect($x, $y, $step, $py + $ph - $y, $fill);        }        $thick = max(1, (int)round($t->scale));        for ($i = 1; $i < count($points); $i++) {            $x1 = $sx((float)$points[$i - 1][0]);            $y1 = $sy((float)$points[$i - 1][1]);            $x2 = $sx((float)$points[$i][0]);            $y2 = $sy((float)$points[$i][1]);            for ($d = 0; $d <= $thick; $d++) {                $r->drawLine($x1, $y1 - $d, $x2, $y2 - $d, $accent);            }        }        // marks: a dashed line, the label above the plot on two rows so that close labels do not overlap        $danger = $t->color('danger');        $dash = $t->px(4);        foreach (array_values((array)$this->props['marks']) as $n => [$time, $label]) {            $x = $sx((float)$time);            if ($x < $px - 1 || $x > $px + $pw + 1) {                continue;            }            for ($y = $py; $y < $py + $ph; $y += 2 * $dash) {                $r->fillRect($x, $y, $thick, min($dash, $py + $ph - $y), $danger);            }            $ly = $this->y + $t->px(6) + ($n % 2) * ($small->lineHeight());            $align = $x - $small->width((string)$label) / 2 < $this->x + $t->px(4) ? 'left' : ($x + $small->width((string)$label) / 2 > $this->x + $this->w - $t->px(4) ? 'right' : 'center');            $small->draw($r, (string)$label, $x, $ly, 1, $danger, $align);        }    }}