{"spec_id":"bland-altman-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// bland-altman-basic: Bland-Altman Agreement Plot\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 89/100 | Updated: 2026-11-08\nimport { ScatterChart } from \"@mui/x-charts/ScatterChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\nimport { useYScale, useDrawingArea } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst LOA_Z = 1.96; // 95% limits of agreement: bias ± 1.96 SD of the differences\nconst X_PAD_SHARE = 0.04; // axis padding as a share of the data span\nconst Y_PAD_SHARE = 0.08;\n\n// Columns: set these to your data's column names\nconst CUFF = \"Oscillometric cuff (mmHg)\"; // role: method1\nconst REFERENCE = \"Mercury reference (mmHg)\"; // role: method2\n\n// Data: replace this block with your own records, one object per row\n// Systolic blood pressure of 70 patients, each measured once with a new\n// oscillometric cuff and once with the mercury-column reference standard.\nconst data = (() => {\n  let seed = 42;\n  const rand = () => {\n    seed = (seed * 1103515245 + 12345) & 0x7fffffff;\n    return seed / 0x7fffffff;\n  };\n  const gaussian = () => {\n    const u1 = Math.max(rand(), 1e-9);\n    const u2 = rand();\n    return Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);\n  };\n  const reference = Array.from({ length: 70 }, () => 122 + gaussian() * 15);\n  // The cuff reads slightly high and its scatter widens with the pressure\n  return reference.map((value) => ({\n    [CUFF]: value + 3.4 + gaussian() * (4 + value * 0.02),\n    [REFERENCE]: value,\n  }));\n})();\n\n// Plot: reads only data and the constants above\nconst n = data.length;\nconst points = data.map((d, i) => ({\n  id: i,\n  x: (d[CUFF] + d[REFERENCE]) / 2,\n  y: d[CUFF] - d[REFERENCE],\n}));\nconst bias = points.reduce((sum, p) => sum + p.y, 0) / n;\nconst sdDiff = Math.sqrt(points.reduce((sum, p) => sum + (p.y - bias) ** 2, 0) / (n - 1));\nconst upperLimit = bias + LOA_Z * sdDiff;\nconst lowerLimit = bias - LOA_Z * sdDiff;\n\nconst xs = points.map((p) => p.x);\nconst xMin = Math.min(...xs);\nconst xMax = Math.max(...xs);\nconst xPad = (xMax - xMin) * X_PAD_SHARE;\nconst ys = [...points.map((p) => p.y), upperLimit, lowerLimit];\nconst yMin = Math.min(...ys);\nconst yMax = Math.max(...ys);\nconst yPad = (yMax - yMin) * Y_PAD_SHARE;\n\n// Axis titles name both methods; the shared unit comes from the column names\nconst cuffName = CUFF.replace(/\\s*\\([^)]*\\)\\s*$/, \"\");\nconst referenceName = REFERENCE.replace(/\\s*\\([^)]*\\)\\s*$/, \"\");\nconst unit = CUFF.match(/\\(([^)]+)\\)\\s*$/)?.[1] ?? \"\";\nconst unitSuffix = unit ? ` ${unit}` : \"\";\nconst xLabel = `Mean of ${cuffName} and ${referenceName}${unit ? ` (${unit})` : \"\"}`;\nconst yLabel = `${cuffName} − ${referenceName}${unit ? ` (${unit})` : \"\"}`;\n\nconst references = [\n  { value: upperLimit, name: `+${LOA_Z} SD`, color: t.inkSoft, weight: 400, dash: \"8 6\", stroke: 1.75 },\n  { value: bias, name: \"Bias\", color: t.ink, weight: 600, dash: undefined, stroke: 2.5 },\n  { value: lowerLimit, name: `−${LOA_Z} SD`, color: t.inkSoft, weight: 400, dash: \"8 6\", stroke: 1.75 },\n];\n\nconst TITLE = \"bland-altman-basic · javascript · muix · anyplot.ai\";\nconst subtitle = `n = ${n} paired readings · SD of the differences ${sdDiff.toFixed(1)}${unitSuffix}`;\nconst TITLE_H = 76;\n\nfunction LimitLabels() {\n  const yScale = useYScale();\n  const area = useDrawingArea();\n  const x = area.left + area.width + 14;\n  return (\n    <g>\n      {references.map((r) => (\n        <text key={r.name} x={x} y={yScale(r.value)} fill={r.color} fontSize={15} fontWeight={r.weight}>\n          <tspan x={x} dy=\"-0.25em\">\n            {r.name}\n          </tspan>\n          <tspan x={x} dy=\"1.25em\">\n            {`${r.value.toFixed(1).replace(\"-\", \"−\")}${unitSuffix}`}\n          </tspan>\n        </text>\n      ))}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\", background: t.pageBg }}>\n      <div style={{ height: TITLE_H, display: \"flex\", flexDirection: \"column\", alignItems: \"center\", justifyContent: \"center\", gap: 6, paddingTop: 10 }}>\n        <div style={{ fontSize: 22, fontWeight: 600, color: t.ink }}>{TITLE}</div>\n        <div style={{ fontSize: 15, color: t.inkSoft }}>{subtitle}</div>\n      </div>\n      <ScatterChart\n        width={width}\n        height={height - TITLE_H}\n        skipAnimation\n        hideLegend\n        grid={{ horizontal: true, vertical: true }}\n        series={[{ id: \"pairs\", data: points, markerSize: 7, color: t.palette[0] }]}\n        xAxis={[\n          {\n            min: xMin - xPad,\n            max: xMax + xPad,\n            label: xLabel,\n            height: 64,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            min: yMin - yPad,\n            max: yMax + yPad,\n            label: yLabel,\n            width: 84,\n            labelStyle: { fontSize: 16, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        margin={{ left: 24, right: 150, top: 16, bottom: 16 }}\n        sx={{\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiScatterChart-marker\": { fillOpacity: 0.6, stroke: t.pageBg, strokeWidth: 1 },\n        }}\n      >\n        {references.map((r) => (\n          <ChartsReferenceLine\n            key={r.name}\n            y={r.value}\n            lineStyle={{ stroke: r.color, strokeWidth: r.stroke, strokeDasharray: r.dash }}\n          />\n        ))}\n        <LimitLabels />\n      </ScatterChart>\n    </div>\n  );\n}\n"}