{"spec_id":"titration-curve","library":"muix","language":"javascript","code":"// anyplot.ai\n// titration-curve: Acid-Base Titration Curve\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 88/100 | Updated: 2026-11-08\n\nimport { LineChart } from \"@mui/x-charts/LineChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst PH_MIN = 0; // the full pH scale the y axis spans\nconst PH_MAX = 14;\nconst DERIV_HEADROOM = 0.25; // share of the derivative peak left free above it on the right axis\nconst TITLE = \"titration-curve · javascript · muix · anyplot.ai\";\nconst TITLE_H = 76;\nconst LEGEND_H = 40;\n\n// Columns: set these to your data's column names\nconst VOLUME = \"Volume of NaOH added (mL)\"; // role: volume_ml\nconst PH = \"pH\"; // role: ph\n\n// Data: replace this block with your own records, one object per row\n// 25 mL of 0.1 M HCl titrated with 0.1 M NaOH, 201 readings from 0 to 50 mL.\n// pH from the exact charge balance [H+] - [OH-] = excess acid, with Kw = 1e-14.\nconst data = (() => {\n  const acidVolume = 25;\n  const acidMolarity = 0.1;\n  const baseMolarity = 0.1;\n  const readings = 201;\n  const maxVolume = 50;\n  const kw = 1e-14;\n  return Array.from({ length: readings }, (_, i) => {\n    const volume = (i / (readings - 1)) * maxVolume;\n    const excess = (acidMolarity * acidVolume - baseMolarity * volume) / (acidVolume + volume);\n    const root = Math.sqrt(excess * excess + 4 * kw);\n    const hydrogen = excess >= 0 ? (excess + root) / 2 : kw / ((root - excess) / 2);\n    return {\n      [VOLUME]: Math.round(volume * 100) / 100,\n      [PH]: Math.round(-Math.log10(hydrogen) * 1000) / 1000,\n    };\n  });\n})();\n\n// Plot: reads only data and the constants above\nconst rows = data\n  .filter((d) => d[VOLUME] != null && d[PH] != null)\n  .sort((a, b) => a[VOLUME] - b[VOLUME]);\nconst volumes = rows.map((d) => d[VOLUME]);\nconst phValues = rows.map((d) => d[PH]);\n\n// dpH/dV by central differences, one-sided at the two ends\nconst derivative = rows.map((_, i) => {\n  const lo = Math.max(0, i - 1);\n  const hi = Math.min(rows.length - 1, i + 1);\n  const dv = volumes[hi] - volumes[lo];\n  return dv === 0 ? null : (phValues[hi] - phValues[lo]) / dv;\n});\n\n// Equivalence point: the reading where the derivative peaks\nconst eqIndex = derivative.reduce(\n  (best, v, i) => (v != null && v > (derivative[best] ?? -Infinity) ? i : best),\n  0,\n);\nconst eqVolume = volumes[eqIndex];\nconst eqPh = phValues[eqIndex];\nconst derivPeak = derivative[eqIndex];\n\nconst VOLUME_UNIT = VOLUME.match(/\\(([^)]+)\\)\\s*$/)?.[1] ?? \"\";\nconst DERIV_NAME = `d${PH}/dV`;\nconst DERIV_LABEL = `${DERIV_NAME}${VOLUME_UNIT ? ` (per ${VOLUME_UNIT})` : \"\"}`;\nconst EQ_LABEL = `Equivalence point · ${eqVolume.toFixed(2)} ${VOLUME_UNIT}, ${PH} ${eqPh.toFixed(2)}`;\nconst SUBTITLE = `${rows.length} readings, ${volumes[0]}–${volumes[volumes.length - 1]} ${VOLUME_UNIT} of titrant · equivalence at the ${DERIV_NAME} maximum`;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <Box sx={{ width, height, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <Box sx={{ height: TITLE_H, px: \"40px\", pt: \"16px\", boxSizing: \"border-box\" }}>\n        <Typography sx={{ color: t.ink, fontSize: \"22px\", fontWeight: 600, lineHeight: 1.2 }}>\n          {TITLE}\n        </Typography>\n        <Typography sx={{ color: t.inkSoft, fontSize: \"15px\", lineHeight: 1.2, mt: \"8px\" }}>\n          {SUBTITLE}\n        </Typography>\n      </Box>\n\n      <LineChart\n        width={width}\n        height={height - TITLE_H - LEGEND_H}\n        skipAnimation\n        colors={t.palette}\n        grid={{ horizontal: true }}\n        xAxis={[\n          {\n            id: \"volume\",\n            data: volumes,\n            scaleType: \"linear\",\n            domainLimit: \"nice\",\n            tickNumber: 10,\n            label: VOLUME,\n            height: 64,\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"ph\",\n            min: PH_MIN,\n            max: PH_MAX,\n            tickNumber: 8,\n            label: PH,\n            width: 64,\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n          {\n            id: \"deriv\",\n            position: \"right\",\n            min: 0,\n            max: derivPeak * (1 + DERIV_HEADROOM),\n            label: DERIV_LABEL,\n            width: 80,\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        series={[\n          {\n            id: \"ph\",\n            data: phValues,\n            label: PH,\n            yAxisId: \"ph\",\n            showMark: false,\n            curve: \"monotoneX\",\n          },\n          {\n            id: \"deriv\",\n            data: derivative,\n            label: DERIV_LABEL,\n            yAxisId: \"deriv\",\n            showMark: false,\n            curve: \"linear\",\n            area: true,\n          },\n        ]}\n        margin={{ top: 16, right: 24, bottom: 12, left: 24 }}\n        slotProps={{\n          legend: {\n            direction: \"horizontal\",\n            position: { vertical: \"top\", horizontal: \"end\" },\n            sx: { fontSize: 16, color: t.ink },\n          },\n        }}\n        sx={{\n          '& .MuiLineChart-line[data-series=\"ph\"]': { strokeWidth: 3.5 },\n          '& .MuiLineChart-line[data-series=\"deriv\"]': { strokeWidth: 2 },\n          '& .MuiLineChart-area[data-series=\"deriv\"]': { fillOpacity: 0.15 },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n        }}\n      >\n        <ChartsReferenceLine\n          x={eqVolume}\n          axisId=\"volume\"\n          label={EQ_LABEL}\n          labelAlign=\"start\"\n          spacing={{ x: 10, y: 10 }}\n          lineStyle={{ strokeDasharray: \"8 5\", stroke: t.inkSoft, strokeWidth: 1.5 }}\n          labelStyle={{ fontSize: 15, fill: t.ink, fontWeight: 500 }}\n        />\n      </LineChart>\n    </Box>\n  );\n}\n"}