{"spec_id":"line-reaction-coordinate","library":"muix","language":"javascript","code":"// anyplot.ai\n// line-reaction-coordinate: Reaction Coordinate Energy Diagram\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 87/100 | Updated: 2026-10-24\n\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { LinePlot } from \"@mui/x-charts/LineChart\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\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 FONT = \"system-ui, -apple-system, sans-serif\";\nconst Y_PAD_BOTTOM_FRAC = 0.2;\nconst Y_PAD_TOP_FRAC = 0.25;\nconst EA_X_OFFSET_FRAC = 0.14;\nconst DH_X_FRAC = 0.86;\nconst REACTANT_LABEL_X_FRAC = 0.02;\nconst PRODUCT_LABEL_X_FRAC = 0.72;\n\n// Columns: set these to your data's column names\nconst COORD = \"Reaction coordinate\"; // role: reaction_coordinate\nconst ENERGY = \"Potential energy (kJ/mol)\"; // role: energy\nconst STAGE = \"Reaction stage\"; // role: stage\n\n// Data: replace this block with your own records\nconst N_POINTS = 201;\nconst data: Array<Record<string, number | string>> = [];\nfor (let i = 0; i < N_POINTS; i++) {\n  const x = Math.round((i / (N_POINTS - 1)) * 1000) / 1000;\n  let energy: number;\n  if (x <= 0.14) {\n    energy = 50;\n  } else if (x <= 0.4) {\n    const u = (x - 0.14) / (0.4 - 0.14);\n    energy = 50 + (120 - 50) * (3 * u * u - 2 * u * u * u);\n  } else if (x <= 0.72) {\n    const u = (x - 0.4) / (0.72 - 0.4);\n    energy = 120 + (20 - 120) * (3 * u * u - 2 * u * u * u);\n  } else {\n    energy = 20;\n  }\n  const stage =\n    x <= 0.14\n      ? \"Reactants\"\n      : Math.abs(x - 0.4) < 0.003\n        ? \"Transition State ‡\"\n        : x >= 0.72\n          ? \"Products\"\n          : \"Pathway\";\n  data.push({\n    [COORD]: x,\n    [ENERGY]: Math.round(energy * 10) / 10,\n    [STAGE]: stage,\n  });\n}\n\n// Plot: reads only data and the constants above\nconst sortedData = [...data].sort((a, b) => Number(a[COORD]) - Number(b[COORD]));\nconst xData = sortedData.map((r) => Number(r[COORD]));\nconst yData = sortedData.map((r) => Number(r[ENERGY]));\n\nconst reactantRow = sortedData[0];\nconst productRow = sortedData[sortedData.length - 1];\nlet peakRow = sortedData[0];\nfor (const row of sortedData) {\n  if (Number(row[ENERGY]) > Number(peakRow[ENERGY])) {\n    peakRow = row;\n  }\n}\n\nconst eReactants = Number(reactantRow[ENERGY]);\nconst eProducts = Number(productRow[ENERGY]);\nconst eTransition = Number(peakRow[ENERGY]);\nconst xPeak = Number(peakRow[COORD]);\nconst xMin = xData[0];\nconst xMax = xData[xData.length - 1];\nconst xSpan = xMax - xMin;\n\nconst stageReactants = String(reactantRow[STAGE]);\nconst stageProducts = String(productRow[STAGE]);\nconst stageTransition = String(peakRow[STAGE]);\n\nconst unitMatch = ENERGY.match(/\\(([^)]+)\\)/);\nconst ENERGY_UNIT = unitMatch ? unitMatch[1] : \"\";\nconst activationEnergy = Math.round(eTransition - eReactants);\nconst enthalpyChange = Math.round(eProducts - eReactants);\nconst enthalpyText =\n  enthalpyChange < 0 ? `−${Math.abs(enthalpyChange)}` : `+${enthalpyChange}`;\n\nconst minEnergy = Math.min(...yData);\nconst maxEnergy = Math.max(...yData);\nconst energyRange = maxEnergy - minEnergy;\nconst yMin = minEnergy - energyRange * Y_PAD_BOTTOM_FRAC;\nconst yMax = maxEnergy + energyRange * Y_PAD_TOP_FRAC;\n\nfunction EnergyAnnotations() {\n  const xScale = useXScale(\"coord\") as (v: number) => number;\n  const yScale = useYScale(\"energy\") as (v: number) => number;\n\n  const yR = yScale(eReactants);\n  const yTS = yScale(eTransition);\n  const yP = yScale(eProducts);\n\n  const xEa = xScale(xPeak + xSpan * EA_X_OFFSET_FRAC);\n  const xDH = xScale(xMin + xSpan * DH_X_FRAC);\n\n  const AW = 5;\n  const AH = 9;\n\n  const ink = t.ink;\n  const inkSoft = t.inkSoft;\n  const green = t.palette[0];\n\n  return (\n    <g fontFamily={FONT}>\n      {/* Transition state peak marker — hollow circle on the curve */}\n      <circle\n        cx={xScale(xPeak)}\n        cy={yTS}\n        r={7}\n        fill={t.pageBg}\n        stroke={green}\n        strokeWidth={2.5}\n      />\n\n      {/* Ea arrow: reactant level -> transition state */}\n      <line\n        x1={xEa}\n        y1={yTS + AH + 1}\n        x2={xEa}\n        y2={yR - AH - 1}\n        stroke={ink}\n        strokeWidth={1.5}\n      />\n      <polygon\n        points={`${xEa - AW},${yTS + AH} ${xEa + AW},${yTS + AH} ${xEa},${yTS}`}\n        fill={ink}\n      />\n      <polygon\n        points={`${xEa - AW},${yR - AH} ${xEa + AW},${yR - AH} ${xEa},${yR}`}\n        fill={ink}\n      />\n      <text\n        x={xEa + 12}\n        y={(yTS + yR) / 2}\n        fill={ink}\n        fontSize={18}\n        dominantBaseline=\"middle\"\n        fontWeight=\"500\"\n      >\n        {`Ea = ${activationEnergy} ${ENERGY_UNIT}`}\n      </text>\n\n      {/* ΔH arrow: reactant level -> product level */}\n      <line\n        x1={xDH}\n        y1={yR + AH + 1}\n        x2={xDH}\n        y2={yP - AH - 1}\n        stroke={inkSoft}\n        strokeWidth={1.5}\n        strokeDasharray=\"6 3\"\n      />\n      <polygon\n        points={`${xDH - AW},${yR + AH} ${xDH + AW},${yR + AH} ${xDH},${yR}`}\n        fill={inkSoft}\n      />\n      <polygon\n        points={`${xDH - AW},${yP - AH} ${xDH + AW},${yP - AH} ${xDH},${yP}`}\n        fill={inkSoft}\n      />\n      <text\n        x={xDH + 12}\n        y={(yR + yP) / 2}\n        fill={inkSoft}\n        fontSize={18}\n        dominantBaseline=\"middle\"\n      >\n        {`ΔH = ${enthalpyText} ${ENERGY_UNIT}`}\n      </text>\n\n      {/* Transition State label (above peak marker) */}\n      <text\n        x={xScale(xPeak)}\n        y={yTS - 22}\n        fill={ink}\n        fontSize={18}\n        textAnchor=\"middle\"\n        fontWeight=\"500\"\n      >\n        {stageTransition}\n      </text>\n\n      {/* Reactants label — offset above flat reactant shelf */}\n      <text\n        x={xScale(xMin + xSpan * REACTANT_LABEL_X_FRAC)}\n        y={yR - 18}\n        fill={ink}\n        fontSize={18}\n        textAnchor=\"start\"\n      >\n        {`${stageReactants} (${eReactants} ${ENERGY_UNIT})`}\n      </text>\n\n      {/* Products label — offset below flat product shelf so it clears ΔH arrow */}\n      <text\n        x={xScale(xMin + xSpan * PRODUCT_LABEL_X_FRAC)}\n        y={yP + 28}\n        fill={ink}\n        fontSize={18}\n        textAnchor=\"start\"\n      >\n        {`${stageProducts} (${eProducts} ${ENERGY_UNIT})`}\n      </text>\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const titleH = 52;\n\n  return (\n    <Box\n      sx={{\n        width,\n        height,\n        bgcolor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        pt: \"14px\",\n      }}\n    >\n      <Typography\n        sx={{\n          color: t.ink,\n          fontSize: 22,\n          fontWeight: 500,\n          textAlign: \"center\",\n          lineHeight: 1.2,\n          mb: \"4px\",\n          fontFamily: \"inherit\",\n        }}\n      >\n        line-reaction-coordinate · javascript · muix · anyplot.ai\n      </Typography>\n\n      <ChartsContainer\n        width={width}\n        height={height - titleH}\n        skipAnimation\n        margin={{ left: 24, right: 64, top: 36, bottom: 20 }}\n        sx={{\n          \"& .MuiLineChart-line\": {\n            strokeWidth: 3.5,\n            strokeLinecap: \"round\",\n            strokeLinejoin: \"round\",\n          },\n        }}\n        series={[\n          {\n            type: \"line\",\n            id: \"energy\",\n            data: yData,\n            xAxisId: \"coord\",\n            yAxisId: \"energy\",\n            color: t.palette[0],\n            showMark: false,\n            curve: \"linear\",\n            label: ENERGY,\n          },\n        ]}\n        xAxis={[\n          {\n            id: \"coord\",\n            data: xData,\n            scaleType: \"linear\",\n            min: xMin,\n            max: xMax,\n            height: 58,\n            label: COORD,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft, fontFamily: FONT },\n            labelStyle: { fontSize: 16, fontWeight: \"500\", fill: t.inkSoft, fontFamily: FONT },\n            disableLine: true,\n            disableTicks: true,\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"energy\",\n            min: yMin,\n            max: yMax,\n            width: 88,\n            label: ENERGY,\n            valueFormatter: (v: number | null) =>\n              v === null ? \"\" : `${Math.round(v)}`,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft, fontFamily: FONT },\n            labelStyle: { fontSize: 16, fontWeight: \"500\", fill: t.inkSoft, fontFamily: FONT },\n            disableLine: true,\n            disableTicks: true,\n          },\n        ]}\n      >\n        <ChartsReferenceLine\n          y={eReactants}\n          axisId=\"energy\"\n          lineStyle={{\n            stroke: t.inkSoft,\n            strokeDasharray: \"8 5\",\n            strokeWidth: 1.2,\n            opacity: 0.65,\n          }}\n        />\n        <ChartsReferenceLine\n          y={eProducts}\n          axisId=\"energy\"\n          lineStyle={{\n            stroke: t.inkSoft,\n            strokeDasharray: \"8 5\",\n            strokeWidth: 1.2,\n            opacity: 0.65,\n          }}\n        />\n\n        <LinePlot skipAnimation />\n        <ChartsXAxis axisId=\"coord\" />\n        <ChartsYAxis axisId=\"energy\" />\n        <EnergyAnnotations />\n      </ChartsContainer>\n    </Box>\n  );\n}\n"}