{"spec_id":"bar-diverging","library":"muix","language":"javascript","code":"// anyplot.ai\n// bar-diverging: Diverging Bar Chart\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 87/100 | Updated: 2026-11-08\nimport { BarChart } from \"@mui/x-charts/BarChart\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TITLE = \"bar-diverging · javascript · muix · anyplot.ai\";\nconst TITLE_H = 84;\nconst PAD_SHARE = 0.1; // room beyond the longest bar for its value label\n// Direction colours: brand green above zero, the semantic red anchor below\nconst POS_COLOR = t.palette[0];\nconst NEG_COLOR = t.palette[4];\n\n// Columns: set these to your data's column names\nconst STATEMENT = \"Survey statement\"; // role: category\nconst NET_SCORE = \"Net agreement score (percentage points)\"; // role: value\n\n// Data: replace this block with your own records, one object per row\n// Employee engagement survey: net agreement = % agree minus % disagree per statement\nconst data = [\n  { [STATEMENT]: \"Leadership communicates clearly\", [NET_SCORE]: 68 },\n  { [STATEMENT]: \"I feel valued at work\", [NET_SCORE]: 52 },\n  { [STATEMENT]: \"Team collaboration is strong\", [NET_SCORE]: 41 },\n  { [STATEMENT]: \"My workload is manageable\", [NET_SCORE]: 22 },\n  { [STATEMENT]: \"Growth opportunities are clear\", [NET_SCORE]: 9 },\n  { [STATEMENT]: \"Compensation feels fair\", [NET_SCORE]: -8 },\n  { [STATEMENT]: \"Feedback is timely and useful\", [NET_SCORE]: -19 },\n  { [STATEMENT]: \"Meetings are a good use of time\", [NET_SCORE]: -34 },\n  { [STATEMENT]: \"Tools I use are reliable\", [NET_SCORE]: -41 },\n  { [STATEMENT]: \"Change is managed effectively\", [NET_SCORE]: -55 },\n];\n\n// Plot: reads only data and the constants above\n// Largest value first, so the chart runs from the top positive bar to the most negative\nconst rows = data.filter((d) => d[NET_SCORE] != null).sort((a, b) => b[NET_SCORE] - a[NET_SCORE]);\nconst categories = rows.map((d) => d[STATEMENT]);\n\n// One series per direction in a shared stack: each row draws a single bar, coloured\n// by its sign, on the side of zero its value points to\nconst positive = rows.map((d) => (d[NET_SCORE] >= 0 ? d[NET_SCORE] : null));\nconst negative = rows.map((d) => (d[NET_SCORE] < 0 ? d[NET_SCORE] : null));\n\n// Symmetric value axis around zero, rounded out to a half step of its magnitude\nconst reach = Math.max(...rows.map((d) => Math.abs(d[NET_SCORE]))) * (1 + PAD_SHARE) || 1;\nconst halfStep = 10 ** Math.floor(Math.log10(reach)) / 2;\nconst limit = Math.ceil(reach / halfStep) * halfStep;\nconst fmtSigned = (v: number) => `${v > 0 ? \"+\" : \"\"}${v}`;\n\nconst SUBTITLE = `${rows.length} categories, sorted by value · ${positive.filter((v) => v != null).length} above zero, ${negative.filter((v) => v != null).length} below`;\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n\n  return (\n    <Box sx={{ width, height, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <Box sx={{ height: TITLE_H, px: \"40px\", pt: \"18px\", boxSizing: \"border-box\" }}>\n        <Typography sx={{ color: t.ink, fontSize: \"22px\", fontWeight: 600, lineHeight: 1.2 }}>\n          {TITLE}\n        </Typography>\n        <Typography sx={{ color: t.inkSoft, fontSize: \"15px\", lineHeight: 1.2, mt: \"8px\" }}>\n          {SUBTITLE}\n        </Typography>\n      </Box>\n      <BarChart\n        width={width}\n        height={height - TITLE_H}\n        layout=\"horizontal\"\n        skipAnimation\n        hideLegend\n        grid={{ vertical: true }}\n        series={[\n          {\n            id: \"pos\",\n            stack: \"score\",\n            data: positive,\n            label: NET_SCORE,\n            color: POS_COLOR,\n            barLabel: (item: { value: number | null }) => (item.value == null ? null : fmtSigned(item.value)),\n            barLabelPlacement: \"outside\",\n            valueFormatter: (v: number | null) => (v == null ? null : fmtSigned(v)),\n          },\n          {\n            id: \"neg\",\n            stack: \"score\",\n            data: negative,\n            label: NET_SCORE,\n            color: NEG_COLOR,\n            barLabel: (item: { value: number | null }) => (item.value == null ? null : fmtSigned(item.value)),\n            barLabelPlacement: \"outside\",\n            valueFormatter: (v: number | null) => (v == null ? null : fmtSigned(v)),\n          },\n        ]}\n        xAxis={[\n          {\n            id: \"value\",\n            min: -limit,\n            max: limit,\n            label: NET_SCORE,\n            height: 64,\n            tickNumber: 8,\n            valueFormatter: fmtSigned,\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft },\n            labelStyle: { fontSize: 16, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            scaleType: \"band\",\n            data: categories,\n            width: 280,\n            categoryGapRatio: 0.35,\n            disableTicks: true,\n            tickLabelStyle: { fontSize: 15, fill: t.ink },\n          },\n        ]}\n        margin={{ top: 16, right: 48, bottom: 8, left: 24 }}\n        sx={{\n          \"& .MuiBarChart-label\": { fill: t.ink, fontSize: 15, fontWeight: 600 },\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeWidth: 1 },\n        }}\n      >\n        <ChartsReferenceLine x={0} axisId=\"value\" lineStyle={{ stroke: t.ink, strokeWidth: 2 }} />\n      </BarChart>\n    </Box>\n  );\n}\n"}