{"spec_id":"errorbar-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// errorbar-basic: Basic Error Bar Plot\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-24\n\nimport * as React from \"react\";\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 { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst FONT = \"-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif\";\nconst TITLE = \"errorbar-basic · javascript · muix · anyplot.ai\";\nconst Y_PAD_SHARE = 0.14;\nconst CAP_BAND_SHARE = 0.22;\nconst MARKER_RADIUS = 10;\nconst SERIES_COLOR = t.palette[0];\n\n// Columns: set these to your data's column names\nconst CONDITION = \"Growth condition\"; // role: x\nconst ELONGATION = \"Mean stem elongation (cm / week)\"; // role: y\nconst ERROR = \"Standard deviation (cm / week)\"; // role: error\n\n// Data: replace this block with your own records\nconst data = [\n  { [CONDITION]: \"Control\", [ELONGATION]: 4.2, [ERROR]: 0.5 },\n  { [CONDITION]: \"Low Temp\", [ELONGATION]: 2.8, [ERROR]: 0.7 },\n  { [CONDITION]: \"High Temp\", [ELONGATION]: 3.1, [ERROR]: 0.6 },\n  { [CONDITION]: \"Low pH\", [ELONGATION]: 2.4, [ERROR]: 0.8 },\n  { [CONDITION]: \"High pH\", [ELONGATION]: 3.6, [ERROR]: 0.5 },\n  { [CONDITION]: \"Drought\", [ELONGATION]: 1.9, [ERROR]: 0.6 },\n  { [CONDITION]: \"Flood\", [ELONGATION]: 2.2, [ERROR]: 0.9 },\n];\n\n// Plot: reads only data and the constants above\nconst categories = data.map((r) => String(r[CONDITION]));\nconst means = data.map((r) => Number(r[ELONGATION]));\nconst errors = data.map((r) => Number(r[ERROR]));\n\nconst lowerBounds = data.map((r) => Number(r[ELONGATION]) - Number(r[ERROR]));\nconst upperBounds = data.map((r) => Number(r[ELONGATION]) + Number(r[ERROR]));\nconst rawMin = Math.min(...lowerBounds);\nconst rawMax = Math.max(...upperBounds);\nconst span = rawMax - rawMin;\nconst yMin = rawMin - span * Y_PAD_SHARE;\nconst yMax = rawMax + span * Y_PAD_SHARE;\nconst SUBTITLE = `Error bars: ± ${ERROR} · ${categories.length} ${CONDITION}s`;\n\nfunction ErrorBars() {\n  const xScale = useXScale(\"x\") as any;\n  const yScale = useYScale(\"y\") as any;\n\n  const bw = xScale.bandwidth();\n  const cap = Math.min(20, Math.round(bw * CAP_BAND_SHARE));\n\n  return (\n    <g>\n      {categories.map((cond, i) => {\n        const cx = xScale(cond) + bw / 2;\n        const yTop = yScale(means[i] + errors[i]);\n        const yBot = yScale(means[i] - errors[i]);\n        const yMid = yScale(means[i]);\n        return (\n          <g key={cond}>\n            <line\n              x1={cx}\n              y1={yTop}\n              x2={cx}\n              y2={yBot}\n              stroke={SERIES_COLOR}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={cx - cap}\n              y1={yTop}\n              x2={cx + cap}\n              y2={yTop}\n              stroke={SERIES_COLOR}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={cx - cap}\n              y1={yBot}\n              x2={cx + cap}\n              y2={yBot}\n              stroke={SERIES_COLOR}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n            />\n            <circle\n              cx={cx}\n              cy={yMid}\n              r={MARKER_RADIUS}\n              fill={SERIES_COLOR}\n              stroke={t.pageBg}\n              strokeWidth={3}\n            />\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const W = window.ANYPLOT_SIZE.width;\n  const H = window.ANYPLOT_SIZE.height;\n  const headerH = 84;\n  const chartH = H - headerH;\n\n  return (\n    <div\n      style={{\n        width: W,\n        height: H,\n        backgroundColor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        fontFamily: FONT,\n      }}\n    >\n      <div\n        style={{\n          height: headerH,\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: 6,\n        }}\n      >\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\n      <ChartsContainer\n        width={W}\n        height={chartH}\n        skipAnimation\n        series={[]}\n        xAxis={[\n          {\n            id: \"x\",\n            scaleType: \"band\",\n            data: categories,\n            height: 60,\n            label: CONDITION,\n            labelStyle: { fontSize: 16, fontWeight: 600, fill: t.ink, fontFamily: FONT },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft, fontFamily: FONT },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"y\",\n            scaleType: \"linear\",\n            min: yMin,\n            max: yMax,\n            width: 88,\n            label: ELONGATION,\n            labelStyle: { fontSize: 16, fontWeight: 600, fill: t.ink, fontFamily: FONT },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft, fontFamily: FONT },\n          },\n        ]}\n        margin={{ top: 20, bottom: 20, left: 24, right: 48 }}\n      >\n        <ChartsGrid horizontal />\n        <ErrorBars />\n        <ChartsXAxis axisId=\"x\" />\n        <ChartsYAxis axisId=\"y\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}