{"spec_id":"bar-error","library":"muix","language":"javascript","code":"// anyplot.ai\n// bar-error: Bar Chart with Error Bars\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 { BarPlot } from \"@mui/x-charts/BarChart\";\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 X_PAD_SHARE = 0.08;\nconst CAP_WIDTH_SHARE = 0.24;\n\n// Columns: set these to your data's column names\nconst CATEGORY = \"Support channel\"; // role: category\nconst VALUE = \"Mean satisfaction score (0–10)\"; // role: value\nconst ERROR = \"Standard deviation (±1 SD)\"; // role: error\nconst SAMPLE_SIZE = \"Respondents per channel\"; // role: extra\n\n// Data: replace this block with your own records, one object per row\nconst data = [\n  { [CATEGORY]: \"Live Chat\", [VALUE]: 8.4, [ERROR]: 0.6, [SAMPLE_SIZE]: 150 },\n  { [CATEGORY]: \"Phone\", [VALUE]: 7.8, [ERROR]: 0.7, [SAMPLE_SIZE]: 150 },\n  { [CATEGORY]: \"Email\", [VALUE]: 7.2, [ERROR]: 0.9, [SAMPLE_SIZE]: 150 },\n  { [CATEGORY]: \"Social Media\", [VALUE]: 6.9, [ERROR]: 0.95, [SAMPLE_SIZE]: 150 },\n  { [CATEGORY]: \"Self-Service\", [VALUE]: 6.5, [ERROR]: 1.1, [SAMPLE_SIZE]: 150 },\n];\n\n// Plot: reads only data and the constants above\nconst channels = data.map((d) => d[CATEGORY]);\nconst scores = data.map((d) => d[VALUE]);\nconst maxUpper = Math.max(...data.map((d) => d[VALUE] + d[ERROR]));\nconst minLower = Math.min(...data.map((d) => d[VALUE] - d[ERROR]));\nconst paddedMax = maxUpper * (1 + X_PAD_SHARE);\nconst paddedMin = Math.min(0, minLower * (1 + X_PAD_SHARE));\nconst niceStep = Math.pow(10, Math.floor(Math.log10(paddedMax - paddedMin))) / 2;\nconst xMax = Math.ceil(paddedMax / niceStep) * niceStep;\nconst xMin = Math.floor(paddedMin / niceStep) * niceStep;\nconst formatValue = (v: number) => v.toLocaleString(\"en-US\");\nconst minSample = Math.min(...data.map((d) => d[SAMPLE_SIZE]));\nconst maxSample = Math.max(...data.map((d) => d[SAMPLE_SIZE]));\nconst sampleText =\n  minSample === maxSample\n    ? minSample.toLocaleString(\"en-US\")\n    : `${minSample.toLocaleString(\"en-US\")}–${maxSample.toLocaleString(\"en-US\")}`;\n\nfunction ErrorBars() {\n  const xScale = useXScale(\"x\") as any;\n  const yScale = useYScale(\"y\") as any;\n  const bw = yScale.bandwidth();\n  const cap = bw * CAP_WIDTH_SHARE;\n\n  return (\n    <g data-drawing-container>\n      {data.map((row) => {\n        const channel = row[CATEGORY];\n        const cy = yScale(channel) + bw / 2;\n        const xLow = xScale(row[VALUE] - row[ERROR]);\n        const xHigh = xScale(row[VALUE] + row[ERROR]);\n        return (\n          <g key={channel}>\n            <line\n              x1={xLow}\n              y1={cy}\n              x2={xHigh}\n              y2={cy}\n              stroke={t.ink}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={xLow}\n              y1={cy - cap / 2}\n              x2={xLow}\n              y2={cy + cap / 2}\n              stroke={t.ink}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={xHigh}\n              y1={cy - cap / 2}\n              x2={xHigh}\n              y2={cy + cap / 2}\n              stroke={t.ink}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n            />\n            <text\n              x={xHigh + 12}\n              y={cy + 5}\n              fontSize={14}\n              fontWeight={600}\n              fill={t.inkSoft}\n            >\n              {`${formatValue(row[VALUE])} ± ${formatValue(row[ERROR])}`}\n            </text>\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nconst TITLE = \"bar-error · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${CATEGORY}: ${channels.length} · ${SAMPLE_SIZE}: ${sampleText}`;\nconst HEADER_H = 100;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <div\n      style={{\n        width,\n        height,\n        backgroundColor: t.pageBg,\n        display: \"flex\",\n        flexDirection: \"column\",\n        fontFamily:\n          '-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif',\n      }}\n    >\n      <div\n        style={{\n          height: HEADER_H,\n          padding: \"14px 48px 0 48px\",\n          display: \"flex\",\n          flexDirection: \"column\",\n          alignItems: \"center\",\n          boxSizing: \"border-box\",\n        }}\n      >\n        <div\n          style={{\n            fontSize: 22,\n            fontWeight: 600,\n            color: t.ink,\n            lineHeight: 1.25,\n          }}\n        >\n          {TITLE}\n        </div>\n        <div\n          style={{\n            fontSize: 15,\n            color: t.inkSoft,\n            marginTop: 4,\n          }}\n        >\n          {SUBTITLE}\n        </div>\n        <div\n          style={{\n            display: \"flex\",\n            alignItems: \"center\",\n            gap: 20,\n            marginTop: 6,\n            fontSize: 14,\n            color: t.inkSoft,\n          }}\n        >\n          <span style={{ display: \"inline-flex\", alignItems: \"center\", gap: 6 }}>\n            <span\n              style={{\n                width: 14,\n                height: 12,\n                borderRadius: 3,\n                backgroundColor: t.palette[0],\n                display: \"inline-block\",\n              }}\n            />\n            <span>{VALUE}</span>\n          </span>\n          <span style={{ display: \"inline-flex\", alignItems: \"center\", gap: 6 }}>\n            <svg width=\"20\" height=\"12\" style={{ display: \"inline-block\" }}>\n              <line x1=\"2\" y1=\"6\" x2=\"18\" y2=\"6\" stroke={t.ink} strokeWidth=\"2.5\" />\n              <line x1=\"2\" y1=\"2\" x2=\"2\" y2=\"10\" stroke={t.ink} strokeWidth=\"2.5\" />\n              <line x1=\"18\" y1=\"2\" x2=\"18\" y2=\"10\" stroke={t.ink} strokeWidth=\"2.5\" />\n            </svg>\n            <span>{ERROR}</span>\n          </span>\n        </div>\n      </div>\n      <ChartsContainer\n        width={width}\n        height={height - HEADER_H}\n        skipAnimation\n        series={[\n          {\n            type: \"bar\",\n            data: scores,\n            id: \"satisfaction\",\n            label: VALUE,\n            layout: \"horizontal\",\n            color: t.palette[0],\n            xAxisId: \"x\",\n            yAxisId: \"y\",\n          },\n        ]}\n        xAxis={[\n          {\n            id: \"x\",\n            scaleType: \"linear\",\n            min: xMin,\n            max: xMax,\n            valueFormatter: formatValue,\n            height: 68,\n            label: VALUE,\n            labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"y\",\n            scaleType: \"band\",\n            data: channels,\n            width: 140,\n            label: CATEGORY,\n            labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n          },\n        ]}\n        margin={{ top: 20, bottom: 24, left: 40, right: 90 }}\n        sx={{\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n        }}\n      >\n        <ChartsGrid vertical />\n        <BarPlot borderRadius={4} skipAnimation />\n        <ErrorBars />\n        <ChartsXAxis axisId=\"x\" />\n        <ChartsYAxis axisId=\"y\" />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}