{"spec_id":"errorbar-asymmetric","library":"muix","language":"javascript","code":"// anyplot.ai\n// errorbar-asymmetric: Asymmetric Error Bars Plot\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 84/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\";\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, 'Segoe UI', sans-serif\";\nconst TITLE = \"errorbar-asymmetric · javascript · muix · anyplot.ai\";\nconst TITLE_HEIGHT = 58;\nconst SUBTITLE_HEIGHT = 42;\nconst MARGIN = { top: 20, right: 48, bottom: 20, left: 24 };\nconst PLATEAU_SPREAD_SHARE = 0.85;\nconst Y_PAD_TOP_SHARE = 0.12;\nconst Y_PAD_BOTTOM_SHARE = 0.04;\nconst CAP_BAND_SHARE = 0.28;\nconst MARKER_RADIUS = 10;\nconst POINT_COLOR = t.palette[0];\n\n// Columns: set these to your data's column names\nconst DOSE = \"Nitrogen dose (kg/ha)\"; // role: x\nconst YIELD_GAIN = \"Median yield increase (%)\"; // role: y\nconst LOWER_ERROR = \"10th percentile gap (%)\"; // role: error_lower\nconst UPPER_ERROR = \"90th percentile gap (%)\"; // role: error_upper\n\n// Data: replace this block with your own records\nconst data = [\n  { [DOSE]: \"0\", [YIELD_GAIN]: 1, [LOWER_ERROR]: 1, [UPPER_ERROR]: 2 },\n  { [DOSE]: \"40\", [YIELD_GAIN]: 8, [LOWER_ERROR]: 3, [UPPER_ERROR]: 5 },\n  { [DOSE]: \"80\", [YIELD_GAIN]: 19, [LOWER_ERROR]: 4, [UPPER_ERROR]: 8 },\n  { [DOSE]: \"120\", [YIELD_GAIN]: 29, [LOWER_ERROR]: 6, [UPPER_ERROR]: 11 },\n  { [DOSE]: \"160\", [YIELD_GAIN]: 35, [LOWER_ERROR]: 7, [UPPER_ERROR]: 13 },\n  { [DOSE]: \"200\", [YIELD_GAIN]: 38, [LOWER_ERROR]: 8, [UPPER_ERROR]: 11 },\n  { [DOSE]: \"240\", [YIELD_GAIN]: 36, [LOWER_ERROR]: 10, [UPPER_ERROR]: 9 },\n];\n\n// Plot: reads only data and the constants above\nconst categories = data.map((r) => String(r[DOSE]));\nconst centralValues = data.map((r) => Number(r[YIELD_GAIN]));\nconst lowerErrors = data.map((r) => Number(r[LOWER_ERROR]));\nconst upperErrors = data.map((r) => Number(r[UPPER_ERROR]));\nconst totalSpreads = data.map((r) => Number(r[LOWER_ERROR]) + Number(r[UPPER_ERROR]));\n\nconst lowerBounds = data.map((r) => Number(r[YIELD_GAIN]) - Number(r[LOWER_ERROR]));\nconst upperBounds = data.map((r) => Number(r[YIELD_GAIN]) + Number(r[UPPER_ERROR]));\n\nconst rawYMin = Math.min(...lowerBounds);\nconst rawYMax = Math.max(...upperBounds);\nconst ySpan = rawYMax - rawYMin;\nconst yMin = Math.min(0, rawYMin - ySpan * Y_PAD_BOTTOM_SHARE);\nconst yMax = rawYMax + ySpan * Y_PAD_TOP_SHARE;\n\nconst maxSpread = Math.max(...totalSpreads);\nconst plateauStartIndex = totalSpreads.findIndex((s) => s >= maxSpread * PLATEAU_SPREAD_SHARE);\nlet plateauEndIndex = plateauStartIndex;\nfor (let i = totalSpreads.length - 1; i >= 0; i--) {\n  if (totalSpreads[i] >= maxSpread * PLATEAU_SPREAD_SHARE) {\n    plateauEndIndex = i;\n    break;\n  }\n}\nconst PLATEAU_ANNOTATION = `Spread ≥ ${Math.round(PLATEAU_SPREAD_SHARE * 100)}% of maximum`;\nconst BOUNDS_LABEL = \"10th–90th percentile range\";\n\nfunction ErrorBars() {\n  const xScale = useXScale() as any;\n  const yScale = useYScale() as any;\n\n  const capHalfWidth = Math.min(18, xScale.bandwidth() * CAP_BAND_SHARE);\n  const halfGap = (xScale.step() - xScale.bandwidth()) / 2;\n  const [yRange0, yRange1] = yScale.range();\n  const plotTop = Math.min(yRange0, yRange1);\n  const plotBottom = Math.max(yRange0, yRange1);\n\n  const showPlateau = plateauStartIndex >= 0 && plateauEndIndex >= plateauStartIndex;\n  const plateauX0 = showPlateau ? xScale(categories[plateauStartIndex]) - halfGap : 0;\n  const plateauX1 = showPlateau\n    ? xScale(categories[plateauEndIndex]) + xScale.bandwidth() + halfGap\n    : 0;\n\n  return (\n    <g>\n      {showPlateau && (\n        <g>\n          <rect\n            x={plateauX0}\n            y={plotTop}\n            width={plateauX1 - plateauX0}\n            height={plotBottom - plotTop}\n            fill={t.elevatedBg}\n            stroke={t.grid}\n            strokeDasharray=\"4 4\"\n            opacity={0.75}\n          />\n          <text\n            x={(plateauX0 + plateauX1) / 2}\n            y={plotTop + 24}\n            textAnchor=\"middle\"\n            fontSize={14}\n            fontStyle=\"italic\"\n            fontFamily={FONT}\n            fill={t.inkSoft}\n          >\n            {PLATEAU_ANNOTATION}\n          </text>\n        </g>\n      )}\n\n      {categories.map((cat, i) => {\n        const cx = xScale(cat) + xScale.bandwidth() / 2;\n        const yTop = yScale(centralValues[i] + upperErrors[i]);\n        const yBottom = yScale(centralValues[i] - lowerErrors[i]);\n        const yMid = yScale(centralValues[i]);\n\n        return (\n          <g key={cat}>\n            <line\n              x1={cx}\n              x2={cx}\n              y1={yTop}\n              y2={yBottom}\n              stroke={POINT_COLOR}\n              strokeWidth={3}\n            />\n            <line\n              x1={cx - capHalfWidth}\n              x2={cx + capHalfWidth}\n              y1={yTop}\n              y2={yTop}\n              stroke={POINT_COLOR}\n              strokeWidth={3}\n            />\n            <line\n              x1={cx - capHalfWidth}\n              x2={cx + capHalfWidth}\n              y1={yBottom}\n              y2={yBottom}\n              stroke={POINT_COLOR}\n              strokeWidth={3}\n            />\n            <circle\n              cx={cx}\n              cy={yMid}\n              r={MARKER_RADIUS}\n              fill={POINT_COLOR}\n              stroke={t.pageBg}\n              strokeWidth={3}\n            />\n          </g>\n        );\n      })}\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\n  return (\n    <Box\n      sx={{\n        width: chartWidth,\n        height: window.ANYPLOT_SIZE.height,\n        backgroundColor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        fontFamily: FONT,\n      }}\n    >\n      <Box\n        sx={{\n          height: TITLE_HEIGHT,\n          display: \"flex\",\n          alignItems: \"flex-end\",\n          justifyContent: \"center\",\n          pb: \"4px\",\n          flexShrink: 0,\n        }}\n      >\n        <Typography sx={{ color: t.ink, fontSize: 22, fontWeight: 600, fontFamily: FONT }}>\n          {TITLE}\n        </Typography>\n      </Box>\n      <Box\n        sx={{\n          height: SUBTITLE_HEIGHT,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          gap: \"28px\",\n          flexShrink: 0,\n        }}\n      >\n        <Box sx={{ display: \"flex\", alignItems: \"center\", gap: \"8px\" }}>\n          <Box\n            sx={{\n              width: 14,\n              height: 14,\n              borderRadius: \"50%\",\n              backgroundColor: POINT_COLOR,\n              border: `2px solid ${t.pageBg}`,\n              boxShadow: `0 0 0 1px ${POINT_COLOR}`,\n            }}\n          />\n          <Typography sx={{ color: t.ink, fontSize: 15, fontFamily: FONT }}>\n            {YIELD_GAIN}\n          </Typography>\n        </Box>\n        <Box sx={{ display: \"flex\", alignItems: \"center\", gap: \"8px\" }}>\n          <svg width={16} height={22} viewBox=\"0 0 16 22\">\n            <line x1={3} x2={13} y1={2} y2={2} stroke={POINT_COLOR} strokeWidth={2.5} />\n            <line x1={3} x2={13} y1={20} y2={20} stroke={POINT_COLOR} strokeWidth={2.5} />\n            <line x1={8} x2={8} y1={2} y2={20} stroke={POINT_COLOR} strokeWidth={2.5} />\n          </svg>\n          <Typography sx={{ color: t.inkSoft, fontSize: 15, fontFamily: FONT }}>\n            {BOUNDS_LABEL}\n          </Typography>\n        </Box>\n      </Box>\n\n      <ChartsContainer\n        skipAnimation\n        width={chartWidth}\n        height={chartHeight}\n        margin={MARGIN}\n        series={[]}\n        xAxis={[\n          {\n            scaleType: \"band\",\n            data: categories,\n            height: 60,\n            label: DOSE,\n            labelStyle: { fontSize: 16, fontWeight: 600, fill: t.ink, fontFamily: FONT },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft, fontFamily: FONT },\n            tickSize: 0,\n          },\n        ]}\n        yAxis={[\n          {\n            scaleType: \"linear\",\n            min: yMin,\n            max: yMax,\n            width: 84,\n            label: YIELD_GAIN,\n            labelStyle: { fontSize: 16, fontWeight: 600, fill: t.ink, fontFamily: FONT },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft, fontFamily: FONT },\n            tickSize: 0,\n          },\n        ]}\n        sx={{\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeOpacity: 0.5 },\n        }}\n      >\n        <ChartsGrid horizontal />\n        <ErrorBars />\n        <ChartsXAxis />\n        <ChartsYAxis />\n      </ChartsContainer>\n    </Box>\n  );\n}\n"}