{"spec_id":"line-arrhenius","library":"muix","language":"javascript","code":"// anyplot.ai\n// line-arrhenius: Arrhenius Plot for Reaction Kinetics\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 88/100 | Updated: 2026-11-08\nimport { ChartsDataProvider } from \"@mui/x-charts/ChartsDataProvider\";\nimport { ChartsWrapper } from \"@mui/x-charts/ChartsWrapper\";\nimport { ChartsSurface } from \"@mui/x-charts/ChartsSurface\";\nimport { ChartsLegend } from \"@mui/x-charts/ChartsLegend\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsTooltip } from \"@mui/x-charts/ChartsTooltip\";\nimport { LinePlot } from \"@mui/x-charts/LineChart\";\nimport { ScatterPlot } from \"@mui/x-charts/ScatterChart\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst R_GAS = 8.314; // gas constant, J/(mol·K)\nconst INV_T_SCALE = 1000; // the x axis shows 1000 / T, so the slope reads in kK\nconst X_PAD_SHARE = 0.05; // share of the 1000/T range added beside the outermost points\nconst Y_PAD_SHARE = 0.08; // share of the ln k range added above and below\nconst NOTE_AT_SHARE = 0.5; // the slope note sits at this share of the fitted x range\n\n// Columns: set these to your data's column names\nconst TEMPERATURE = \"Temperature (K)\"; // role: temperature_K\nconst RATE = \"Rate constant k (s⁻¹)\"; // role: rate_constant_k\n\n// Data: replace this block with your own records, one object per measurement\n// First-order thermal decomposition of N₂O₅ (2 N₂O₅ → 4 NO₂ + O₂) measured at 11\n// temperatures from 278 K to 338 K: Ea = 103 kJ/mol, A = 4.94 × 10¹³ s⁻¹ (ln A = 31.53),\n// with a little scatter on each ln k.\nconst data = (() => {\n  let seed = 42;\n  const rand = () => {\n    seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;\n    return seed / 4294967296;\n  };\n  return [278, 284, 290, 296, 302, 308, 314, 320, 326, 332, 338].map((kelvin) => {\n    const lnK = 31.53 - 103000 / (8.314 * kelvin) + (rand() - 0.5) * 0.3;\n    return { [TEMPERATURE]: kelvin, [RATE]: Number(Math.exp(lnK).toPrecision(4)) };\n  });\n})();\n\n// Plot: reads only data and the constants above\nconst rows = data\n  .filter((d) => d[TEMPERATURE] > 0 && d[RATE] > 0)\n  .sort((a, b) => a[TEMPERATURE] - b[TEMPERATURE]);\nconst invT = rows.map((d) => INV_T_SCALE / d[TEMPERATURE]);\nconst lnK = rows.map((d) => Math.log(d[RATE]));\nconst n = rows.length;\n\n// Least-squares fit ln k = intercept + slope · (1000 / T)\nconst xMean = invT.reduce((a, b) => a + b, 0) / n;\nconst yMean = lnK.reduce((a, b) => a + b, 0) / n;\nlet sxy = 0;\nlet sxx = 0;\nlet syy = 0;\nfor (let i = 0; i < n; i += 1) {\n  sxy += (invT[i] - xMean) * (lnK[i] - yMean);\n  sxx += (invT[i] - xMean) ** 2;\n  syy += (lnK[i] - yMean) ** 2;\n}\nconst slope = sxy / sxx;\nconst intercept = yMean - slope * xMean;\nconst r2 = (sxy * sxy) / (sxx * syy);\n// slope = −Ea / (R · 1000), so the slope per kelvin is −Ea/R and Ea in kJ/mol follows from it\nconst slopeKelvin = slope * INV_T_SCALE;\nconst eaKj = (-slopeKelvin * R_GAS) / 1000;\n\nconst xLow = Math.min(...invT);\nconst xHigh = Math.max(...invT);\nconst xPad = (xHigh - xLow) * X_PAD_SHARE;\nconst fitX = [xLow, xHigh];\nconst fitY = fitX.map((x) => intercept + slope * x);\nconst yLow = Math.min(...lnK, ...fitY);\nconst yHigh = Math.max(...lnK, ...fitY);\nconst yPad = (yHigh - yLow) * Y_PAD_SHARE;\nconst noteX = xLow + (xHigh - xLow) * NOTE_AT_SHARE;\n\nconst rateUnit = RATE.match(/\\(([^)]+)\\)/)?.[1];\nconst Y_LABEL = rateUnit ? `ln(k / ${rateUnit})` : \"ln k\";\nconst SLOPE_NOTE = `slope = −Ea/R = ${slopeKelvin < 0 ? \"−\" : \"\"}${Math.abs(slopeKelvin).toLocaleString(\"en-US\", { maximumFractionDigits: 0 })} K`;\nconst EA_NOTE = `Ea = ${eaKj.toFixed(1)} kJ/mol · R² = ${r2.toFixed(4)}`;\n\nconst TITLE = \"line-arrhenius · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${n} measurements, ${rows[0][TEMPERATURE]}–${rows[n - 1][TEMPERATURE]} K · least-squares fit of ln k against 1/T`;\nconst HEADER_H = 84;\nconst LEGEND_H = 44;\n\nfunction SlopeNote() {\n  const xScale = useXScale(\"invT\");\n  const yScale = useYScale();\n  const x = xScale(noteX) + 24;\n  const y = yScale(intercept + slope * noteX) - 52;\n  return (\n    <text x={x} y={y} fontSize={16} fill={t.ink}>\n      <tspan x={x} fontWeight={600}>\n        {SLOPE_NOTE}\n      </tspan>\n      <tspan x={x} dy={22}>\n        {EA_NOTE}\n      </tspan>\n    </text>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const axisStyle = {\n    tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n    labelStyle: { fontSize: 16, fill: t.ink },\n  };\n\n  return (\n    <Box sx={{ width, height, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\", overflow: \"hidden\" }}>\n      <Box sx={{ height: HEADER_H, pt: \"14px\", textAlign: \"center\", boxSizing: \"border-box\" }}>\n        <Typography sx={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.3 }}>{TITLE}</Typography>\n        <Typography sx={{ fontSize: 15, color: t.inkSoft, lineHeight: 1.6 }}>{SUBTITLE}</Typography>\n      </Box>\n      <ChartsDataProvider\n        width={width}\n        height={height - HEADER_H - LEGEND_H}\n        margin={{ top: 8, right: 48, bottom: 8, left: 20 }}\n        skipAnimation\n        series={[\n          {\n            type: \"line\",\n            id: \"fit\",\n            data: fitY,\n            xAxisId: \"invT\",\n            label: \"Linear fit\",\n            color: t.palette[1],\n            curve: \"linear\",\n            showMark: false,\n            labelMarkType: \"line\",\n          },\n          {\n            type: \"scatter\",\n            id: \"measured\",\n            data: rows.map((_, i) => ({ id: i, x: invT[i], y: lnK[i] })),\n            xAxisId: \"invT\",\n            label: \"Measured rate constants\",\n            color: t.palette[0],\n            markerSize: 9,\n            valueFormatter: (v) => (v ? `T = ${rows[v.id][TEMPERATURE]} K, ln k = ${v.y.toFixed(2)}` : \"\"),\n          },\n        ]}\n        xAxis={[\n          {\n            id: \"invT\",\n            data: fitX,\n            scaleType: \"linear\",\n            position: \"bottom\",\n            min: xLow - xPad,\n            max: xHigh + xPad,\n            label: `${INV_T_SCALE} / T (K⁻¹)`,\n            height: 64,\n            tickNumber: 8,\n            valueFormatter: (v: number) => v.toFixed(2),\n            ...axisStyle,\n          },\n          {\n            // The same scale as the bottom axis, ticked at every measured temperature\n            id: \"kelvin\",\n            scaleType: \"linear\",\n            position: \"top\",\n            min: xLow - xPad,\n            max: xHigh + xPad,\n            tickInterval: invT,\n            tickLabelInterval: () => true,\n            label: TEMPERATURE,\n            height: 64,\n            valueFormatter: (v: number) => String(Math.round(INV_T_SCALE / v)),\n            ...axisStyle,\n          },\n        ]}\n        yAxis={[\n          {\n            min: yLow - yPad,\n            max: yHigh + yPad,\n            width: 72,\n            label: Y_LABEL,\n            ...axisStyle,\n          },\n        ]}\n      >\n        <ChartsWrapper legendPosition={{ vertical: \"bottom\", horizontal: \"center\" }} legendDirection=\"horizontal\">\n          <ChartsLegend sx={{ fontSize: 15, color: t.ink, gap: \"36px\", pt: \"8px\" }} />\n          <ChartsSurface\n            sx={{\n              \"& .MuiLineChart-line\": { strokeWidth: 3 },\n              \"& .MuiChartsGrid-line\": { stroke: t.grid },\n            }}\n          >\n            <ChartsGrid horizontal />\n            <LinePlot skipAnimation />\n            <ScatterPlot />\n            <SlopeNote />\n            <ChartsXAxis axisId=\"invT\" />\n            <ChartsXAxis axisId=\"kelvin\" />\n            <ChartsYAxis />\n          </ChartsSurface>\n          <ChartsTooltip trigger=\"item\" />\n        </ChartsWrapper>\n      </ChartsDataProvider>\n    </Box>\n  );\n}\n"}