{"spec_id":"forest-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// forest-basic: Meta-Analysis Forest Plot\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 87/100 | Updated: 2026-10-24\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\nimport { useXScale, useYScale, useDrawingArea } 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 TITLE = \"Systolic BP Reduction · forest-basic · javascript · muix · anyplot.ai\";\nconst POOLED_LABEL = \"Pooled Effect (Random Effects)\";\nconst NULL_LABEL = \"No difference\";\nconst NULL_EFFECT = 0;\nconst Z_95 = 1.95996;\n\nconst X_PAD_SHARE = 0.12;\nconst Y_PAD_TOP = 0.75;\nconst Y_PAD_BOTTOM = 0.95;\nconst POOLED_ROW_GAP = 1.6;\nconst MIN_MARKER_HALF = 5;\nconst MAX_MARKER_HALF = 11;\n\nconst TITLE_HEIGHT = 56;\nconst SUBTITLE_HEIGHT = 36;\nconst MARGIN = { top: 20, right: 48, bottom: 20, left: 16 };\nconst X_AXIS_HEIGHT = 56;\nconst Y_AXIS_WIDTH = 228;\n\n// Columns: set these to your data's column names\nconst STUDY = \"Study\"; // role: study\nconst EFFECT = \"Mean difference in SBP (mmHg)\"; // role: effect_size\nconst CI_LOWER = \"95% CI lower\"; // role: ci_lower\nconst CI_UPPER = \"95% CI upper\"; // role: ci_upper\nconst WEIGHT = \"Study weight\"; // role: weight\n\n// Data: replace this block with your own records\nconst data: Record<string, string | number>[] = [\n  { [STUDY]: \"Anderson et al. 2019\", [EFFECT]: -4.2, [CI_LOWER]: -7.1, [CI_UPPER]: -1.3, [WEIGHT]: 11.9 },\n  { [STUDY]: \"Chen et al. 2020\", [EFFECT]: -6.8, [CI_LOWER]: -9.5, [CI_UPPER]: -4.1, [WEIGHT]: 15.1 },\n  { [STUDY]: \"Dubois et al. 2020\", [EFFECT]: -3.1, [CI_LOWER]: -6.9, [CI_UPPER]: 0.7, [WEIGHT]: 8.8 },\n  { [STUDY]: \"Fernandez et al. 2021\", [EFFECT]: -8.5, [CI_LOWER]: -11.2, [CI_UPPER]: -5.8, [WEIGHT]: 16.0 },\n  { [STUDY]: \"Garcia et al. 2021\", [EFFECT]: -5.0, [CI_LOWER]: -8.8, [CI_UPPER]: -1.2, [WEIGHT]: 10.5 },\n  { [STUDY]: \"Halvorsen et al. 2022\", [EFFECT]: -7.3, [CI_LOWER]: -10.0, [CI_UPPER]: -4.6, [WEIGHT]: 15.7 },\n  { [STUDY]: \"Ivanova et al. 2022\", [EFFECT]: -2.0, [CI_LOWER]: -5.5, [CI_UPPER]: 1.5, [WEIGHT]: 7.9 },\n  { [STUDY]: \"Kowalski et al. 2023\", [EFFECT]: -6.1, [CI_LOWER]: -8.7, [CI_UPPER]: -3.5, [WEIGHT]: 14.1 },\n];\n\n// Plot: reads only data and the constants above\nconst nStudies = data.length;\nconst studyRecords = data.map((r, i) => {\n  const name = String(r[STUDY]);\n  const effect = Number(r[EFFECT]);\n  const lower = Number(r[CI_LOWER]);\n  const upper = Number(r[CI_UPPER]);\n  const weight = Number(r[WEIGHT]);\n  const se = (upper - lower) / (2 * Z_95);\n  const invVar = 1 / (se * se);\n  return { name, effect, lower, upper, weight, se, invVar, y: nStudies - i };\n});\n\nconst sumInvVar = studyRecords.reduce((s, r) => s + r.invVar, 0);\nconst fixedMean = studyRecords.reduce((s, r) => s + r.invVar * r.effect, 0) / sumInvVar;\nconst qStat = studyRecords.reduce((s, r) => s + r.invVar * Math.pow(r.effect - fixedMean, 2), 0);\nconst df = nStudies - 1;\nconst cDenom = sumInvVar - studyRecords.reduce((s, r) => s + r.invVar * r.invVar, 0) / sumInvVar;\nconst tauSq = Math.max(0, (qStat - df) / cDenom);\n\nconst reWeights = studyRecords.map((r) => 1 / (r.se * r.se + tauSq));\nconst sumReWeights = reWeights.reduce((s, w) => s + w, 0);\nconst pooledEffect = studyRecords.reduce((s, r, idx) => s + reWeights[idx] * r.effect, 0) / sumReWeights;\nconst pooledSe = Math.sqrt(1 / sumReWeights);\nconst pooledLower = pooledEffect - Z_95 * pooledSe;\nconst pooledUpper = pooledEffect + Z_95 * pooledSe;\n\nconst maxWeight = Math.max(...studyRecords.map((s) => s.weight));\n\n// Square area proportional to weight: side scales with sqrt(weight)\nconst rows = studyRecords.map((s) => ({\n  ...s,\n  halfSize: Math.max(MIN_MARKER_HALF, MAX_MARKER_HALF * Math.sqrt(s.weight / maxWeight)),\n}));\n\nconst overallY = 1 - POOLED_ROW_GAP;\nconst yTicks = [...rows.map((r) => r.y), overallY];\nconst yLabels = new Map<number, string>(\n  yTicks.map((y) => [y, y === overallY ? POOLED_LABEL : rows.find((r) => r.y === y)!.name]),\n);\n\nconst rawXMin = Math.min(...rows.map((s) => s.lower), pooledLower, NULL_EFFECT);\nconst rawXMax = Math.max(...rows.map((s) => s.upper), pooledUpper, NULL_EFFECT);\nconst rawXSpan = rawXMax - rawXMin;\nconst xMin = rawXMin - rawXSpan * X_PAD_SHARE;\nconst xMax = rawXMax + rawXSpan * X_PAD_SHARE;\n\nfunction ForestMarks() {\n  const xScale = useXScale() as (v: number) => number;\n  const yScale = useYScale() as (v: number) => number;\n  const drawingArea = useDrawingArea();\n\n  const dividerY = yScale((rows[rows.length - 1].y + overallY) / 2);\n  const capHalf = 6;\n\n  const cyOverall = yScale(overallY);\n  const xL = xScale(pooledLower);\n  const xC = xScale(pooledEffect);\n  const xU = xScale(pooledUpper);\n  const halfH = 13;\n\n  return (\n    <g>\n      <line\n        x1={drawingArea.left}\n        x2={drawingArea.left + drawingArea.width}\n        y1={dividerY}\n        y2={dividerY}\n        stroke={t.grid}\n        strokeWidth={1}\n      />\n\n      {rows.map((r) => {\n        const cx = xScale(r.effect);\n        const cy = yScale(r.y);\n        const x1 = xScale(r.lower);\n        const x2 = xScale(r.upper);\n        const half = r.halfSize;\n        return (\n          <g key={r.name}>\n            <line x1={x1} x2={x2} y1={cy} y2={cy} stroke={t.palette[0]} strokeWidth={2.2} />\n            <line x1={x1} x2={x1} y1={cy - capHalf} y2={cy + capHalf} stroke={t.palette[0]} strokeWidth={2.2} />\n            <line x1={x2} x2={x2} y1={cy - capHalf} y2={cy + capHalf} stroke={t.palette[0]} strokeWidth={2.2} />\n            <rect x={cx - half} y={cy - half} width={half * 2} height={half * 2} fill={t.palette[0]} />\n          </g>\n        );\n      })}\n\n      <g>\n        <polygon\n          points={`${xL},${cyOverall} ${xC},${cyOverall - halfH} ${xU},${cyOverall} ${xC},${cyOverall + halfH}`}\n          fill={t.ink}\n          stroke={t.pageBg}\n          strokeWidth={1}\n        />\n      </g>\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const chartWidth = window.ANYPLOT_SIZE.width;\n  const chartHeight = window.ANYPLOT_SIZE.height - TITLE_HEIGHT - SUBTITLE_HEIGHT;\n  const subtitle = `${EFFECT} · squares sized by ${WEIGHT} · diamond = pooled effect`;\n\n  return (\n    <Box\n      sx={{\n        width: chartWidth,\n        height: window.ANYPLOT_SIZE.height,\n        display: \"flex\",\n        flexDirection: \"column\",\n      }}\n    >\n      <Box\n        sx={{\n          height: TITLE_HEIGHT,\n          display: \"flex\",\n          alignItems: \"flex-end\",\n          justifyContent: \"center\",\n          flexShrink: 0,\n        }}\n      >\n        <Typography sx={{ color: t.ink, fontSize: 22, fontWeight: 600 }}>{TITLE}</Typography>\n      </Box>\n      <Box\n        sx={{\n          height: SUBTITLE_HEIGHT,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          flexShrink: 0,\n        }}\n      >\n        <Typography sx={{ color: t.inkSoft, fontSize: 15 }}>{subtitle}</Typography>\n      </Box>\n\n      <ChartsContainer\n        skipAnimation\n        width={chartWidth}\n        height={chartHeight}\n        margin={MARGIN}\n        series={[]}\n        xAxis={[\n          {\n            scaleType: \"linear\",\n            min: xMin,\n            max: xMax,\n            tickMinStep: 1,\n            height: X_AXIS_HEIGHT,\n            label: EFFECT,\n            labelStyle: { fontSize: 15, fill: t.ink },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            scaleType: \"linear\",\n            min: overallY - Y_PAD_BOTTOM,\n            max: nStudies + Y_PAD_TOP,\n            width: Y_AXIS_WIDTH,\n            tickInterval: yTicks,\n            valueFormatter: (v: number) => yLabels.get(v) ?? \"\",\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            tickSize: 4,\n            disableLine: true,\n          },\n        ]}\n        sx={{ \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeOpacity: 0.5 } }}\n      >\n        <ChartsGrid vertical />\n        <ChartsReferenceLine\n          x={NULL_EFFECT}\n          label={NULL_LABEL}\n          labelAlign=\"end\"\n          labelStyle={{ fontSize: 14, fill: t.inkSoft }}\n          lineStyle={{ stroke: t.inkSoft, strokeDasharray: \"6 4\", strokeWidth: 1.5 }}\n        />\n        <ForestMarks />\n        <ChartsXAxis />\n        <ChartsYAxis />\n      </ChartsContainer>\n    </Box>\n  );\n}\n"}