{"spec_id":"line-loss-training","library":"muix","language":"javascript","code":"// anyplot.ai\n// line-loss-training: Training Loss Curve\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 89/100 | Updated: 2026-10-23\n\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\nimport { useXScale, useDrawingArea } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Columns: set these to your data's column names\nconst EPOCH = \"Epoch\"; // role: epoch\nconst TRAIN_LOSS = \"Training loss\"; // role: train_loss\nconst VAL_LOSS = \"Validation loss\"; // role: val_loss\n\n// Data: replace this block with your own records, one object per row\n// Training and validation cross-entropy loss across 60 epochs; validation loss\n// diverges upward past epoch ~28 (overfitting).\nconst data = (() => {\n  function lcg(seed: number) {\n    let state = seed;\n    return () => {\n      state = (state * 1664525 + 1013904223) % 4294967296;\n      return state / 4294967296;\n    };\n  }\n  const rand = lcg(42);\n  const totalEpochs = 60;\n  const epochs = Array.from({ length: totalEpochs }, (_, i) => i + 1);\n  const trainArr = epochs.map(\n    (e) => 2.35 * Math.exp(-e / 17) + 0.08 + (rand() - 0.5) * 0.02,\n  );\n  const valArr = epochs.map((e) => {\n    const overfitPenalty = Math.max(0, e - 28) ** 2 * 0.00085;\n    return 2.5 * Math.exp(-e / 15.5) + 0.12 + overfitPenalty + (rand() - 0.5) * 0.035;\n  });\n\n  return epochs.map((e, i) => ({\n    [EPOCH]: e,\n    [TRAIN_LOSS]: trainArr[i],\n    [VAL_LOSS]: valArr[i],\n  }));\n})();\n\n// Plot: reads only data and the constants above\nconst TRAIN_GRADIENT_ID = \"lineLossTrainingTrainFill\";\nconst VAL_GRADIENT_ID = \"lineLossTrainingValFill\";\n\nconst epoch = data.map((d) => d[EPOCH]);\nconst trainLoss = data.map((d) => d[TRAIN_LOSS]);\nconst valLoss = data.map((d) => d[VAL_LOSS]);\nconst lastEpoch = epoch[epoch.length - 1];\n\nlet bestEpoch = epoch[0];\nlet bestValLoss = valLoss[0];\nvalLoss.forEach((v, i) => {\n  if (v < bestValLoss) {\n    bestValLoss = v;\n    bestEpoch = epoch[i];\n  }\n});\n\n// Highlights the post-early-stop overfitting window (bestEpoch → lastEpoch)\nfunction DivergenceZone() {\n  const xScale = useXScale() as any;\n  const drawingArea = useDrawingArea();\n  if (!xScale) return null;\n  const xStart = xScale(bestEpoch);\n  const xEnd = xScale(lastEpoch);\n  return (\n    <rect\n      x={xStart}\n      y={drawingArea.top}\n      width={Math.max(0, xEnd - xStart)}\n      height={drawingArea.height}\n      fill={t.amber}\n      fillOpacity={0.08}\n    />\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const titleHeight = 56;\n\n  return (\n    <div\n      style={{\n        width,\n        height,\n        background: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        fontFamily: \"'Roboto', 'Helvetica', 'Arial', sans-serif\",\n      }}\n    >\n      <div\n        style={{\n          height: titleHeight,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          fontSize: 22,\n          fontWeight: 600,\n          letterSpacing: \"0.2px\",\n          color: t.ink,\n        }}\n      >\n        line-loss-training · javascript · muix · anyplot.ai\n      </div>\n\n      <LineChart\n        width={width}\n        height={height - titleHeight}\n        skipAnimation\n        disableHitArea\n        series={[\n          {\n            id: \"train\",\n            data: trainLoss,\n            label: TRAIN_LOSS,\n            color: t.palette[0],\n            showMark: false,\n            curve: \"monotoneX\",\n            area: true,\n            baseline: 0,\n          },\n          {\n            id: \"val\",\n            data: valLoss,\n            label: VAL_LOSS,\n            color: t.palette[1],\n            showMark: false,\n            curve: \"monotoneX\",\n            area: true,\n            baseline: 0,\n          },\n        ]}\n        xAxis={[\n          {\n            height: 64,\n            data: epoch,\n            scaleType: \"linear\",\n            label: EPOCH,\n            tickMinStep: 5,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fontWeight: 500, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            width: 78,\n            label: \"Cross-Entropy Loss\",\n            min: 0,\n            valueFormatter: (v: number) => v.toFixed(1),\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fontWeight: 500, fill: t.ink },\n          },\n        ]}\n        grid={{ horizontal: true }}\n        sx={{\n          \"& .MuiChartsLegend-root\": { color: t.ink, pb: \"12px\" },\n          \"& .MuiChartsLegend-label\": {\n            fontSize: \"15px !important\",\n            color: t.ink,\n          },\n          \"& .MuiLineChart-line\": {\n            strokeWidth: \"3px\",\n          },\n          '& [data-series=\"train\"].MuiLineChart-area': {\n            fill: `url(#${TRAIN_GRADIENT_ID})`,\n          },\n          '& [data-series=\"val\"].MuiLineChart-area': {\n            fill: `url(#${VAL_GRADIENT_ID})`,\n          },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n        }}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"bottom\", horizontal: \"center\" },\n          },\n        }}\n        margin={{ top: 18, right: 40, bottom: 16, left: 40 }}\n      >\n        <defs>\n          <linearGradient id={TRAIN_GRADIENT_ID} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n            <stop offset=\"0%\" stopColor={t.palette[0]} stopOpacity={0.22} />\n            <stop offset=\"100%\" stopColor={t.palette[0]} stopOpacity={0.02} />\n          </linearGradient>\n          <linearGradient id={VAL_GRADIENT_ID} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n            <stop offset=\"0%\" stopColor={t.palette[1]} stopOpacity={0.22} />\n            <stop offset=\"100%\" stopColor={t.palette[1]} stopOpacity={0.02} />\n          </linearGradient>\n        </defs>\n        <DivergenceZone />\n        <ChartsReferenceLine\n          x={bestEpoch}\n          label={`Early-stop point · epoch ${bestEpoch}`}\n          labelAlign=\"start\"\n          lineStyle={{ stroke: t.ink, strokeDasharray: \"6 6\", strokeWidth: 1.5, strokeOpacity: 0.6 }}\n          labelStyle={{ fill: t.inkSoft, fontSize: 13 }}\n        />\n      </LineChart>\n    </div>\n  );\n}\n"}